简体   繁体   中英

Power BI - Calculate difference between two date/time values in Hours (> 24 hours) and then Average

I've a Power BI published dataset and using DAX I'm trying to calculate difference(Total Time Taken) between Start datetime and End datetime for each task and then Average Total Time Taken in Hours for all tasks. I've tried few methods and all of them have failed. I don't have the option to edit queries or Transform data as its a published dataset that I've connected to, all i can do is create measures that gives the time difference in Hours even it is over 24 hours and then do the average like in the example below;

在此处输入图像描述

Tasks     Start DateTime         End DateTime        Total Time Taken
1     10/08/2020 10:30:00    11/08/2020 10:30:00        24:00:00
2     10/08/2020 11:30:00    12/08/2020 10:30:00        47:00:00
3     12/08/2020 10:30:00    14/08/2020 10:30:00        48:00:00
4     10/08/2020 10:30:00    16/08/2020 10:30:00        144:00:00
        

Average for all Tasks 65:45:00

So need your guys help in cracking a solution. Thanks in advance.

Create a measure as below-

duration_average = 

VAR total_second = 
AVERAGEX(
    your_table_name,
    DATEDIFF(your_table_name[Start DateTime],your_table_name[End DateTime],SECOND)
)

VAR DAXDay   =  INT(total_second/(24*60*60))
VAR DAXHours =  MOD(INT(total_second/(60*60)),24)
VAR DAXMin   =  MOD(INT(total_second/60),60)
VAR DAXSec   =  MOD(total_second,60)

RETURN DAXDay &" Day "& DAXHours &" Hour "& DAXMin &" Minute "& DAXSec &" Second"

Output will be as below. You can also create your own required formatted output from the raw values.

在此处输入图像描述

You can use below formula to display Total_time_taken for each task:

Total_time_taken = DATEDIFF(CALCULATE(VALUES(Time_Track[Start_Date_Time]), FILTER(Time_Track, Time_Track[Task] = SELECTEDVALUE(Time_Track[Task]))),CALCULATE(VALUES(Time_Track[End_Date_Time]), FILTER(Time_Track, Time_Track[Task] = SELECTEDVALUE(Time_Track[Task]))), HOUR)

在此处输入图像描述

Calculation on Average time_taken is getting little tricky after this so I have used the below formula for calculating the average time. It displays correct value if you want to round you avg to hours but if you want to show minutes it shows 66.75 instead of your 66:45 because I cannot change the datatype of measure to time.

Average_Time_Taken = AVERAGEX(Time_Track, Time_Track[Total_time_taken])

在此处输入图像描述

The below Measure will get you the exact output like seen in your image:

Total Hours = 
var val = SUBSTITUTE(averagex(Sickness,DATEDIFF(Sickness[Start_Date],Sickness[End_Date],HOUR)),".",":")&":00:00"
var ln = LEN(val) - LEN(SUBSTITUTE(val,":",""))
return
IF(ln <= 2,val,LEFT(val,LEN(val)-3))

the Output of the above code is shown below:

在此处输入图像描述

Late for the party but with a one-liner:

FORMAT( DIVIDE( [DurationInSeconds] ) , 86400), "hh:mm:ss" )

It returns desired solution in hours, minutes, and seconds. If Duration is longer than 1 day (86400 sec), it just trims the day part.

https://docs.microsoft.com/en-us/dax/format-function-dax

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