Forum Discussion

nadir's avatar
nadir
Frequent Visitor
5 years ago
Solved

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...
  • v-jingzhang's avatar
    5 years ago

    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

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