Compare MAX date with YOY: Month-to-Date (MTD) Comparison
Trying to create a sql statement in magic ETL to compare YOY: Month-to-Date (MTD) by MAX date. My current code from
is:CASE WHEN (MONTH(MEMBERSHIP_INSTANCE_PURCHASE_DATE
) = MONTH(CURDATE()) and YEAR(MEMBERSHIP_INSTANCE_PURCHASE_DATE
) = YEAR(CURDATE()) and DAYOFMONTH(MEMBERSHIP_INSTANCE_PURCHASE_DATE
) <=DAYOFMONTH(CURDATE())) THEN 'This Year' WHEN (MONTH(MEMBERSHIP_INSTANCE_PURCHASE_DATE
) = MONTH(CURDATE()) AND YEAR(MEMBERSHIP_INSTANCE_PURCHASE_DATE
) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 year)) and DAYOFMONTH(MEMBERSHIP_INSTANCE_PURCHASE_DATE
) <=DAYOFMONTH(CURDATE())) THEN 'Last Year' END
However since this is based on current date, there are times when there might not be a purchase on the current date so 'This Year' could pull latest date of 4/10/24 while 'Last Year' would pull current date 4/12/24 - I want to compare max dates of current period so if max date of 'This Year' is 4/10/24 then the code would classify the max date of 'Last Year' as 4/10/23.
I understand you can use the max function in dataflow, however from my research I am having a hard time creating a code to obtain my goal and am just hoping with a slight tweak of my magic etl code, I could obtain my goal. I have played with subtime function as well with this code but still learning sql so its been a struggle. Appreciate any insight and guidance!
Best Answer
-
First thing I noticed, you don't want to have DAYOFMONTH in the check for Last Year, because comparing less than or equal to would mean you skip 05-2023 through 12-2023. Second, I would suggest splitting out your calculation into different formulas or even different tiles. So, I would first calculate your max date in a group by tile. Then, I would use that column in a formula tile to subtract a year and get the previous year date.
Was this comment helpful? Click Agree or Like below.
Did this comment solve your problem? Accept it as the solution!0
Answers
-
First thing I noticed, you don't want to have DAYOFMONTH in the check for Last Year, because comparing less than or equal to would mean you skip 05-2023 through 12-2023. Second, I would suggest splitting out your calculation into different formulas or even different tiles. So, I would first calculate your max date in a group by tile. Then, I would use that column in a formula tile to subtract a year and get the previous year date.
Was this comment helpful? Click Agree or Like below.
Did this comment solve your problem? Accept it as the solution!0 -
Hey @brycec thanks so much for the comment - you gave me a renewed sense of motivation to be able to use the max function in group by and I have played around a good bit. however it seems best to create a constant max date column with the latest date so I can use that column to subtract a year, but when I use max(date field) it just pulls the same date in each row. would it be best to create a new dataset that runs everyday with this constant value and then have it join to my current dataset to create that new column?
0 -
If it is pulling the same date for every row, it sounds like you aren't grouping on the correct columns. What columns are you grouping on? You'll want to group in a way that identifies the unique row that you want the date aggregated for.
Was this comment helpful? Click Agree or Like below.
Did this comment solve your problem? Accept it as the solution!0 -
I am sure there was a much more straightforward way to achieve this but these are the steps I took:
- Create a constant max purchase date based on the CY
2. Then join the dataset output to the original dataset to create a new max column
3. Then finally create the CY & PY max columns in a new dataset output
4. Lastly, I was able to create a chart to compare the max membership purchase date of the CY to that same date in the month or less to the PY membership purchase data
Appreciate your help to get me to this point @brycec
0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 296 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 614 Datasets
- 2.2K Magic ETL
- 3.8K Visualize
- 2.5K Charting
- 729 Beast Mode
- 53 App Studio
- 40 Variables
- 677 Automate
- 173 Apps
- 451 APIs & Domo Developer
- 45 Workflows
- 8 DomoAI
- 34 Predict
- 14 Jupyter Workspaces
- 20 R & Python Tiles
- 394 Distribute
- 113 Domo Everywhere
- 275 Scheduled Reports
- 6 Software Integrations
- 121 Manage
- 118 Governance & Security
- Domo Community Gallery
- 32 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 108 Community Announcements
- 4.8K Archive