When talking about things you "had to", "could", or "wanted to" do in the past, you use the passato prossimo of modal verbs (dovere, potere, volere). Because the passato prossimo requires an auxiliary (helper) verb, how do you know whether to use avere or essere?
Choosing the Auxiliary Verb
The rule is simple: look at the infinitive verb that comes right after the modal. The modal verb will "borrow" whichever auxiliary verb that main action normally uses.
If the main action verb normally takes avere (like cancellare or fare), use avere with the modal verb. As usual with avere, the past participle of the modal (dovuto, potuto, voluto) ends in -o.
When the main verb takes "essere"
If the main action verb normally uses essere (typically verbs of motion or state of being, like restare, venire, or tornare), you must use essere as your auxiliary.
Crucially, whenever you use essere, the past participle of the modal verb must change its ending to agree in gender and number with the subject (-o, -a, -i, or -e).
Reflexive Verbs
When giving an excuse, you might need to use a reflexive verb like scusarsi (to apologize). When the reflexive pronoun (mi, ti, si, etc.) is placed before the modal verb, you must use essere. Just like before, the past participle will agree with the subject.
Putting it together
To explain why you had to or couldn't do something, it is helpful to use words like siccome (since) to introduce your reason.
Notice how in longer excuses, you might use both rules in the exact same sentence depending on the verbs you choose: