I struggled a bit with this, so I figured I’d leave this here.
Dividing the day of the month by 7 and rounding up would work… if the week started on a Sunday (or whatever day you choose to start the week with).
To account for the shift in days, shift ‘back’ by subtracting the day of week from the day.