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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
mjohannesson
Advocate I
Advocate I

Average difference of column values between two subsets of same table

We have a report where you can select an origin and destination stop to calculate the average travel time between those stops using the measure below. Is there any way to speed up the measure? WkOperatingDayDateTechnicalLineNumber, JourneyGid and VehicleIdentity uniquely identifies a journey and SequenceNumber is the ordinal number of a stop on the journey route.

 

AvgActualTravelDuration(s) = 
VAR originStop = SELECTEDVALUE(OriginStopArea[StopAreaGid])
VAR destinationStop = SELECTEDVALUE(DestinationStopArea[StopAreaGid])
VAR originTable =
    SELECTCOLUMNS(
        CALCULATETABLE(
            F_TravelStop,
            D_StopArea[StopAreaGid]=originStop
        ),
        "WkOperatingDayDate", [WkOperatingDayDate], 
        "TechnicalLineNumber", RELATED(D_Line[TechnicalLineNumber]),
        "JourneyGid", [JourneyGid],
        "VehicleIdentity", [VehicleIdentity],
        "OriginSequenceNumber", [SequenceNumber], 
        "OriginActualTravelDurationRunningTotal", [ActualTravelDurationRunningTotal]
    )        
VAR destinationTable =
    SELECTCOLUMNS(       
            CALCULATETABLE(
                F_TravelStop,
                D_StopArea[StopAreaGid]=destinationStop
            ),
        "WkOperatingDayDate", [WkOperatingDayDate], 
        "TechnicalLineNumber", RELATED(D_Line[TechnicalLineNumber]),
        "JourneyGid", [JourneyGid],
        "VehicleIdentity", [VehicleIdentity],
        "DestinationSequenceNumber", [SequenceNumber], 
        "DestinationActualTravelDurationRunningTotal", [ActualTravelDurationRunningTotal]
    )    
VAR joinedTable =
    FILTER(
        NATURALINNERJOIN(
               originTable,
            destinationTable
        ),
        [OriginSequenceNumber] < [DestinationSequenceNumber]
     )
RETURN
AVERAGEX(
    joinedTable,
    ([DestinationActualTravelDurationRunningTotal] - [OriginActualTravelDurationRunningTotal]))

1 REPLY 1
kentyler
Solution Sage
Solution Sage

The first thing you should do is to download Dax Studio, an invaluable tool that will give you detailed feedback on how your measure executes https://daxstudio.org/

The next thing is to watch https://www.sqlbi.com/tv/my-power-bi-report-is-slow-what-should-i-do/....which will give you some good hints about possible improvements.

I'm a personal Power Bi Trainer I learn something every time I answer a question

The Golden Rules for Power BI

  1. Use a Calendar table. A custom Date tables is preferable to using the automatic date/time handling capabilities of Power BI. https://www.youtube.com/watch?v=FxiAYGbCfAQ
  2. Build your data model as a Star Schema. Creating a star schema in Power BI is the best practice to improve performance and more importantly, to ensure accurate results! https://www.youtube.com/watch?v=1Kilya6aUQw
  3. Use a small set up sample data when developing. When building your measures and calculated columns always use a small amount of sample data so that it will be easier to confirm that you are getting the right numbers.
  4. Store all your intermediate calculations in VARs when you’re writing measures. You can return these intermediate VARs instead of your final result  to check on your steps along the way.




Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


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.

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.