It's set when you receive the egg from the client, usually as the ??? reward for a mission. This means you can soft reset for the egg, but it means that you have to save before you complete the mission and you must play a few filler days until the egg hatches.
Also, the egg's species is determined by the dungeon of the mission, and the egg will contain a Pokemon species that's found in the dungeon.
You can also use Wonder Mail generator to ensure you get a certain egg as well.
Source - Games FAQ