Forum Discussion
Calculate Total Hours (excluding weekends) but include the start and end of the working week.
Hi quincy_p - you can add your DAX with some additional logic to handle these situation.
Weekday_Hours =
VAR StartDate = 'Work Order ALL'[SVMXC__CreatedDate__c]
VAR EndDate = 'Work Order ALL'[SVMXC__Actual_Initial_Response__c]
-- Ensure dates are not blank
VAR ValidDates = NOT(ISBLANK(StartDate) || ISBLANK(EndDate))
-- Calculate the total hours between the start and end dates
VAR TotalHours = DATEDIFF(StartDate, EndDate, HOUR)
-- Function to calculate work hours within a single day
VAR WorkDayHours =
SWITCH(TRUE(),
WEEKDAY(StartDate, 2) = 6 && HOUR(StartDate) >= 18, 0,
WEEKDAY(StartDate, 2) = 7, 0,
WEEKDAY(EndDate, 2) = 1 && HOUR(EndDate) < 8, 0,
TRUE,
MAX(0, MIN(18, HOUR(EndDate)) - MAX(8, HOUR(StartDate))) +
(MINUTE(EndDate) - MINUTE(StartDate)) / 60
)
-- Calculate the number of full work days excluding weekends
VAR FullWorkDays =
COUNTROWS(
FILTER(
ADDCOLUMNS(
GENERATESERIES(0, DATEDIFF(StartDate, EndDate, DAY) - 1, 1),
"Date", StartDate + [Value]
),
WEEKDAY([Date], 2) < 6
)
)
-- Calculate the total work hours excluding weekends and specific boundaries
VAR TotalWorkHours = FullWorkDays * 8 + WorkDayHours
-- Ensure no negative values
VAR Result = IF(TotalWorkHours < 0, 0, TotalWorkHours)
RETURN
IF(ValidDates, Result, BLANK())
This solution should give you the correct total work hours between two dates, excluding weekends and considering the specific time boundaries.
Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!
Hi rajendraongole1
Thanks for your quick response!
One thing however - this is calculating only working hours it seems? I still want all 24 hours within the week I just want to set the start of that week.
So include all hours between Monday 8 AM and Friday 6 PM.
Below is the result but this should be 19 hrs between not 8.
Does that make sense?
Thanks!