Are you talking about transposing? If so, you simply figure out the scale degrees of all the notes of your original piece and apply them to a different key. There is a strict mathematical relationship, if that's what you mean with "formula", but most people simply know their scales and chords well and know what the tonic, dominant, I, IV, V chords, and so on are in all keys.