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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
tatmaninov
Frequent Visitor

How to list all direct and indirect linked values

I am working in Excel Power Query and I am trying to take a table that details relationships between two values example below

FromTo
AB
BC
CD
CE
XY
YZ

 

And output this

FromTo
A{B,C,D,E}
X{Y,Z}

 

Would really appreciate a steer or pointers about how I can do this.

 

1 ACCEPTED SOLUTION
ZhangKun
Super User
Super User

let
    dataTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXJSitWJBpI6Ss5gljOQ5QJnuYJZEUBWJJgVCWRFKcXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [From = _t, To = _t]),
    fx = (tbl, s) => 
        let 
            a = List.Combine(List.Transform(s, (v) => Table.SelectRows(tbl, each [From] = v)[To]))
        in 
            if List.Count(a) = 0 then {} else (a & @Fx(tbl, a)), 
    result = Table.AddColumn(
        Table.SelectRows(dataTable, each Table.RowCount(Table.SelectRows(dataTable, (r) => r[To] = [From])) = 0)[[From]], 
        "To", 
        each "{" & Text.Combine(fx(dataTable, {[From]}), ",") & "}"
    )
in
    result

View solution in original post

2 REPLIES 2
tatmaninov
Frequent Visitor

Amazing!, thank you!

ZhangKun
Super User
Super User

let
    dataTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXJSitWJBpI6Ss5gljOQ5QJnuYJZEUBWJJgVCWRFKcXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [From = _t, To = _t]),
    fx = (tbl, s) => 
        let 
            a = List.Combine(List.Transform(s, (v) => Table.SelectRows(tbl, each [From] = v)[To]))
        in 
            if List.Count(a) = 0 then {} else (a & @Fx(tbl, a)), 
    result = Table.AddColumn(
        Table.SelectRows(dataTable, each Table.RowCount(Table.SelectRows(dataTable, (r) => r[To] = [From])) = 0)[[From]], 
        "To", 
        each "{" & Text.Combine(fx(dataTable, {[From]}), ",") & "}"
    )
in
    result

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.