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
frankhofmans
Helper IV
Helper IV

Average lenght of employment with multiple contracts

hi all,

 

i want to calculate the moving average length of employment in weeks. I created the following measure:

 

Average lengt employment = CALCULATE(averageX('Employments',
        VAR date1 = MAX( 'Employments'[EMPLOYMENT_START] , MIN('Employments'[EMPLOYMENT_START]) )
        VAR date2 = IF('Employments'[EMPLOYMENT_END]= BLANK(),
            MAX('Date table'[Date]),
            MIN( 'Employments'[EMPLOYMENT_END], MAX('Date table'[Date]))
        )
        VAR weeks = DATEDIFF(date1, date2, WEEK)
       RETURN weeks))
 
This formula works, but i have a lot of employee who start with a first contract (mostly a month), followed by a contract for a year or a permanent contract. With this formula the average of both separate contracts is taken, but i want the total average length (so the length of both contracts).
 
Employee IDStartEnd
0011 jan 202131 jan 2021
0011 feb 202131 jan 2022
0021 mar 2021 
0031 mar 202131 mar 2021
0031 apr 20211 jun 2022
Etc  
   
   

 

Does anyone know ho i can get this result?

 

Thanks in advance,

 

Regards, Frank

1 ACCEPTED SOLUTION

@frankhofmans 

Please check the attched file

Fowmy_0-1661950660263.png

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

5 REPLIES 5
Fowmy
Super User
Super User

@frankhofmans 

Create the following measure and give it try:

Avg Weeks = 
VAR __WeeksTable =
    ADDCOLUMNS (
        ADDCOLUMNS (
            VALUES ( Employments[Employee ID] ),
            "@StartDate", CALCULATE ( MIN ( Employments[Start] ) ),
            "@EndDate", COALESCE ( CALCULATE ( MAX ( Employments[End] ) ), MAX('Date table'[Date]) )
        ),
        "Weeks", DATEDIFF ( [@StartDate], [@EndDate], WEEK )
    )
RETURN
    CALCULATE ( AVERAGEX ( __WeeksTable, [Weeks] ) )
Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

hi Fowmy,

 

Thanks for your reply. I can't duplicate the formula. 2 times addcolumns? and the "@" parts in the formula are also VAR's? If i copy the formula, i can't select "Weektabel" after the calculate(averagex....


Thanks in advance!

 

Regards,

 

Frank

@frankhofmans 

Please an example data in a PBIX file, you can save the file in Google Drive, One Drive etc and share the link here.


Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

@frankhofmans 

Please check the attched file

Fowmy_0-1661950660263.png

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

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.