# Compare MAX date with YOY: Month-to-Date (MTD) Comparison

Options
Member
edited April 12

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!

Tagged:

• Contributor
Options

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.

Did this comment solve your problem? Accept it as the solution!

• Contributor
Options

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.

Did this comment solve your problem? Accept it as the solution!

• Member
Options

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?

• Contributor
Options

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.

Did this comment solve your problem? Accept it as the solution!

• Member
Options

I am sure there was a much more straightforward way to achieve this but these are the steps I took:

1. 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