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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
smann
Helper I
Helper I

Can I join tables from a column of tables based on a different column?

Sorry for the titlegore! Here's the situation:

 

I want to start with something like this:

TableTypeTableColumn
1Table
1Table
2Table
2Table
2Table
3Table
3Table

 

And my desired result is this:

TableTypeJoinedTableColumn
1Table
2Table
3Table

 

...Where the final tables are joined 1+1, 2+2+2, 3+3.

 

My instinct says iterate through the list, if [TableType]{Counter} = [TableType]{Counter-1} then join Table to previous Table, else Table. The problem is if I use something like List.Generate, I don't have a way of storing previous table from the last run-through? 

 

Again, very new to M. Any thoughts are appreciated, thanks!

 

1 ACCEPTED SOLUTION

@WolfBiber isn't Power Query the part before the datamodel?

 

Below 2 queries and a video to show how it looks like. Tables are joined and expanded.

 

Remark: the tables that are joined, must have different column names (except for "PrimaryKey").

 

Should you have further requirements, please provide a link to a file with representative test data.

 

Query Start:

 

let
    Source = #table(type table[TableType = number,TableColumn = table,joinKind = number],
    {{1,Table1,null},
     {1,Table2,JoinKind.Inner},
     {2,Table3,null},
     {2,Table4,JoinKind.FullOuter},
     {2,Table5,JoinKind.FullOuter},
     {3,Table6,null},
     {3,Table7,JoinKind.FullOuter}})
in
    Source

 

Query Result:

 

let
    Source = Table.Buffer(Start),

    JoinTables = (Table as table) as table =>
    let
        RemovedTableType = Table.RemoveColumns(Table,{"TableType"}),
        TableRows = Table.ToRows(RemovedTableType),
        JoinTables = List.Accumulate(List.Skip(TableRows),TableRows{0}{0},(jt,t) => 
        let
            Joined = Table.NestedJoin(jt,{"PrimaryKey"},t{0},{"PrimaryKey"},"JoinedTableColumn",t{1}),
            ColumnNames = List.Difference(Table.ColumnNames(t{0}),{"PrimaryKey"}),
            Expanded = Table.ExpandTableColumn(Joined,"JoinedTableColumn",ColumnNames)
        in
            Expanded)    
    in
        JoinTables,

    #"Grouped Rows" = Table.Group(Source, {"TableType"}, {{"AllRows", JoinTables, type table}})
in
    #"Grouped Rows"

 

Video:

Specializing in Power Query Formula Language (M)

View solution in original post

14 REPLIES 14

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.