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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
nadir
Frequent Visitor

Issue with Datediff Function

Hi,

I have an Employee table which has ID,name,Department,Hire date etc.. , I want to calculate total Service period for Employees in Years , Months , Days by substracting Hire date from Today every time the report is opened , I used two methods 
1. I created A measure as follows

Service Period in Years = DATEDIFF('pp EMPLOYEES'[HIRE_DATE_G],TODAY(),YEAR)

2. I created 2 columns , one for today's date and another  as follows

Service in Years in Column = DATEDIFF('pp EMPLOYEES'[HIRE_DATE_G],'pp EMPLOYEES'[Todays date],YEAR)
Both methods are giving me a wrong result , for example the hire date for Employee is 17/03/2012 and today's date is 13/01/2021 , I am getting 9 Years 10 Months 14 Days where the correct result must be 8 Years 9 Months 27 days
I don't know what is wrong , I checked the date format , I tried a measure , I tried a column but no luck.
 
Please help
Nadir
1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @nadir 

 

Please try the following measures. I didn't test them with many instances, so feel free to let me know if they don't work in some conditions.

Today = TODAY()
Years = INT(YEARFRAC(SELECTEDVALUE('Table'[HIRE_DATE]),[Today],1))
Months = INT(MOD(YEARFRAC(SELECTEDVALUE('Table'[HIRE_DATE]),[Today],1),1)*12)
Days =
VAR _HireDate = SELECTEDVALUE ( 'Table'[HIRE_DATE] )
VAR _Today = [Today]
VAR _LastMonthDays = DAY ( EOMONTH ( [Today], -1 ) )
VAR _days =
    SWITCH (
        TRUE (),
        DAY ( _HireDate ) <= DAY ( _Today ), DAY ( _Today ) - DAY ( _HireDate ),
        DAY ( _HireDate ) > DAY ( _Today ), DAY ( _Today ) + ( _LastMonthDays - DAY ( _HireDate ) )
    )
RETURN
    _days

011902.jpg

Regards,
Community Support Team _ Jing Zhang
If this post helps, please consider Accept it as the solution to help other members find it.

View solution in original post

5 REPLIES 5
v-jingzhang
Community Support
Community Support

Hi @nadir 

 

Please try the following measures. I didn't test them with many instances, so feel free to let me know if they don't work in some conditions.

Today = TODAY()
Years = INT(YEARFRAC(SELECTEDVALUE('Table'[HIRE_DATE]),[Today],1))
Months = INT(MOD(YEARFRAC(SELECTEDVALUE('Table'[HIRE_DATE]),[Today],1),1)*12)
Days =
VAR _HireDate = SELECTEDVALUE ( 'Table'[HIRE_DATE] )
VAR _Today = [Today]
VAR _LastMonthDays = DAY ( EOMONTH ( [Today], -1 ) )
VAR _days =
    SWITCH (
        TRUE (),
        DAY ( _HireDate ) <= DAY ( _Today ), DAY ( _Today ) - DAY ( _HireDate ),
        DAY ( _HireDate ) > DAY ( _Today ), DAY ( _Today ) + ( _LastMonthDays - DAY ( _HireDate ) )
    )
RETURN
    _days

011902.jpg

Regards,
Community Support Team _ Jing Zhang
If this post helps, please consider Accept it as the solution to help other members find it.

amitchandak
Super User
Super User

@nadir , In this case, better to try a column like

Service in Years in Column = DATEDIFF('pp EMPLOYEES'[HIRE_DATE_G],'pp EMPLOYEES'[Todays date],Month)/12.0

 

check if this works better

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak  , thanks for your reply but I am getting the same result

@nadir Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Datediff issue   .jpg

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.