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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
MichaelBalla
New Member

DATEDIFF by group

I have a dataset as shown below that I would like to calculate the average duration of each type based on the difference between the start and end times of each group.

 

Sample Data

GroupTypeStartTimeEndTime
10001A2/11/21 8:12:032/11/21 8:12:23
10001A2/11/21 8:12:452/11/21 8:14:59
10002B2/11/21 9:15:052/11/21 9:16:20
10002B2/11/21 9:16:252/11/21 9:16:50
10002B2/11/21 9:16:552/11/21 9:17:10
10002B2/11/21 9:17:172/11/21 9:17:46
10003C2/11/21 9:18:032/11/21 9:18:30
10003C2/11/21 9:18:342/11/21 9:18:58
10003C2/11/21 9:19:022/11/21 9:19:58
10003C2/11/21 9:20:062/11/21 9:20:47
10004A2/11/21 9:21:062/11/21 9:21:48
10004A2/11/21 9:21:562/11/21 9:22:10
10004A2/11/21 9:22:132/11/21 9:22:37


The desired output matrix would look something like this:

 

TypeAverage Duration (Seconds)
A133.5
B161
C164


I have been able to make a DAX measure that calculates the difference between the first occurance of A and the last occurance of A, etc., but I have been unable to group the measure. The measure: 

 
PartFabTime = CALCULATE ( DATEDIFF ( MIN (  Log[StartTime] ), MAX ( Log[EndTime]) , HOUR ) )


Anyone have any ideas?

1 ACCEPTED SOLUTION
v-xulin-mstf
Community Support
Community Support

Hi @MichaelBalla

 

Create column as:

Duration = 
CALCULATE(DATEDIFF(MAX('Log'[StartTime]),MAX('Log'[EndTime]),SECOND))

Create a measure as:

PartFabTime = 
AVERAGEX(
    FILTER(
        ALL('Log'),
        'Log'[Type]=MAX('Log'[Type])
    ),
    'Log'[Duration]
)

Here is the output:

vxulinmstf_0-1625124938042.png

 

If you still have some question, please don't hesitate to let me known.‌‌

 

Best Regards,

Link

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!

 

View solution in original post

7 REPLIES 7
v-xulin-mstf
Community Support
Community Support

Hi @MichaelBalla

 

Create column as:

Duration = 
CALCULATE(DATEDIFF(MAX('Log'[StartTime]),MAX('Log'[EndTime]),SECOND))

Create a measure as:

PartFabTime = 
AVERAGEX(
    FILTER(
        ALL('Log'),
        'Log'[Type]=MAX('Log'[Type])
    ),
    'Log'[Duration]
)

Here is the output:

vxulinmstf_0-1625124938042.png

 

If you still have some question, please don't hesitate to let me known.‌‌

 

Best Regards,

Link

 

Is that the answer you're looking for? If this post helps, then please consider Accept it as the solution. Really appreciate!

 

AlexisOlson
Super User
Super User

This is easier if you add a calculated column on the Log table

Seconds = DATEDIFF (  Log[StartTime], Log[EndTime] , SECOND )

 

Then you can write a measure that references that column:

CALCULATE ( AVERAGE ( Log[Seconds] ), ALLEXCEPT ( Log, Log[Type] ) )

 

Otherwise, you need to do that calculation inside an iterator, which isn't particularly efficient computationally.

@AlexisOlson I have tried something similar, however, the calculated column will only calculate the average for each row, and will not account for the gap in time between the rows.

Ah, OK. That does change the requirement since it isn't really an "average" anymore.

How about this?

 

SecondsPerType =
VAR CurrType = VALUES ( Log[Type] )
VAR StartTime = CALCULATE ( MAX ( Log[StartTime] ), ALLSELCTED ( Log ), Log[Type] IN CurrType )
VAR EndTime = CALCULATE ( MAX ( Log[EndTime] ), ALLSELCTED ( Log ), Log[Type] IN CurrType )
RETURN
    DATEDIFF ( StartTime, EndTime, SECOND )

 

So I took your example, which didn't immediately work, and tried modifying it a bit to see if I could get it to work, but I keep getting this output:

Input:

 

SecondsPerType = 
VAR CurrType = SELECTEDVALUE ( Log[Type] )
VAR StartTime = CALCULATE ( MAX( Log[StartTime] ), ALLSELECTED( Log[Type] )) IN CurrType
VAR EndTime =  CALCULATE ( MAX ( Log[EndTime] ), ALLSELECTED( FabLog[Type] )) IN CurrType 
RETURN
    DATEDIFF ( StartTime, EndTime, SECOND )

 

Ouput:

 

Multiple arguements aren't allowed in the ALLSELECTED function when the first argument is a table reference.

 

 Any ideas? For some reason I think it is expecting the CurrType to be a table?

Yes, I wrote it so that CurrType could be multiple values (so that totals would be more likely to work as expected). If you don't care about totals and use SELECTEDVALUE, which gives a single value instead of a list of value(s), then you'd have something like this:

 

SecondsPerType =
VAR CurrType = SELECTEDVALUE ( Log[Type] )
VAR StartTime = CALCULATE ( MAX ( Log[StartTime] ), ALLSELECTED ( Log ), Log[Type] = CurrType )
VAR EndTime = CALCULATE ( MAX ( Log[EndTime] ), ALLSELECTED ( Log ), Log[Type] = CurrType )
RETURN
    DATEDIFF ( StartTime, EndTime, SECOND )

Ah I was just trying different functions to see if I could get it to work. A list of values is what I would be expecting as a result, so I changed it back to VALUES.  It is now returning a value of 0 for every row, and there is no error. Any thoughts?

EDIT:

After some experimenting, it is calculating the difference between the most recent start time and most recent end time, instead of doing it by group.

PBIX file:

https://skyviewglass-my.sharepoint.com/:u:/p/michael/EShyNzYS1qVPhYhotAhceBYBtAR5_9sX6tzx1NkgDzbvPQ?... 

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.