简体   繁体   中英

Power BI calculating time difference excluding weekends and non working hours

I have the below table and calculating the durations between First_change_Date and Create_date using this DAX formula:

Response_time = 
    VAR Minutes = DATEDIFF('otrs ticket Response'[otrs ticket.create_time], 'otrs ticket Response'[First_Change_time],MINUTE)
    var days =INT(Minutes/1440)
    var hourNo=INT(MOD(Minutes,1440) / 60)
    var minuteNO=MOD(Minutes,60)
RETURN
    CONCATENATE( CONCATENATE( CONCATENATE(days,"d "), CONCATENATE(hourNo, "H ")), CONCATENATE(minuteNO, "m "))

I want to exclude the weekends (Friday, Saturday in my case) and non working hours (5:00pm - 9:00am)

Data:

在此处输入图像描述

I split the date/time in to date and time columns. I then use a date dimension, where one of the columns is "Is Working Day" = TRUE() , based on which day of the week it is (a simple calculated column). In the time dimension, you do the same to identify "Working Hour" = TRUE() , again, a simple calculation.

Once you have the dimensions in place it then becomes very easy to build your calculations to include / exclude.

For my client, I have created a logic. First created a WorkingHoursTable .

在此处输入图像描述

Then created a calculated column with the following formula, in the table which has the start and end dateTime's.

Working Hours Between Dates = 
var startDate = [yourStartDateTime].[Date]
var startTime = [yourStartDateTime] - startDate
var endDate = [yourEndDateTime].[Date]
var endTime = [yourEndDateTime] - endDate

var firstFullDay = startDate + 1
var lastFullDay = endDate - 1

var inBetweenWorkingHours = 
    IF(
        firstFullDay > lastFullDay,
        0,
        SUMX(CALENDAR(firstFullDay, lastFullDay), LOOKUPVALUE(WorkingHoursTable[WorkingHoursInAllDay], WorkingHoursTable[WeekDay], WEEKDAY([Date], 2)))
    )

var firstDayStart = LOOKUPVALUE(WorkingHoursTable[StartTime], WorkingHoursTable[WeekDay], WEEKDAY(startDate, 2))
var firstDayEnd = LOOKUPVALUE(WorkingHoursTable[EndTime], WorkingHoursTable[WeekDay], WEEKDAY(startDate, 2))

var lastDayStart = LOOKUPVALUE(WorkingHoursTable[StartTime], WorkingHoursTable[WeekDay], WEEKDAY(endDate, 2))
var lastDayEnd = LOOKUPVALUE(WorkingHoursTable[EndTime], WorkingHoursTable[WeekDay], WEEKDAY(endDate, 2))

var effectiveStartTime = IF(startTime < firstDayStart, firstDayStart, startTime)
var effectiveEndTime = IF(endTime > lastDayEnd, lastDayEnd, endTime)
        
return 
    IF(
        startDate = endDate,
        24 * IF(effectiveEndTime > effectiveStartTime, effectiveEndTime - effectiveStartTime, 0),
        var firstDayWorkingHour = 
            24 *
            IF(
                startTime > firstDayEnd,
                0,
                firstDayEnd - effectiveStartTime
            )

        var lastDayWorkingHour = 
            24 *
            IF(
                endTime < lastDayStart,
                0,
                effectiveEndTime - lastDayStart
            )
        
        return firstDayWorkingHour + lastDayWorkingHour + inBetweenWorkingHours
    )

In this formula you just set the first 4 variables correctly. Then it will calculate total working hours. Unit will be in hours.

Edit: As I see from your post that your weekends are Friday and Saturday, you will need to use WEEKDAY functions slightly different. You can send 1 as a second parameter to WEEKDAY function, instead of 2. You will also need to modify the WeekDay column of WorkingHoursTable .

After this one, you can parse it to '9d 20H 52m' with your formula.

this helped immsenly with calculation on working hours, is there a way to introduce holiday's

Example Jan 1st or Christmas?

I do have a calendar table with this information but not sure how to add it to the this calc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM