Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

What Power Query for combinations between Excel tables?

Although it's pretty straightforward in SQL, I can't get the Power Query M code required to get all combinations between association tables in Excel. Here are my (fictitious) tables and data:

Color:

ID Name
#FF0000 Red
#00FF00 Green
#0000FF Blue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Generally, Power Query works on the idea of joining just two tables at a time so you need to take a series of steps.

Using just the GUI to merge and expand columns, you can get the final result with M code that looks like this:

let
    Source = ColorInObject,
    #"Merged Queries" = Table.NestedJoin(Source, {"ColorID"}, ColorInPaint, {"ColorID"}, "ColorInPaint", JoinKind.LeftOuter),
    #"Expanded ColorInPaint" = Table.ExpandTableColumn(#"Merged Queries", "ColorInPaint", {"PaintID"}, {"PaintID"}),
    #"Merged Queries1" = Table.NestedJoin(#"Expanded ColorInPaint", {"ObjectID"}, Object, {"ID"}, "Object", JoinKind.LeftOuter),
    #"Expanded Object" = Table.ExpandTableColumn(#"Merged Queries1", "Object", {"Name"}, {"Object.Name"}),
    #"Merged Queries2" = Table.NestedJoin(#"Expanded Object", {"ColorID"}, Color, {"ID"}, "Color", JoinKind.LeftOuter),
    #"Expanded Color" = Table.ExpandTableColumn(#"Merged Queries2", "Color", {"Name"}, {"Color.Name"}),
    #"Merged Queries3" = Table.NestedJoin(#"Expanded Color", {"PaintID"}, Paint, {"ID"}, "Paint", JoinKind.LeftOuter),
    #"Expanded Paint" = Table.ExpandTableColumn(#"Merged Queries3", "Paint", {"Name"}, {"Paint.Name"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Paint",{"Object.Name", "Color.Name", "Paint.Name"})
in
    #"Removed Other Columns"

You can cut down on some of the verbosity if you tinker with the M by hand:

let
    JoinTables =
        Table.NestedJoin(
            Table.NestedJoin(
                Table.NestedJoin(
                    Table.Join(
                        ColorInPaint, "ColorID",
                        ColorInObject, "ColorID"
                    ), "ObjectID",
                    Object, "ID", "Object"
                ), "PaintID",
                Paint, "ID", "Paint"
            ), "ColorID",
            Color, "ID", "Color"
        ),
    Expand =
        Table.ExpandTableColumn(
            Table.ExpandTableColumn(
                Table.ExpandTableColumn(
                    JoinTables, "Object", {"Name"}, {"Object.Name"}
                ), "Paint", {"Name"}, {"Paint.Name"}
            ), "Color", {"Name"}, {"Color.Name"}
        )
in
    Table.SelectColumns(Expand,{"Object.Name", "Paint.Name", "Color.Name"})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...