Calcium and carbonates bind to form calcium carbonate, which cannot be used by coral. Magnesium, roughly explained, acts as a buffer between calcium and carbonates to keep them from bonding. Carbonates will also bond with magnesium, but will also disassociate at a high pH.
Always get Mg correct first. Second, fix your alkalinity. Third, worry about your calcium. These elements need enough time to be absorbed into the water column so they do not precipitate out. Always pour them in slowly to avoid a super saturation in one part of your tank. This also help prevent precipitation.
A high Mg level is necessary for stable Ca and KH levels. Corals will live in the presense of low calcium, but will die if your alkalinity drops. That's why you address them in that order.
Mg 1200-1300ppm
dKH 8-12
Ca min. 380ppm max 450ppm