It really depends on the person... magnesium supplements are not like medications / sleeping tablets. Their primary use is to help supplement our diets when we can’t get enough magnesium through food alone.
One of the first signs of a magnesium deficiency is poor sleep, as magnesium plays a key role in supporting neurotransmitters responsible for sleep. So if you're low, replenishing and maintaining levels can gradually improve sleep ... some people notice a difference quite quickly, while for others it can take a few months.
That said, sleep is complex and influenced by many factors, so magnesium is just one piece of the puzzle. And nutrition is always the first place to start.