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 1 | Column 2 | Column 3 | Column 4 | Column 5 | Column 6 | Column 7 | Column 8 | Column 9 | Column 10 |
Report 2.xlsx
Column 6 | Column 7 | Column 8 | Column 9 | Column 10 | Column 1 | Column 2 | Column 3 | Column 4 | Column 5 |
Report 3.xlsx
Column 6 | Column 1 | Column 7 | Column 2 | Column 8 | Column 3 | Column 4 | Column 5 | Column 9 | Column 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
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.
@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