Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

DateTimeDiff | How to account for Leap Years

maier_alexander_MO
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:

 

maier_alexander_MO_0-1583156188969.pngmaier_alexander_MO_1-1583156206757.png

 

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

4 REPLIES 4
neilgallen
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.

LukeG
Alteryx Alumni (Retired)

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

 

Leap Year.PNG

 

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.

 

RolandSchubert
16 - Nebula
16 - Nebula

Hi Alex,

 

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

 

02-03-_2020_15-23-20.png

 

Best,

 

Roland

maier_alexander_MO
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'
 ENDIF
ELSE
 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'
 ENDIF
ENDIF
Labels