March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
I have what I believe is a fairly common scenario related to timesheets that I've been breaking my head on.
Scenario: Each employee is available for X numbers of hours on a weekly basis. Typically it is 40 hrs per week but could be different. It would also be lesser if say an employee exits or joins the company in the middle of the week. This data is stored in a table.
Our reporting is at a Monthly (calendar month) level so I need a way to calculate the Available Hours by month for a given employee. Ideally, would like it to be flexible so it can calculate Available Hours for any period chosen i.e. One could choose 2 months or 6 weeks or 24 days.. basically any custom start and end date and the measure should be able to tell exactly how many hours the employee was available during that period.
I don't necessarily want to extrapolate and store the data if there is a way to achieve what I really need using just a DAX formula i.e. a measure that just takes the weekly data and is able to provide the available hours for any selected period.
What's the best solution?
Green box represents the data I have and Blue box represents the extrapolated data.
Solved! Go to Solution.
HI @vickyd,
You can refer to below sample to generate the analysis daily work hour table.
1. Merge targe tables.
Merged = UNION(Table1,Table2)
2. Generate the calendar table with id and target.
Result = VAR startDate = MINX ( Merged, [End Date] ) + ( 1 - WEEKDAY ( MINX ( Merged, [End Date] ), 1 ) ) VAR endDate = MAXX ( Merged, [End Date] ) RETURN ADDCOLUMNS ( CROSSJOIN ( CALENDAR ( startDate, endDate ), DISTINCT ( SELECTCOLUMNS ( Merged, "ID", [ID] ) ) ), "Target", LOOKUPVALUE ( Merged[Work Hour], Merged[ID], [ID], Merged[End Date], [Date] + ( 7 - WEEKDAY ( [Date], 1 ) ) ) + 0, "Day of Week", FORMAT ( [Date], "ddd" ), "Month", FORMAT ( [Date], "mmm" ), "Work Hour", IF ( WEEKDAY ( [Date], 1 ) <> 1 && WEEKDAY ( [Date], 1 ) <> 7, 8, 0 ) )
3. Add calculated column to calculate the available work hour.
Available Hours = VAR isWorkDay = IF ( WEEKDAY ( [Date], 1 ) <> 1 && WEEKDAY ( [Date], 1 ) <> 7, TRUE (), FALSE () ) VAR rolling = SUMX ( FILTER ( ALL ( Result ), [ID] = EARLIER ( [ID] ) && WEEKNUM ( [Date], 1 ) = WEEKNUM ( EARLIER ( Result[Date] ), 1 ) && [Date] < EARLIER ( [Date] ) ), [Work Hour] ) RETURN IF ( [Target] = 0, 0, IF ( rolling < [Target] && rolling + 8 > [Target], [Target] - rolling, IF ( rolling < [Target] && isWorkDay, 8 ) + 0 ) )
Regards,
Xiaoxin Sheng
HI @vickyd,
You can refer to below sample to generate the analysis daily work hour table.
1. Merge targe tables.
Merged = UNION(Table1,Table2)
2. Generate the calendar table with id and target.
Result = VAR startDate = MINX ( Merged, [End Date] ) + ( 1 - WEEKDAY ( MINX ( Merged, [End Date] ), 1 ) ) VAR endDate = MAXX ( Merged, [End Date] ) RETURN ADDCOLUMNS ( CROSSJOIN ( CALENDAR ( startDate, endDate ), DISTINCT ( SELECTCOLUMNS ( Merged, "ID", [ID] ) ) ), "Target", LOOKUPVALUE ( Merged[Work Hour], Merged[ID], [ID], Merged[End Date], [Date] + ( 7 - WEEKDAY ( [Date], 1 ) ) ) + 0, "Day of Week", FORMAT ( [Date], "ddd" ), "Month", FORMAT ( [Date], "mmm" ), "Work Hour", IF ( WEEKDAY ( [Date], 1 ) <> 1 && WEEKDAY ( [Date], 1 ) <> 7, 8, 0 ) )
3. Add calculated column to calculate the available work hour.
Available Hours = VAR isWorkDay = IF ( WEEKDAY ( [Date], 1 ) <> 1 && WEEKDAY ( [Date], 1 ) <> 7, TRUE (), FALSE () ) VAR rolling = SUMX ( FILTER ( ALL ( Result ), [ID] = EARLIER ( [ID] ) && WEEKNUM ( [Date], 1 ) = WEEKNUM ( EARLIER ( Result[Date] ), 1 ) && [Date] < EARLIER ( [Date] ) ), [Work Hour] ) RETURN IF ( [Target] = 0, 0, IF ( rolling < [Target] && rolling + 8 > [Target], [Target] - rolling, IF ( rolling < [Target] && isWorkDay, 8 ) + 0 ) )
Regards,
Xiaoxin Sheng
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
123 | |
85 | |
85 | |
70 | |
51 |
User | Count |
---|---|
205 | |
153 | |
97 | |
79 | |
69 |