cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
zhivana
Resolver II
Resolver II

Returning SUM of MAX duration for bus trips to calculate total duration

Note - I want to do this in a measure, rather than a calculated column.

 

My data set comprises stop level data for bus routes, with timings, as well as a calculated column that calculates total elapsed duration. E.g. the following, where the result should be 840:

 

UniqueTripIDStopTimeRunningTimeDuration (calc column)
A17:00:00am0
A27:02:00am120
A37:04:00am240
A47:06:00am360
B18:00:00am0
B28:03:00am180
B38:05:00am300
B48:08:00am480

 

The total time for a trip is the MAX value for Calculated Duration for any given UniqueTripID (these are unique identifiers )

 

I've come up with a measure that works for individual level routes:

 

var maxtime = MAX(DATA_RADIOLA[RunningTripDuration])

var uniquetripid = MAX(DATA_RADIOLA[Unique TRIPID])

var routeresult =
CALCULATE(
MAX(
DATA_RADIOLA[RunningTripDuration]),
uniquetripid=DATA_RADIOLA[Unique TRIPID]
)

What I want to do is SUM the MAX for every unique TripID. The following does not work:
//CALCULATE(
// SUM(
// DATA_RADIOLA[RunningTripDuration]),
// DATA_RADIOLA[RunningTripDuration]=maxtime,
// DATA_RADIOLA[Unique TRIPID]=uniquetripid
//)
1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

@zhivana lbendlin_0-1639229750628.png

Max Cumul := 
var a = SUMMARIZE('DATA_RADIOLA','DATA_RADIOLA'[UniqueTripID],"maxlen",MAX('DATA_RADIOLA'[RunningTimeDuration (calc column)]))
return sumx(a,[maxlen])

 

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

@zhivana lbendlin_0-1639229750628.png

Max Cumul := 
var a = SUMMARIZE('DATA_RADIOLA','DATA_RADIOLA'[UniqueTripID],"maxlen",MAX('DATA_RADIOLA'[RunningTimeDuration (calc column)]))
return sumx(a,[maxlen])

 

Wonderful, works perfectly. Here is your result compared to one I created using a helper column (which slows down calculation time), and the difference is infinitesimal.
Thanks so much!!!

zhivana_0-1639339349968.png

 

you make a good point, especially on the impact of larger data sets on the performance. Instead of SUMMARIZE you can also use GROUPBY() or a variety of other approaches. In DAX Studio you can study the query plan and optimize your query as needed.

Helpful resources

Announcements
Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through May 31st!

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Power BI Release May 2022 768x460.png

Check it out!

Click here to read more about the May 2022 updates!