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.
when MONTH(CURDATE()) = MONTH(`Entry Date`) then 'This Month'
when MONTH(CURDATE()) - 1 = MONTH(`Entry Date`) then 'Last Month'
else 'Other Month'
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.
when MONTH(`Latest Hire date`) = MOD(MONTH(CURDATE()),12) + 1 then 'Next Month'
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
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)
Alternatively yes, you can just filter for last month in the date selector which is probably the cleanest 🙂
@GrantSmith yea not sure why I never thought to use that before. I think with my other concept looking forward I had hire dates and was needing to know which anniversary dates were coming up so the default Domo options would not work. But for looking back I think they should work fine.