# Alteryx Designer Discussions

###### ALTER.NEXT:

Join us on Dec 2 for a half-day virtual analytics + data science event!
US & CA customers only

It's the most wonderful time of the year - Santalytics 2020 is here! This year, Santa's workshop needs the help of the Alteryx Community to help get back on track, so head over to the Group Hub for all the info to get started!
SOLVED

## DateTimeDiff | How to account for Leap Years

Highlighted
8 - Asteroid

Hi all,

I am having the Period End Date as well as the Expiry date of certain contracts. I now want to calculate the difference between Expiry date and Period end date in Years. I have tried the following formula:

(DateTimeDiff([Expiry date],[PeriodDate],"days"))/365

If the result is negative, this means the contract is overdue, if it is positive i want to group it into maturity buckets.

Everything is working as intended except for the leap years. Due to those, the calculation of DateTimeDiff/365 does often not give me the correct numbers. When calculating the time difference between 31/12/2020 and 31/12/2019 I would actually have to divide by 366 since there is an additional day in February.

Is there a way to design this flexibly, even for longer time periods? Please see my formulas and my time buckets down below:

I have already tried things like using 1.00247 instead of 1 in the AgeBucket calclulation, but that will of course falsify the buckets in a non-leap year.

Thank you very much for your help!

Kind regards,

Alex

Highlighted
12 - Quasar

You can use 'year' as a unit in the datetimediff function. Is there a reason you're doing by days and then doing the division?

I believe it accounts for the leap day accordingly.

Highlighted
Alteryx

My approach would start with making the static value of 365 more dynamic by using the DateTimeDiff() function.

This screenshot demonstrates the ability to dynamically change the value of 365 to 366 for a leap year (if the dates used come from a field).

Let me know if this is at all helpful.

Highlighted
Alteryx Certified Partner

Hi Alex,

interesting problem. I tried a different approach integrationg both steps (difference calculation and assign to bucket) - what do you think?

Best,

Roland

Highlighted
8 - Asteroid

Hi all,

thank you so much for your quick help and the ideas! Sadly the DateTimeDiff for years does not show decimals, which is a shame.

@RolandSchubert This is a great idea, thank you so much! I just made minor changes with regards to the ">=" to adjust for my cause.

That is a an awesome solution, have a great day!

 IF DateTimeDiff([PeriodDate], [Expiry date], 'days') >= 0 THEN IF DateTimeDiff(DateTimeAdd([PeriodDate], -1, 'years'), [Expiry date],  'days') < 0 THEN  'Overdue<1Y' ELSE  'Overdue>1Y' ENDIFELSE IF DateTimeDiff(DateTimeAdd([PeriodDate], 1, 'years'), [Expiry date],  'days') >= 0 THEN  'Maturity<1Y' ELSEIF DateTimeDiff(DateTimeAdd([PeriodDate], 2, 'years'), [Expiry date],  'days') >= 0 THEN  'Maturity<2Y' ELSEIF DateTimeDiff(DateTimeAdd([PeriodDate], 5, 'years'), [Expiry date],  'days') >= 0 THEN  'Maturity<5Y' ELSEIF DateTimeDiff(DateTimeAdd([PeriodDate], 10, 'years'), [Expiry date],  'days') >= 0 THEN  'Maturity<10Y' ELSEIF DateTimeDiff(DateTimeAdd([PeriodDate], 25, 'years'), [Expiry date],  'days') >= 0 THEN  'Maturity<25Y' ELSE  'Maturity>25Y' ENDIFENDIF
Labels