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
HassanAli123
Helper I
Helper I

calculate duration of two Dates

Hi,
     i have 2 dates columns. i want to calculate the "duration" of start date and end date and the format should be like "5 year and 4 months". how to do it in DAX
Thanks,

Date.PNG

4 REPLIES 4
v-yanjiang-msft
Community Support
Community Support

Hi @HassanAli123 ,

Has your problem been solved by @jdbuchanan71 's solution? If yes, please consider accept it as the solution to help the other members find it more quickly. If not, please feel free to let me know.

Best Regards,
Community Support Team _ kalyj

jdbuchanan71
Super User
Super User

Here is one I was messing with that does just years and months.

Duration = 
VAR _Months = DATEDIFF ( 'Table'[Start Date], 'Table'[End Date], MONTH )
VAR _Years = INT ( DIVIDE ( _Months, 12 ) )
VAR _RemMonths = MOD ( _Months, 12 )
VAR _YearText = IF ( _Years = 0, BLANK(), IF ( _Years = 1, _Years & " year and ", _Years & " years and " ) )
VAR _MonthText = _RemMonths & IF ( _RemMonths = 1, " month", " months" )
RETURN 
_YearText & _MonthText

jdbuchanan71_0-1647355481715.png

 

mh2587
Super User
Super User

Measure = 
VAR __daysinMonth = DAY(EOMONTH([Date1],0))
VAR __years = DATEDIFF([Date1],[Date2],YEAR) - 1
VAR __months = IF(MONTH([Date1])<MONTH([Date2]),MONTH([Date2])-MONTH([Date1]),12-MONTH([Date1])+MONTH([Date2])-1)
VAR __days = IF(DAY([Date1])<DAY([Date2]),DAY([Date2])-DAY([Date1]),DAY([Date2])+(__daysinMonth-DAY([Date1])))
RETURN __years & " year, " & __months & " months and " & __days & " days"
//Try this with your requirements

 


Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



mh2587
Super User
Super User

https://community.powerbi.com/t5/Desktop/Calculating-the-difference-between-two-dates-in-Years-Month...

referring you to this link


Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



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.