Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
KulCA
New Member

DATEDIFF formula to return Years and Months

Hello,

 

I have a formula in Excel where i can calculate the Date Difference in years and months by using the interval "Y" for years and "YM" for months so my date difference would be diplayed as 3 years and 8 months.

 

In Power BI, I have found the correct formula to calculate the year so it returns 3 years

DATEDIFF([Hire Date],[Report Date],YEAR)

 

But how can i get the equivalent of the Excel "YM"?  If i do the same formula as above with the MONTH interval, i will get the total number of months from one date to another. However, i only want the number of months that are over the 3 years.  Is there anyway i can do this in BI?

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

YearMonths = 
VAR Years = DATEDIFF([Hire Date],[Report Date],YEAR) 
VAR Months = DATEDIFF([Hire Date],[Report Date],MONTH) - Years * 12
RETURN CONCATENATE(CONCATENATE(CONCATENATE(Years," Years, "),Months)," Months")

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

YearMonths = 
VAR Years = DATEDIFF([Hire Date],[Report Date],YEAR) 
VAR Months = DATEDIFF([Hire Date],[Report Date],MONTH) - Years * 12
RETURN CONCATENATE(CONCATENATE(CONCATENATE(Years," Years, "),Months)," Months")

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

This solution didn't work for me as I was getting negative values like "1 years, -10 months". I figured out this is because DATEDIFF using YEAR is calculated based on the calendar year of the dates, not the actual time elapsed (e.g. 365 days).

As an example, the DATEDIFF using YEAR between 31/12/2016 and 1/1/2017 is "1 year", even though it's only 1 day apart. However the DATEDIFF between 1/1/2017 and 31/12/2017 would be "0 years", despite being 364 days apart.

 

I modified the formula and this worked for me:

YearMonths = 
VAR Months = DATEDIFF([Hire Date],[Report Date],MONTH)
VAR Years =  ROUNDDOWN(Months/12,0)
RETURN CONCATENATE(CONCATENATE(CONCATENATE(Years," Years, "),Months-(Years*12))," Months")

Technically you'd have the same problem with Months, so if you need it to be more accurate you could add DAY as a variable.

hey kyle, thanks for this formula.  that worked for me, however, i am trying to create a card visualization that shows the average time but i cannot get it to work.  my new dax formula is stored as 'text', but if i swap it to time it errors so i cannot get an actual avg

Anonymous
Not applicable

Thanks! It works great! Can you also share code where you added Day? Because CONCATENATE limits to 2 items only. I appreciate your help.

KulCA
New Member

Hello,

 

I have a formula in Excel where i can calculate the Date Difference in years and months by using the interval "Y" for years and "YM" for months so my date difference would be diplayed as 3 years and 8 months.

 

In Power BI, I have found the correct formula to calculate the year so it returns 3 years

DATEDIFF([Hire Date],[Report Date],YEAR)

 

But how can i get the equivalent of the Excel "YM"?  If i do the same formula as above with the MONTH interval, i will get the total number of months from one date to another. However, i only want the number of months that are over the 3 years.  Is there anyway i can do this in BI?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.