I’m not sure I have a full visual of what you are trying to do. Seems that you already have the start and end dates figured out, so you just need the year and month? A good calculation for that is as follows.
YEAR(date)*100+MONTH(date)
This will give you YYYYMM which is easy to use for IF comparisons because you can compare in proper numerical order.