Previous Month

Florida 🟣

So I need to create a BM to give me the previous month, I use this as a filter for a report to send people the previous month's data. I found that with the year change my current formula does not catch December if it is January. I am looking for a filter to give me the rows where Entry Date is during the previous month.

case
when MONTH(CURDATE()) = MONTH(`Entry Date`) then 'This Month'
when MONTH(CURDATE()) - 1 = MONTH(`Entry Date`) then 'Last Month'
else 'Other Month'
end

I have a similar concept that looks for the next month and I found a way to use MOD in order to capture year changes. I tried applying the same concept in order to get the previous month but I could not get it. Here was my next month BM.

case
when MONTH(`Latest Hire date`) = MOD(MONTH(CURDATE()),12) + 1 then 'Next Month'
else 'Other'

end

For now I have added this line to my BM to give me the previous month when it is Jan but does anyone have something similar to the MOD fix that will allow me to just have one line to account for all cases? I hate having to do a manual case.

when MONTH(CURDATE()) = 1 and MONTH(`Entry Date`) = 12 then 'Last Month' -- manual fix to get December after year change

• Florida 🟣

Just realized I don't even need a BM and could just use normal date operations for the card.

• Indiana 🥷

There's several ways to tackle this. One possible way is to utilize a date dimension to calculate last month data. This has been outlined before on the Dojo.

Alternatively the quickest option would be to utilize the LAST_DAY function in your beast mode.

CASE
WHEN LAST_DAY(`dt`) = LAST_DAY(CURRENT_DATE()) THEN 'This Month'
WHEN LAST_DAY(`dt`) = LAST_DAY(DATE_SUB(CURRENT_DATE(), interval 1 month)) THEN 'Last Month'
WHEN LAST_DAY(`dt`) = LAST_DAY(DATE_ADD(CURRENT_DATE(), interval 1 month)) THEN 'Next Month'
ELSE 'Other Month'
END

This will also protect against accidentally filtering data from a prior month and prior year (for example December 2019 and December 2020 for this month)