Dynamically expanding columns
This page shows how to combine data from several sheets when the sheets do not share exactly the same columns. The query reads the column names from the data on every refresh, so it keeps working when a column is added or renamed.
The example
Section titled “The example”A workbook holds three sheets of sales data. Most of their columns match, but the last column differs on each sheet, and all of the data should be imported:

1. Import the workbook
Section titled “1. Import the workbook”Connect to the workbook from Power Query. The Source step lists each sheet as a row, with the sheet’s contents held as a table in the Data column:

2. Promote the headers on every sheet
Section titled “2. Promote the headers on every sheet”Clicking beside a Table in the Data column previews that sheet. The headers are still in the first row, and the columns are named Column1, Column2 and so on:

- Add a custom column, called Custom, that applies
Table.PromoteHeadersto each sheet’s table. Each row of Custom then holds the same table with its headers in place:
![The formula Table.AddColumn(Source, “Custom”, each Table.PromoteHeaders([Data])), with a preview of Sheet1’s table now headed Date, Sales Rep, Customer and so on](/_astro/dynamically-expanding-columns-6.Dwz0SW9F_26ekvL.webp)
3. Build the list of columns and expand
Section titled “3. Build the list of columns and expand”- Add a step named ColNames that stacks the tables with
Table.Combineand then reads the headers withTable.ColumnNames. BecauseTable.Combinekeeps every column found in any of the tables, the list includes the columns that appear on only one sheet:

- Use
Table.SelectColumnson the Added Custom step to keep just the Custom and Name columns, where Name holds the name of each sheet:

- Finally, use
Table.ExpandTableColumnto expand the Custom column, passing it the ColNames list instead of a fixed list of names:

The query now copes with a column being added to any sheet, or with an existing heading being renamed, because the list of columns is rebuilt on each refresh.
4. Load the result to Excel
Section titled “4. Load the result to Excel”Close & Load puts the combined data on a worksheet. Each sheet’s rows have blanks in the columns that sheet does not have, and the Name column shows which sheet each row came from:

The full query
Section titled “The full query”Here is the whole query. The file path in the Source step will be different on your machine:
let Source = Excel.Workbook(File.Contents("C:\Users\OneDrive\Desktop\Dynamically Expanding Columns - Data.xlsx"), null, true), #"Added Custom" = Table.AddColumn(Source, "Custom", each Table.PromoteHeaders([Data])), ColNames = Table.ColumnNames(Table.Combine(#"Added Custom"[Custom])), #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom", "Name"}), #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", ColNames)in #"Expanded Custom"