cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Advocate I
Advocate I

Rank Order Network

Hi all,

I'm trying to find a way to rank order a network.  I have two columns with "start" and "end" destinations of each segment of the network.  In the case of this problem, there will always be a last destination, though their may be multiple paths into the network.  I've included a simple example.


To do this in C++ or Matlab I would use a For Loop and something like an insertion sort algorithm to build the list where the prior row's "end" is equal to current rows "start" then apply the rank order to the sorted list by group.  I know in principle that I would perform this in M by wrapping it in a Group function, but beyond that I'm uncertain how to compare different columns in different row in this way.

In the event of ties (where two nodes feed into one) which is given each rank is not important.

 

Simple Data Example

 

Group

Start

End

1

A

C

1

C

D

1

B

C

1

E

F

1

D

E

2

K

L

2

M

O

2

L

M

2

N

O

2

O

P

 

 

Network diagramNetwork diagram

 

Desired Results

 

Order Number

Group

Start

End

1

1

A

C

3

1

C

D

2

1

B

C

5

1

E

F

4

1

D

E

1

2

K

L

3

2

M

O

2

2

L

M

4

2

N

O

5

2

O

P



Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Support
Community Support

Re: Rank Order Network

Hi @wallace13 ,

You can use the following iterate function to add a custom column to get the detail relationship mapping(notice: you need to enter to query editor to do these operations), then you can use PATHLENGTH to ranking records.

Iterate function:

let
    findPath=(tb as table,para as text, optional path as text)=> 
    let 
        source= Table.SelectRows(tb,each [Start]=para),
        result= 
        if Table.RowCount(source)>0 
        then @findPath(tb, source[End]{0},if Text.Length(path) <>0 then path&"|"&source[End]{0} else para&"|"&source[End]{0} ) 
        else path
    in
        result
in
    findPath

Full query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYmelWB0IzxmIXeA8JxQ5VyB2g/NcwCIgnhGQ5Q3EPnCeLxD7w3k+YBEYzw9Fzh+IA5RiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Group = _t, Start = _t, End = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", Int64.Type}, {"Start", type text}, {"End", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Path", each @findPath(Table.SelectRows(#"Changed Type", each [Group]=_[Group]),[Start],""))
in
    #"Added Custom"

14.png

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Highlighted
Super User IV
Super User IV

Re: Rank Order Network

Refer -https://docs.microsoft.com/en-us/dax/parent-and-child-functions-dax

check if pathlength can help

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks.

My Recent Blog - https://community.powerbi.com/t5/Community-Blog/Comparing-Data-Across-Date-Ranges/ba-p/823601



Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
My Recent Blog -Week is not so Weak Connect on Linkedin

Proud to be a Super User!

Highlighted
Super User III
Super User III

Re: Rank Order Network

I had a similar idea to @amitchandak , but I think the networks are becoming more complex than a parent child path.

 

Probably the goal can only be achieved with an R or Python transformation in Power Query.

 

If I answered your question, please mark my post as solution, this will also help others.

Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

I work at KUMAVISION AG , one of the world's largest
implementation partners for Microsoft Dynamics. #BetterTogether

Highlighted
Community Support
Community Support

Re: Rank Order Network

Hi @wallace13 ,

You can use the following iterate function to add a custom column to get the detail relationship mapping(notice: you need to enter to query editor to do these operations), then you can use PATHLENGTH to ranking records.

Iterate function:

let
    findPath=(tb as table,para as text, optional path as text)=> 
    let 
        source= Table.SelectRows(tb,each [Start]=para),
        result= 
        if Table.RowCount(source)>0 
        then @findPath(tb, source[End]{0},if Text.Length(path) <>0 then path&"|"&source[End]{0} else para&"|"&source[End]{0} ) 
        else path
    in
        result
in
    findPath

Full query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYmelWB0IzxmIXeA8JxQ5VyB2g/NcwCIgnhGQ5Q3EPnCeLxD7w3k+YBEYzw9Fzh+IA5RiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Group = _t, Start = _t, End = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", Int64.Type}, {"Start", type text}, {"End", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Path", each @findPath(Table.SelectRows(#"Changed Type", each [Group]=_[Group]),[Start],""))
in
    #"Added Custom"

14.png

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Highlighted
Advocate I
Advocate I

Re: Rank Order Network

Thanks!  I made some minor adjustments to convert the concatenation of stations to a counter (adding 1 for each step).  This saves the need to count the length of the path.  The concatenation of the stations is a great approach to check that it is working correctly though!

Helpful resources

Announcements
Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Using the Community

Using the Community

Need help with the Power BI Community? Our 'Using the Community' support articles are a great place to start.

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Power Platform 2020 release wave 2 plan

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors