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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
croissant
Regular Visitor

Combine multiple files with dynamic headers

Hi, I have a multiple sheets in which the headers at are at 2 subsequent rows: ie

croissant_1-1692034616410.png

 

I want to compile these sheets such that this will be the resulting query

croissant_0-1692034594493.png

 

 

*Data shown above are just samples. I am really working with 50+ files in a shared folder that I can't modify. I have tried using the transform file where you transpose merge then transpose again, but the method was not dynamic. Thanks.

 

4 REPLIES 4
wdx223_Daniel
Super User
Super User

=Table.Combine(List.Transform(YourTableList,each Table.RenameColumns(_,List.RemoveMatchingItems(List.Zip({Table.ColumnNames(_),Record.ToList(_{0})}),true,each if _ is logical then _ else _{0}=null or _{0}=""))))

Anonymous
Not applicable

Hi @croissant 

You can consider to put these sheets as multiple excel files then put them into a folder. the sheet format is as follows , set them as a table.

Book1:

vxinruzhumsft_0-1692066296656.png

Book2

vxinruzhumsft_1-1692066320073.png

Then put the files in a folder

vxinruzhumsft_2-1692066345800.png

 

Then you can use folder transform, you can refer to the following link.

Combine Data from Multiple Workbooks in Excel (using Power Query) [5296-9a87-434-44a7] (trumpexcel.c...

(refer to the 'Each Workbook has the data with Different Table names or Sheet Names' in the link)

Then you can transform them to the result .

Output

vxinruzhumsft_3-1692066555035.png

 

 

Best Regards!

Yolo Zhu

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

 

Hi! Thanks for the response but the samples I've shown are just samples. I'm really working with 50+ files in a company folder that I can't modify. The main problem is the different and dynamic headers across two rows that is different per file

Anonymous
Not applicable

Hi @croissant 

You can try the following code in Advanced Editor

The Folder.files() need to put the path of your folder.

let
    Source = Folder.Files("C:\Users\xxx\Desktop\yourfoldername"),
    #"Filtered Hidden Files1" = Table.SelectRows(Source, each [Attributes]?[Hidden]? <> true),
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File", each #"Transform File"([Content])),
    #"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File"}),
    #"Added Custom" = Table.AddColumn(#"Removed Other Columns1", "Custom", each Table.PromoteHeaders([Transform File], [PromoteAllScalars=true])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Transform File"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Column1", "Column2", "Column3", "Avail", "WFH", "Rate", "Grade", "CSAT"}, {"Column1", "Column2", "Column3", "Avail", "WFH", "Rate", "Grade", "CSAT"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Custom",{{"Column1", "ID"}, {"Column2", "Name"}, {"Column3", "Date"}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Renamed Columns",{"Source.Name"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns1",{{"ID", Order.Ascending}})
in
    #"Sorted Rows"

Best Regards!

Yolo Zhu

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

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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