Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
WallyWallWal
Helper I
Helper I

Average of DateTime based on a 10 hour work day

Hello.  I am having issues calculating the average hh:nn it takes to completed a task.  Image below for assistance in trying to explain:

 

I have a start datetime of 01/17/23 16:49:00 (hh:nn:ss) and an Completed DateTime of 01/26/2023 16:05:00 (hh:nn:ss).  I need to take into account an 8-hour work day and exclude weekends.  

WallyWallWal_3-1683238787694.png

 

So if am am looking at this, the time from StartDatetime to CompleteDatetime based on the above example would be 55:16 (hh:mm), with the weekend exclude from this calculation (1/20 & 1/21).  Now lets says I needed to know the average time in hh:nn over the last month that it took one individual to complete each task (assume each row in the StartDatetime column is a task for this example).  How would I go about doing that in a DAX calculation or otherwise?

 

Basing this off the CompletedDatetime, there were 12 task completed. If it took 507.50 hours (507:30 hh:nn), I should get an average of 42.29 hours (42:17 hh:nn).  How can I get this calculation to work in my favor?

 

Below is the calculation I am using to get a string for the total, but can't average out a string.  This is where I need help.  Thanks!

 

Business Hours Time Format =
VAR _WorkStart =
    TIME( 7, 00, 0 )
VAR _WorkFinish =
    TIME( 18, 00, 0 )
VAR _1DayWorkingTime =
    DATEDIFF( _WorkStart, _WorkFinish, HOUR )
VAR _StartDate =
    [StartDate] // Set the start Date column here
VAR _EndDate =
    [InitialTranslationDateCompleted]// Set the End Date column here
VAR _StartDateTime =
    [StartTime] //  Set the start Time column here
VAR _EndDateTime =
    [CompletedTime] //  Set the End Time column here
VAR _DaysBetweenStartFinish =
    ADDCOLUMNS( 'Calendar', "DayN", WEEKDAY( 'Calendar'[Calendar_Date], 2 ) ) // Use the Date table here
VAR _WorkingDaysBetweenStartFinish =
    COUNTX(FILTER(_DaysBetweenStartFinish,
            'Calendar'[Calendar_Date] > _StartDate && 'Calendar'[Calendar_Date] < _EndDate && [DayN] < 6  && 'Calendar'[HOLIDAY_YN]="N"),
           [DayN]) // Sunday and Saturday are weekend days in this calculation //  Set [Holiday = No] to exclude those days from the calculation
VAR _Day1WorkingHour =
    IF(WEEKDAY( _StartDate, 2 ) < 6,
        ( MAX( _WorkFinish, _StartDateTime ) - MAX( _StartDateTime, _WorkStart ) ) * 24,
        0)
VAR _LastDayWorkingHour =
    IF(WEEKDAY( _EndDate, 2 ) < 6,
        ( MIN( _WorkFinish, _EndDateTime ) - MIN( _EndDateTime, _WorkStart ) ) * 24,
        0)
VAR _Duration =
    IF(_StartDate = _EndDate&&WEEKDAY(_StartDate,2)<6,
        MAX(( MIN( _WorkFinish, _EndDateTime ) - MAX( _WorkStart, _StartDateTime ) ) * 24,
            0),
        _WorkingDaysBetweenStartFinish * _1DayWorkingTime + _Day1WorkingHour + _LastDayWorkingHour
    )
VAR _Hour =
    TRUNC( _Duration )
VAR _Minute =
    TRUNC( ( _Duration - _Hour ) * 60 )
RETURN
    _Hour & ":" & _Minute

 

WallyWallWal_4-1683239100689.png

 

 

 

1 REPLY 1
lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...
Please show the expected outcome based on the sample data you provided.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

Check out the June 2024 Power BI update to learn about new features.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.