Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Automatic tables merge and include new columns

Hello,

I have the following 2 tables in Power Query and i am trying to automatically merge them based on the Year and Week number and include any new columns.

 

Table A:

YearWeek numberStockReturnsDefective
20221348
202226917
202232510
202249316
202255615
202362210
202277822
202283313
202296318
2022103314
2022112717
2022123722
2022137519
2022149818
2022156716
20221671434

 

Table B:

YearWeek numberSoldExcessExpired
202212811
202226210
202233612
202243512
202257315
202362311
202279319
202284713
202297218
2022108914
2022115217
2022123621
2022136818
20221491817
202215112616
20221612323

 

This is what i did:

 

Table AB Merged:

YearWeek numberStockReturnsDefectiveSoldExcessExpired
202213482811
2022269176210
2022325103612
2022493163512
2022556157315
2023622102311
2022778229319
2022833134713
2022963187218
20221033148914
20221127175217
20221237223621
20221375196818
202214981891817
2022156716112616
2022167143412323

 

But the problem is that if new columns are added to the tables, then they are not automatically reflected into the merged table. I have to manually select the new columns.

For example, if a new column is added to Table B, called Overdue, then it is not automatically included into the Merged table.

Is there a way to fix this? Any help is much appreciated!

 

  • Hi Anonymous ,

     

    try 

    Table.ExpandTableColumn(
          Source, 
          "Table B",  
          List.Select(
               Table.ColumnNames(#"Table B"), 
               each 
                   _ <> "Year" and _ <> "Week Number"
          )
    )

7 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      KT_Bsmart2gethe Thanks for your reply. I tried to adapt it to the two tables A and B above, but i couldn't get the correct results. 

       

      This is the line that should be replaced:

       

      = Table.ExpandTableColumn(Source, "Table B", {"Sold", "Excess", "Expired"}, {"Sold", "Excess", "Expired"})

       

      This is my attempt and but there are some issues:

      1. how to remove the prefix for the second table's columns?

      2. how to keep only one copy for Year and Week number columns?

       

      = Table.ExpandTableColumn(Source, "Table B", Table.ColumnNames(#"Table B"), List.Transform(List.Select(Table.ColumnNames(#"Table B"), each not Text.Contains(_,"Suburb")), each "City_List."&_))

       

       

      Results:

      YearWeek numberStockReturnsDefectiveCity_List.YearCity_List.Week numberCity_List.SoldCity_List.ExcessCity_List.Expired
      20221348202212811
      202226917202226210
      202232510202233612
      202249316202243512
      202255615202257315
      202362210202362311
      202277822202279319
      202283313202284713
      202296318202297218
      20221033142022108914
      20221127172022115217
      20221237222022123621
      20221375192022136818
      202214981820221491817
      2022156716202215112616
      2022167143420221612323

       

      • KT_Bsmart2gethe's avatar
        KT_Bsmart2gethe
        Icon for Impactful Individual rankImpactful Individual

        Hi Anonymous ,

         

        1. how to remove the prefix for the second table's columns?

        Remove the RED below will remove the prefix.

         

        2. how to keep only one copy for Year and Week number columns?

        Add the BLUE below will remove the Year and Week columns.

        Table.ExpandTableColumn(Source, "Table B", Table.ColumnNames(#"Table B"), List.Transform(List.Select(Table.ColumnNames(#"Table B"), each _ <> "Year" and _ <>"Week Number" not Text.Contains(_,"Suburb")), each "City_List."&_))

         

        The corrected code to your case:

        Table.ExpandTableColumn(
              Source, 
              "Table B", 
              Table.ColumnNames(#"Table B"), 
              List.Select(
                   Table.ColumnNames(#"Table B"), 
                   each 
                       _ <> "Year" and _ <> "Week Number"
              )
        )

         

        Regards

        KT