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
Miss
Frequent Visitor

Caclulate contracted hours at a dynamically specified date

I need to calulate the contracted hours per employee on a given date which can be dynamically selected through slicers and fitlers.

 

The base data I have been provided contains the employement history for the employees.

 

Based on this I need to perform the following actions:

- exclude any leavers - end date is before the specified date

- exclude any furture contracts - where effective date is after the specified date

- identify the contract with the latest date per each employee.

 

The data that has been supplied looks like the following:

Payroll NumberEffective StatusEffective DateEnd DateCareer Change ReasonHours Per Week
1Current02 January 2019 Starter6.75
1Current27 April 2019 Change of Contract12.5
2Leaver27 September 201829 March 2019Starter0
3Current29 January 2018 Starter32.75
3Current04 May 2019 Promotion32.75
4Current11 December 2017 Starter36
5Leaver02 June 201830 March 2019Internal Move0
6Current14 March 2019 Starter12.5
7Leaver06 September 201812 September 2018Starter0
8Leaver29 January 201814 April 2019Starter12.5
9Current29 January 2018 Starter12.5
9Current20 April 2019  12.5
10Current17 June 2019 Starter36
11Leaver29 January 201815 March 2019Starter26
12Current10 December 2018 Starter16
12Current27 April 2019 Change of Contract16
13Current29 January 2018 Starter32.75
13Current30 March 2019 Demotion32.75
14Leaver29 January 201806 July 2018Starter19.25
15Leaver24 March 201805 April 2018Starter26

 

Based on the specified date of the 27th July 2019 the total contract hours should calculate to be 191 hours (rows highlighted in red).

 

As I want to be able to dynamically calculate this I feel this needs to be caulcated using measures, rather than using the query editor.

 

I have managed to filter out leavers and future contracts based on the following measure:

   CALCULATE(

      sum(Contracts[Hours Per Week]),

      filter(Contracts, Contracts[End Date] >= min(PeakWeeks[TY]) || ISBLANK(Contracts[End Date])),

      FILTER(Contracts, Contracts[Effective Date] <=min(PeakWeeks[TY]))

   )

 

I know I need to select the max effective date per employee, but I'm struggling to identifiy the correct command to do this,

 

Is anyone able to help please?!

 

Thanks

 

 

 

 

 

 

1 ACCEPTED SOLUTION

@Miss hey do;;t have much time to think about it but try following two measures

 

Effective Hours per Employee = 
VAR __selectedDate = MAX ( 'Calendar'[Date] )
VAR __maxDate = 
CALCULATE ( 
    MAX( 'Table'[Effective Date] ),
    ALLEXCEPT( 'Table', 'Table'[Payroll Number] ), 
    'Table'[End Date] = BLANK() || 'Table'[End Date]>= __selectedDate, 
    'Table'[Effective Date] <= __selectedDate 
)
VAR __payroll = MAX ( 'Table'[Payroll Number] )
RETURN
CALCULATE( 
    SUM ( 'Table'[Hours Per Week] ), 
    'Table'[Effective Date] = __maxDate , 
    'Table'[Payroll Number] = __payroll  
) 

Effective Hours = SUMX(  VALUES ( 'Table'[Payroll Number] ), [Effective Hours per Employee] ) 

 

Output

 

 

image.png

 

Would appreciate Kudos 🙂 if my solution helped.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

4 REPLIES 4
kentyler
Solution Sage
Solution Sage

I hope I've understood your problem correctly.

Here is the measure for calculating the max effective date per employee:

End Date =
VAR current_employee = selectedvalue(EmployeeWork[Payroll Number])
RETURN CALCULATE(MAX(EmployeeWork[Effective Date]),ALL(EmployeeWork),EmployeeWork[Payroll Number] = current_employee)
It uses a VAR to store the Employee payroll number that is in the current filter context.
Then it uses CALCULATE to reset the filter context, first it gets ALL() the rows, then filters them for just the rows for the current employee, then it get the max effective date for that employee.

Using ALL() to restore rows that have been filtered out by the current filter context is a common trick when you need to do a count or sum or average over more rows than are in the current filter context. When a measure is being called for each row in a table only that row is in its filter context. If you take out the ALL() you will get the max date as the same effective date that is in the row.

enddate.png




Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


@Miss hey do;;t have much time to think about it but try following two measures

 

Effective Hours per Employee = 
VAR __selectedDate = MAX ( 'Calendar'[Date] )
VAR __maxDate = 
CALCULATE ( 
    MAX( 'Table'[Effective Date] ),
    ALLEXCEPT( 'Table', 'Table'[Payroll Number] ), 
    'Table'[End Date] = BLANK() || 'Table'[End Date]>= __selectedDate, 
    'Table'[Effective Date] <= __selectedDate 
)
VAR __payroll = MAX ( 'Table'[Payroll Number] )
RETURN
CALCULATE( 
    SUM ( 'Table'[Hours Per Week] ), 
    'Table'[Effective Date] = __maxDate , 
    'Table'[Payroll Number] = __payroll  
) 

Effective Hours = SUMX(  VALUES ( 'Table'[Payroll Number] ), [Effective Hours per Employee] ) 

 

Output

 

 

image.png

 

Would appreciate Kudos 🙂 if my solution helped.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Miss
Frequent Visitor

Thank you! that has worked perfectly! 

 

Bonus is I understand how you've acheived this answer!

kentyler
Solution Sage
Solution Sage

I hope I've understood your problem correctly.

Here is the measure for calculating the max effective date per employee:

End Date =
VAR current_employee = selectedvalue(EmployeeWork[Payroll Number])
RETURN CALCULATE(MAX(EmployeeWork[Effective Date]),ALL(EmployeeWork),EmployeeWork[Payroll Number] = current_employee)
It uses a VAR to store the Employee payroll number that is in the current filter context.
Then it uses CALCULATE to reset the filter context, first it gets ALL() the rows, then filters them for just the rows for the current employee, then it get the max effective date for that employee.

Using ALL() to restore rows that have been filtered out by the current filter context is a common trick when you need to do a count or sum or average over more rows than are in the current filter context. When a measure is being called for each row in a table only that row is in its filter context. If you take out the ALL() you will get the max date as the same effective date that is in the row.
enddate.png




Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow 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.