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
Anonymous
Not applicable

Measure to get sum of allocated hours between two dates

Hi, 

 

I have three tables Users, Allocations and Calendar. Most users are allocated 7.5 hours as standard (Monday to Friday), and there can be some users with different hours, and such users have entries in the Allocations table (see below). Day 1 is Monday, Day 2 - Tuesday and so on.

Relationship -> Users 1:1 Allocations

 

Users:

User Id
101
102
103
104

 

Allocations

User IdDay 1Day 2Day 3Day 4Day 5
1027.53.507.54
1047.57.57.530

 

Calendar:

Date

Day of Week

Weekday name

 

I am trying to create a measure to calculate the sum of hours allocated to all users between two dates.

For example, if user_id 101 and 103 have a standard allocation of 7.5 hours on all 5 days, then the Sum of Allocated Hours between Oct 1, 2020 and Oct 9, 2020 for user_ids 101, 102, 103 and 104 would be 167.5. 

Number of weekdays between Oct 1, 2020 and Oct 9, 2020 = 7

Total hours for

User_id 101 = 7.5 * 7 = 52.5

user_id 102 = 7.5 + 4 + 7.5 + 3.5 + 0 + 7.5 + 4 = 34

User_id 103 = 7.5 * 7 = 52.5

User_id 104 = 3 + 0+ 7.5 + 7.5 + 7.5+ 3 + 0 = 28.5

 

Please can anyone advise how to create this measure. Greatly appreciated. Thanks!

1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @Anonymous ,

 

I use unpivot and create a calculated table and a measure.

 

Dummy Power BI model here.

 

1.Select these columns and click Unpivot Columns.

3.png4.png

 

2.Create the Calendar table.

Calendar =
ADDCOLUMNS (
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2020, 10, 1 ), DATE ( 2020, 10, 9 ) ),
        "Day of Week", WEEKDAY ( [Date], 2 )
    ),
    "Weekday Name", "Day " & [Day of Week]
)

5.png

 

3.Create a new table using DAX.\

Table =
FILTER ( CROSSJOIN ( 'Allocations', 'Calendar' ), [Attribute] = [Weekday Name] )

6.png

 

4.Create the measure to calculate the sum of hours.

Hours =
IF (
    MAX ( 'Users'[User Id] ) IN VALUES ( 'Table'[User Id] ),
    SUM ( 'Table'[Value] ),
    CALCULATE (
        COUNT ( 'Calendar'[Day of Week] ),
        FILTER ( 'Calendar', [Day of Week] IN { 1, 2, 3, 4, 5 } )
    ) * 7.5
)

7.png

 

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-stephen-msft
Community Support
Community Support

Hi @Anonymous ,

 

I use unpivot and create a calculated table and a measure.

 

Dummy Power BI model here.

 

1.Select these columns and click Unpivot Columns.

3.png4.png

 

2.Create the Calendar table.

Calendar =
ADDCOLUMNS (
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2020, 10, 1 ), DATE ( 2020, 10, 9 ) ),
        "Day of Week", WEEKDAY ( [Date], 2 )
    ),
    "Weekday Name", "Day " & [Day of Week]
)

5.png

 

3.Create a new table using DAX.\

Table =
FILTER ( CROSSJOIN ( 'Allocations', 'Calendar' ), [Attribute] = [Weekday Name] )

6.png

 

4.Create the measure to calculate the sum of hours.

Hours =
IF (
    MAX ( 'Users'[User Id] ) IN VALUES ( 'Table'[User Id] ),
    SUM ( 'Table'[Value] ),
    CALCULATE (
        COUNT ( 'Calendar'[Day of Week] ),
        FILTER ( 'Calendar', [Day of Week] IN { 1, 2, 3, 4, 5 } )
    ) * 7.5
)

7.png

 

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

nvprasad
Solution Sage
Solution Sage

Hi ,

 

The best solution is, Do the unpivot transformation of the "Allocation" table and create a measure.

 

Appreciate a Kudos! 🙂
If this helps and resolves the issue, please mark it as a Solution! 🙂

Regards,
N V Durga Prasad

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.