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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
MSAYED26
Helper II
Helper II

Sum

I want to calculate total travel time based on line and station filter

Example : Departure station (A1) Arrival, station(A3) and line red so travel time =24 min 

How make this in power bi  

LineDeparture stationArrival stationTravel Time (min)
RedA1A212
RedA2A312
RedA3A412
RedA4A512
RedA5A612
RedA6A712
RedA7A812
RedA8A912
RedA9A1012
RedA10S112
RedS1S212
GreenA1A28
GreenA2A38
GreenA3A48
GreenA4A58
GreenA5A68
GreenA6A78
GreenA7A88
GreenA8A98
GreenA9A108
GreenA10F18
GreenF1F28
BlueA1A27
BlueA2A37
BlueA3A47
BlueA4A57
BlueA5A67
BlueA6A77
BlueA7A87
BlueA8A97
BlueA9A107
PurpleA1A27
PurpleA2A37
PurpleA3A47
PurpleA4A57
PurpleA5A67
PurpleA6A77
PurpleA7A87
PurpleA8A97
PurpleA9A107
1 ACCEPTED SOLUTION

@MSAYED26 OK, try this one. Updated PBIX attached.

 

Travel Time Measure = 
    VAR __Departure = MAX([Departure station])
    VAR __Arrival = MAX([Arrival station])
    VAR __Line = MAX([Line])
    VAR __DepartIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Departure station] = __Departure ), [Index] )
    VAR __ArrivalIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Arrival station] = __Arrival ), [Index] )
    VAR __ArrivalIndexReverse = MAXX( FILTER( 'Table', [Line] = __Line && [Departure station] = __Arrival ), [Index] )
    VAR __Result = 
        SWITCH( __Line,
            "Blue", SUMX( FILTER( 'Table', [Index] >= __DepartIndex && [Index] <= __ArrivalIndex ), [Travel Time (min)] ),
            SUMX( FILTER( 'Table', [Index] < __DepartIndex && [Index] >= __ArrivalIndexReverse ), [Travel Time (min)] )
        )
RETURN
    __Result

Also, I don't think that your data posted is correct or something is weird. Red S1 goes to S2 to A4, to A3, A2, A1, A10 so I don't see how S1 to A10 is 12 minutes.

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

9 REPLIES 9
Ashish_Mathur
Super User
Super User

Hi,

Cannot make any sense of the filter conditions itself.  For departure station A1, there is no Arrival station A3 at all.  Please clarify/review carefully.

Ashish_Mathur_0-1715040214182.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Greg_Deckler
Super User
Super User

@MSAYED26 Looks like a Transitive Closure problem: Transitive Closure - Microsoft Fabric Community


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

But i  want make filter with  Departure station and filter with Arrival station then arrival time calculated

@MSAYED26 See what you think of this. PBIX is attached below signature. First, I added an Index in Power Query. Then I think after that you just need the calculated column "Travel Time" that you can easily turn into a measure like so:

Travel Time = 
    VAR __Departure = MAX([Departure station])
    VAR __Arrival = MAX([Arrival station])
    VAR __Line = MAX([Line])
    VAR __DepartIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Departure station] = __Departure ), [Index] )
    VAR __ArrivalIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Arrival station] = __Arrival ), [Index] )
    VAR __Result = SUMX( FILTER( 'Table', [Index] >= __DepartIndex && [Index] <= __ArrivalIndex ), [Travel Time (min)] )
RETURN
    __Result

 

In my case, I created a calculated table like this:

