=IF(MONTH(DATE(YEAR(D6),MONTH(D6),1))<>MONTH(DATE(YEAR(D6),MONTH(D6),1)-(WEEKDAY(DATE(YEAR(D6),MONTH(D6),1))-0)+{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1),"",DATE(YEAR(D6),MONTH(D6),1)-(WEEKDAY(DATE(YEAR(D6),MONTH(D6),1))-0)+{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1)