Indicates substitution: doing or using something in place of something else (i.e., instead of).
車の代わりに自転車で通勤している。
I commute by bicycle instead of by car.
今日はケーキの代わりにフルーツを出します。
Today we'll serve fruit instead of cake.
彼の仕事を代わりに引き受けてあげた。
I took on his work in his place.
忙しいから、私が代わりに電話しておくよ。
You're busy, so I'll call in your stead.
Indicates doing something in exchange or as compensation/in return for something else.
手伝ってくれたお礼に、代わりに夕食をおごるよ。
As a thank-you for helping, I'll treat you to dinner in return.
今日は私が出勤する代わりに、明日は君が代わってくれないか。
I'll go to work today; in return, could you cover for me tomorrow?
約束を破った代わりに、きちんと説明するべきだ。
Instead of breaking a promise, you should at least explain properly as compensation.