DATEDIF in Power Query

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,912
Office Version
  1. 365
Platform
  1. Windows
Hi

I am using Power Query (in Power BI Desktop), and I have a function to calculate difference between dates, in Years. I cannot find a DATEDIF equivalent, and battling to handle variable days in a year.

I have projected values with dates from 01-Jan-2023 through to 30 years from now, and can be attributed to any days on any months.

The base date to calculate the difference from (the effective date) will always be a quarter end date, being 31-Dec-YY; 31-Mar-YY, 30-Jun-YY, 30-Sep-YY.

When calculating the difference in Years, I need it to follow the same convention. E.g:

Effective (Reporting) Date: 30-Jun-2023
Projection date: 29-Jun-2024 Result: 0 Years
Projection date: 30-Jun-2024 Result: 1 Year
Projection date: 01-Jul-2024 Result: 1 Year

So effectively I believe I want to emulate DATEDIF in Excel.

I haven't figured out the code for this. Here is my initial rough attempt; which does not yield correct result because of different # days in years (leap years):

Power Query:
(EffectiveDate as date, ProjectionDate as date) =>
let
    A = Duration.Days(ProjectionDate - EffectiveDate),
    B = if A < 1 then -1 else A/365.25,
    C = B - Number.Mod(B,1)
in
    C

A little help please?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top