Pairs = 
    VAR __Red = FILTER( 'Table', [Line] = "Red" )
    VAR __RedPairs = 
        ADDCOLUMNS(
            GENERATE(
                SELECTCOLUMNS( __Red, "Line", [Line], "Departure station", [Departure station], "Index", [Index]),
                SELECTCOLUMNS( __Red, "Arrival station", [Departure station], "Index1", [Index] )
            ),
            "__Keep", IF( [Index] > [Index1] || ( [Departure station] = [Arrival station] && [Departure station] <> "A1"), 0, 1 )
        )
    VAR __Blue = FILTER( 'Table', [Line] = "Blue" )
    VAR __BluePairs = 
        ADDCOLUMNS(
            GENERATE(
                SELECTCOLUMNS( __Blue, "Line", [Line], "Departure station", [Departure station], "Index", [Index]),
                SELECTCOLUMNS( __Blue, "Arrival station", [Departure station], "Index1", [Index] )
            ),
            "__Keep", IF( [Index] > [Index1] || ( [Departure station] = [Arrival station] && [Departure station] <> "A1"), 0, 1 )
        )
    VAR __Green = FILTER( 'Table', [Line] = "Blue" )
    VAR __GreenPairs = 
        ADDCOLUMNS(
            GENERATE(
                SELECTCOLUMNS( __Green, "Line", [Line], "Departure station", [Departure station], "Index", [Index]),
                SELECTCOLUMNS( __Green, "Arrival station", [Departure station], "Index1", [Index] )
            ),
            "__Keep", IF( [Index] > [Index1] || ( [Departure station] = [Arrival station] && [Departure station] <> "A1"), 0, 1 )
        )
    VAR __Purple = FILTER( 'Table', [Line] = "Blue" )
    VAR __PurplePairs = 
        ADDCOLUMNS(
            GENERATE(
                SELECTCOLUMNS( __Purple, "Line", [Line], "Departure station", [Departure station], "Index", [Index]),
                SELECTCOLUMNS( __Purple, "Arrival station", [Departure station], "Index1", [Index] )
            ),
            "__Keep", IF( [Index] > [Index1] || ( [Departure station] = [Arrival station] && [Departure station] <> "A1"), 0, 1 )
        )
    VAR __Table = UNION( __RedPairs, __BluePairs, __GreenPairs, __PurplePairs ) 
    VAR __Result = SELECTCOLUMNS( FILTER( __Table, [__Keep] = 1 ), "Line", [Line], "Departure station", [Departure station], "Arrival station", [Arrival station] )
RETURN
    __Result

And then a calculated column in that table like this:

Travel Time = 
    VAR __Departure = [Departure station]
    VAR __Arrival = [Arrival station]
    VAR __Line = [Line]
    VAR __DepartIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Departure station] = __Departure ), [Index] )
    VAR __ArrivalIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Arrival station] = __Arrival ), [Index] )
    VAR __Result = SUMX( FILTER( 'Table', [Index] >= __DepartIndex && [Index] <= __ArrivalIndex ), [Travel Time (min)] )
RETURN
    __Result

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks alot it work but need something more , In the photo below the direction of blue from A1 to A2 so the calculation right, but other line the direction is The opposite, Can you help me .

MSAYED26_1-1714934985233.png

 

@MSAYED26 I haven't tested this but I *think* you just need to reverse your comparison depending on the Line:

Travel Time = 
    VAR __Departure = MAX([Departure station])
    VAR __Arrival = MAX([Arrival station])
    VAR __Line = MAX([Line])
    VAR __DepartIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Departure station] = __Departure ), [Index] )
    VAR __ArrivalIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Arrival station] = __Arrival ), [Index] )
    VAR __Result = 
        SWITCH( __Line,
            "Blue", SUMX( FILTER( 'Table', [Index] >= __DepartIndex && [Index] <= __ArrivalIndex ), [Travel Time (min)] ),
            SUMX( FILTER( 'Table', [Index] <= __DepartIndex && [Index] >= __ArrivalIndex ), [Travel Time (min)] )
        )
RETURN
    __Result

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

All travel time give me blank

MSAYED26_0-1714946140073.png

 

@MSAYED26 OK, try this one. Updated PBIX attached.

 

Travel Time Measure = 
    VAR __Departure = MAX([Departure station])
    VAR __Arrival = MAX([Arrival station])
    VAR __Line = MAX([Line])
    VAR __DepartIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Departure station] = __Departure ), [Index] )
    VAR __ArrivalIndex = MAXX( FILTER( 'Table', [Line] = __Line && [Arrival station] = __Arrival ), [Index] )
    VAR __ArrivalIndexReverse = MAXX( FILTER( 'Table', [Line] = __Line && [Departure station] = __Arrival ), [Index] )
    VAR __Result = 
        SWITCH( __Line,
            "Blue", SUMX( FILTER( 'Table', [Index] >= __DepartIndex && [Index] <= __ArrivalIndex ), [Travel Time (min)] ),
            SUMX( FILTER( 'Table', [Index] < __DepartIndex && [Index] >= __ArrivalIndexReverse ), [Travel Time (min)] )
        )
RETURN
    __Result

Also, I don't think that your data posted is correct or something is weird. Red S1 goes to S2 to A4, to A3, A2, A1, A10 so I don't see how S1 to A10 is 12 minutes.

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.