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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
ragav_in
New Member

Combine Multiple Worksheets with Different Order of Columns

Dear members,

 

The task in hand is to combine data from multiple workbooks from a local folder in my computer. I wanted to use Power Query to combine data from all the files and perform certain transformation and then consolidate data into single worksheet. This task is easy when the individual workbooks are of the same template (same number of Columns in the same order). However, the challenge is that the various workbooks that we receive are of different templates (the Columns are not in the same order). In some workbooks we have 10 Columns, while in some we have 20+ columns (the number of Columns are dynamic and cannot be controlled).  We have to collate data from 1st 5 Columns in a certain workbook, while these 5 Columns are in a different Columns in other workbooks. The only thing that is common across all workbooks are that they have the same Column Header.

Is there a way how this can be achieved via Power Query? Below are sample reports with different column order. What we need is to collate data from Column 1, Column 2, Column 3, Column 4 and Column 5 (as highlighted in Red font). 

 

Report 1.xlsx

Column 1Column 2Column 3Column 4Column 5Column 6Column 7Column 8Column 9Column 10

 

Report 2.xlsx

Column 6Column 7Column 8Column 9Column 10Column 1Column 2Column 3Column 4Column 5

 

Report 3.xlsx

Column 6Column 1Column 7Column 2Column 8Column 3Column 4Column 5Column 9Column 10

 

I thank everyone who puts their time and effort to review this situation and provide a solution to achieve this in Power Query.

 

Thanks

ragav_in

2 REPLIES 2
v-stephen-msft
Community Support
Community Support

Hi @ragav_in ,

 

Did you want to merge with multi tables?

In the Merge feature, catching columns can have multiple columns, hold down Ctrl and then multi-select.

Tutorial: Shape and combine data in Power BI Desktop - Power BI | Microsoft Learn

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

AntrikshSharma
Community Champion
Community Champion

@ragav_in If names of columns that you want to retain are same across all workbooks then you can just write a simple code to select only those columns?

let
    Source = Folder.Contents ( "C:\Users\SharmaAnt\Downloads\ColumnsTest" ),
    KeepContent = Table.SelectColumns ( Source, { "Content" } ),
    ColumnsToKeep = List.Transform ( { 1 .. 5 }, each "Column " & Text.From ( _ ) ),
    AddedCustom = 
        Table.AddColumn (
            KeepContent,
            "Custom",
            each 
            Table.SelectColumns (
                Table.SelectRows ( 
                    Excel.Workbook ( [Content] ), 
                    each [Kind] = "Table" 
                )[Data]{0},
                ColumnsToKeep
            )
        ),
    RemovedOtherColumns = Table.SelectColumns ( AddedCustom, { "Custom" } )
in
    RemovedOtherColumns

 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors