Do you mind sharing which type you take (brand as well)? I tried magnesium glycinate supplements for two nights in a row and they kept me wide awake.
Most magnesium doesn't cross the blood brain barrier, such as the one you tried.
Try magnesium threonate, which does.