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
Anonymous
Not applicable

Combine Tables from a List of Tables having different columns - Power Query

Hi,

I have a List of Tables to Combine together. Each Table contains a main set of columns that are same and another set of columns that are different in each table. I want to combine them so that all the row values fall in the same rows and not on subsequent rows.

 

List of Tables:

List
Table
Table

 

Table1:

ABCR1R2R3R4
1asd0.60.230.510.330.56
2asd0.6ACFBCAFGILMN
3asd0.611null1
1ghi0.10.110.210.430.96
2ghi0.1DCMDHJTROPQR
3ghi0.111null1
1hkl0.20.230.510.330.56
2hkl0.2ARPVBAGFIWQE
3hkl0.211null1

 

Table2:

ABCR5R6R7R8
1asd0.60.450.250.110.36
2asd0.6DSAFDGRERLUN
3asd0.61nullnull1
1ghi0.10.910.430.130.94
2ghi0.1WEDQYTSAOLAP
3ghi0.1111null
1hkl0.20.490.780.610.56
2hkl0.2MAGRORKALOLS
3hkl0.21nullnull1

 

Final Table:

combined table.png

Is this possible to do using M-Language when the List of Tables is expanded?

@shaowu459 

 

https://docs.microsoft.com/en-us/answers/questions/159128/merge-tables-in-a-list-of-tables-having-di... 

1 ACCEPTED SOLUTION

= Table.PromoteHeaders(
                    Table.FromColumns(
                         List.FirstN( Table.ToColumns( Table.DemoteHeaders( RemovedCol1Column[List]{0} ) ) , 6) & 
                         List.Combine( List.Transform( RemovedCol1Column[List], each List.Skip( Table.ToColumns( Table.DemoteHeaders(_) ) , 6) ) )
                    )
               )

 

add [List] after RemovedCol1Column, List is the first column name of table in RemovedCol1Column.

View solution in original post

11 REPLIES 11
Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

you can join both tables, and then expand the needed data.

Here an example.

Step Table1 and Table2 are only to simulate your list of tables

let
    Table1 = 
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUosTgGSBnpmYNLIGEyZGoIpYyjPTClWJ1rJCE21o7MbkHRydgSSbu6eQNLH1w+s0hhNpSEU55Xm5ICZIEUggfSMTLAiiHWGEMoIQplALLdEWI6s2sXZF0R6eAHJkCB/IBkQGAS3HFklTsszsnMg9hHlc2TVjkEBQDLMCeRzdzeQz8MDXeGWI6vEtDwWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [A = _t, B = _t, C = _t, R1 = _t, R2 = _t, R3 = _t, R4 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", type text}, {"C", Int64.Type}, {"R1", type text}, {"R2", type text}, {"R3", type text}, {"R4", type text}})
    in
        #"Changed Type",
    Table2 = 
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY/BCsIwEET/JecipkmrOQZSPRiNNopI6EEQrFi8iP/vZhfTUDz0DYRHZycExlnBru8bcD6rkbLCKCk4xxA164rAyoltvAauzBrYNi3QnnZoiokZf/P6DMMYHL34fu8f1IVUFFLQG4WSqT+3z40BHi5HoNcu9ut96s/N/MMDfuX9c6C91KowFks6nE6pxvG5vdU428XZG22BzvpUnpt/x3df", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [A = _t, B = _t, C = _t, R5 = _t, R6 = _t, R7 = _t, R8 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", type text}, {"C", Int64.Type}, {"R5", type text}, {"R6", type text}, {"R7", type text}, {"R8", type text}})
    in
        #"Changed Type",
    CreateYourList = {Table1, Table2},
    JoinTables = Table.NestedJoin
    (
        CreateYourList{0},
        {"A","B","C"},
        CreateYourList{1},
        {"A","B","C"},
        "Table2"
    ),
    #"Expanded Table2" = Table.ExpandTableColumn(JoinTables, "Table2", {"R5", "R6", "R7", "R8"}, {"R5", "R6", "R7", "R8"})
in 
    #"Expanded Table2"

Jimmy801_0-1605100903347.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

mahoneypat
Employee
Employee

You can create your desired table with a Merge step.  On your first query, click on Merge Queries, choose your second one, and in the join screen popup choose the first three columns in order in both tables.  Then expand the column with the merged tables.

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat it is a list containing multiple tables that i need to combine, but the rows are not getting merged, but falling in separate rows.

Change Source to the previous step name, there are TWO SOURCE in my code.

 

= Table.PromoteHeaders(Table.FromColumns(List.FirstN(Table.ToColumns(Table.DemoteHeaders(Source{0})),3)&List.Combine(List.Transform(Source,each List.Skip(Table.ToColumns(Table.DemoteHeaders(_)),3)))))

1.png

 

1.png

Anonymous
Not applicable

I see your solution is dynamic to incorporate multiple number of tables. However, i am getting the following error when i tried to implement your code :

 

Expression.Error: We cannot convert a value of type Record to type Table.
Details:
    Value=Record
    Type=Type

 

 My first 6 columns are similar in my original file:  

Code:

 

= Table.PromoteHeaders(
                    Table.FromColumns(
                         List.FirstN( Table.ToColumns( Table.DemoteHeaders( RemovedCol1Column{0} ) ) , 6) & 
                         List.Combine( List.Transform( RemovedCol1Column, each List.Skip( Table.ToColumns( Table.DemoteHeaders(_) ) , 6) ) )
                    )
               )

 

`RemovedCol1Column` is the previous step containing the List of tables.

= Table.PromoteHeaders(
                    Table.FromColumns(
                         List.FirstN( Table.ToColumns( Table.DemoteHeaders( RemovedCol1Column[List]{0} ) ) , 6) & 
                         List.Combine( List.Transform( RemovedCol1Column[List], each List.Skip( Table.ToColumns( Table.DemoteHeaders(_) ) , 6) ) )
                    )
               )

 

add [List] after RemovedCol1Column, List is the first column name of table in RemovedCol1Column.

Anonymous
Not applicable

Man, you are a Genius! 😀

are there more thant 2 tables in the list and the first 3 columns are all the same for all the tables?

Anonymous
Not applicable

Yes, multipe tables depending on the number of csv files that get converted to a list of tables. Yes, the initial 3 column rows (multiples of 3 rows) may be same across the different tables. If same, then their rows should get merged on the same rows, as their R1...Rx columns will be different. 

Got it.  How many tables are in the list?  If just the two shown, you have at least two options

 

1. Convert list to table, transpose it, then add a column with the Table.Join of the first two columns, specifying the columns in order

 

2. Insert a step after, use Table.Join and reference the two tables with #"Previous Step"{0} and #"Previous Step"{1}

 

Replace with your actual previous step name.

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

More than 2 tables. These could vary depending on number of csv files that get converted to a list of tables.

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.

Top Solution Authors
Top Kudoed Authors