Forum Discussion
hour difference without workhours
- 2 years ago
Hi mina97
I have attached a PBIX with suggested calculated colums in DAX for Work Hours and Work Minutes.
The code is a bit long-winded in order to capture the required logic.
I assumed:
- Weekends/holidays do not need to be excluded. If you do, you would need to adjust and use NETWORKDAYS.
- You want to round down to the nearest integer.
Here is the expression for Work Hours (Work Minutes is similar):
Work Hours = -- Working Start and End time VAR WorkTimeStart = TIME ( 08, 00, 00 ) VAR WorkTimeEnd = TIME ( 17, 10, 10 ) VAR WorkingHours = ( WorkTimeEnd - WorkTimeStart ) -- Start and End date/time on current row VAR StartingDateTime = Data[start date] VAR EndingDateTime = Data[end date] VAR StartingTime= StartingDateTime - TRUNC ( StartingDateTime ) VAR StartingDate = StartingDateTime - StartingTime VAR EndingTime = EndingDateTime - TRUNC ( EndingDateTime ) VAR EndingDate = EndingDateTime - EndingTime -- Adjust start/end times to fall within working hours. VAR StartingTimeEffective = MIN ( MAX ( StartingTime, WorkTimeStart ), WorkTimeEnd ) VAR EndingTimeEffective = MAX ( MIN ( EndingTime, WorkTimeEnd ), WorkTimeStart ) -- Adjust for hours not worked on StartingDate -- StartingTimeOffset will always be <= 0 VAR StartingTimeOffset = WorkTimeStart - StartingTimeEffective -- Adjust for hours not worked on EndingDate -- EndingTimeOffset will always be <= 0 VAR EndingTimeOffset = EndingTimeEffective - WorkTimeEnd VAR DayCount = EndingDate - StartingDate + 1 VAR TotalTimeInDays = DayCount * WorkingHours + StartingTimeOffset + EndingTimeOffset VAR TotalTimeInHours = TotalTimeInDays * 24 VAR TotalTimeInHoursRounded = ROUNDDOWN ( TotalTimeInHours, 0 ) RETURN TotalTimeInHoursRoundedThis is a good article on a similar but different calculation:
https://www.sqlbi.com/blog/alberto/2019/03/25/using-dax-with-datetime-values/
Does this work for you?
Regards
- 2 years ago
That's odd - it's in my post above, but I reattached here in case that helps.
Here is the link created by the forum:
Hi mina97
I have attached a PBIX with suggested calculated colums in DAX for Work Hours and Work Minutes.
The code is a bit long-winded in order to capture the required logic.
I assumed:
- Weekends/holidays do not need to be excluded. If you do, you would need to adjust and use NETWORKDAYS.
- You want to round down to the nearest integer.
Here is the expression for Work Hours (Work Minutes is similar):
Work Hours =
-- Working Start and End time
VAR WorkTimeStart = TIME ( 08, 00, 00 )
VAR WorkTimeEnd = TIME ( 17, 10, 10 )
VAR WorkingHours = ( WorkTimeEnd - WorkTimeStart )
-- Start and End date/time on current row
VAR StartingDateTime = Data[start date]
VAR EndingDateTime = Data[end date]
VAR StartingTime= StartingDateTime - TRUNC ( StartingDateTime )
VAR StartingDate = StartingDateTime - StartingTime
VAR EndingTime = EndingDateTime - TRUNC ( EndingDateTime )
VAR EndingDate = EndingDateTime - EndingTime
-- Adjust start/end times to fall within working hours.
VAR StartingTimeEffective =
MIN (
MAX ( StartingTime, WorkTimeStart ),
WorkTimeEnd
)
VAR EndingTimeEffective =
MAX (
MIN ( EndingTime, WorkTimeEnd ),
WorkTimeStart
)
-- Adjust for hours not worked on StartingDate
-- StartingTimeOffset will always be <= 0
VAR StartingTimeOffset =
WorkTimeStart - StartingTimeEffective
-- Adjust for hours not worked on EndingDate
-- EndingTimeOffset will always be <= 0
VAR EndingTimeOffset =
EndingTimeEffective - WorkTimeEnd
VAR DayCount =
EndingDate - StartingDate + 1
VAR TotalTimeInDays =
DayCount * WorkingHours + StartingTimeOffset + EndingTimeOffset
VAR TotalTimeInHours =
TotalTimeInDays * 24
VAR TotalTimeInHoursRounded =
ROUNDDOWN ( TotalTimeInHours, 0 )
RETURN
TotalTimeInHoursRounded
This is a good article on a similar but different calculation:
https://www.sqlbi.com/blog/alberto/2019/03/25/using-dax-with-datetime-values/
Does this work for you?
Regards
thank you it helped