Forum Discussion
Calculating Working hours
- Anonymous8 years ago
HI rocky09,
You can try to use below calculated column formula to calculate valid working hour:
Work Hour = VAR filtered = FILTER ( ADDCOLUMNS ( CROSSJOIN ( CALENDAR ( [ACTIVITY_DATE], [LASTMODIFIEDDATE] ), SELECTCOLUMNS ( GENERATESERIES ( 9, 18 ), "Hour", [Value] ) ), "Day of week", WEEKDAY ( [Date], 2 ) ), [Day of week] < 6 && [TicketID] = EARLIER ( Table1[TicketID] ) ) VAR hourcount = COUNTROWS ( FILTER ( filtered, ( [Date] >= DATEVALUE ( [ACTIVITY_DATE] ) && [Hour] > HOUR ( [ACTIVITY_DATE] ) + 1 ) && ( [Date] <= DATEVALUE ( [LASTMODIFIEDDATE] ) && [Hour] > HOUR ( [LASTMODIFIEDDATE] ) - 1 ) ) ) VAR remained = DATEDIFF ( TIMEVALUE ( [ACTIVITY_DATE] ), TIME ( HOUR ( [ACTIVITY_DATE] ) + 1, 0, 0 ), MINUTE ) + DATEDIFF ( TIME ( HOUR ( [LASTMODIFIEDDATE] ) - 1, 0, 0 ), TIMEVALUE ( [LASTMODIFIEDDATE] ), MINUTE ) RETURN IF ( hourcount <> BLANK (), (hourcount*60 + remained)/60, 0 )Regards,
Xiaoxin Sheng
Bring some new life to an old thread.
I was staring at this issue with no idea where to start, and then I stumble on this thread which gave me hope.
Unfortunately the examples in this thread gave wildly wrong figures and missed a couple of edge cases that may data was riddled with.
So with massive inspiration from this thread I have put the below together which as far as I can tell is spot on for my 300,000+ data set:
Business Hours =
VAR BusinessStart = 8
VAR BusinessEnd = 18
VAR StartDate = DATEVALUE ( [Start] )
VAR StartHour = HOUR ( [Start] )
VAR EndDate = DATEVALUE ( [End] )
VAR EndHour = HOUR ( [End] )
//Generate a table of Dates and Hours based on the Start and End of item
VAR BusinessCalendar =
FILTER (
ADDCOLUMNS (
CROSSJOIN (
CALENDAR ( StartDate, EndDate ),
SELECTCOLUMNS ( GENERATESERIES ( BusinessStart, BusinessEnd ), "Hour", [Value] )
),
"Day of week", WEEKDAY ( [Date], 2 )
),
[Day of week] < 6
)
//Calculate the number of hours on the first day
//Also check if the start and end dates are the same and account for that, will drop lastDayHours in final calc
VAR firstDayHours =
IF ( StartDate <> EndDate,
COUNTROWS (
FILTER (
BusinessCalendar,
([Date] = StartDate && [HOUR] > StartHour )
)
),
COUNTROWS (
FILTER (
BusinessCalendar,
([Date] = StartDate && [HOUR] > StartHour && [HOUR] < EndHour )
)
)
)
//Calculate hours in last day
VAR lastDayHours =
COUNTROWS (
FILTER (
BusinessCalendar,
([Date] = EndDate && [HOUR] < EndHour )
)
)
//Add first and last hours together unless both same day
VAR combinedHours =
IF ( StartDate = EndDate,
firstDayHours,
firstDayHours + lastDayHours
)
//Calculate the hours from the full days
VAR fullDays =
COUNTROWS (
FILTER (
BusinessCalendar,
([Date] > StartDate && [Date] < EndDate)
)
)
//Calculate the minutes in the hours at each end of the date/time period
//Check if start and end hours are the same and account for that
VAR remainingMinutes =
IF ( StartDate <> EndDate,
(60 - MINUTE ( [Start] )) + MINUTE ( [End] ),
IF ( StartHour <> EndHour,
(60 - MINUTE ( [Start] )) + MINUTE ( [End] ),
MINUTE ( [End] ) - MINUTE ( [Start] )
)
)
RETURN
( ( (combinedHours + fullDays ) * 60 ) + remainingMinutes ) / 60NOTE - This does not account for End dates that are after the Start date, these will be calculated as negative values, this was not an issue for my data set but this could be handled with the MIN function to clamp to 0
This is great! How would I go about excluding holidays as well?