Skip to content

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.

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:

Sheet1, with columns Date, Sales Rep, Customer, Amount, Profit, Region and Another Col Sheet2, with the same first six columns and a last column named Category Sheet3, with the same first six columns and a last column named New Col

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:

The Source step built with Excel.Workbook, listing Sheet1, Sheet2 and Sheet3 with a Table in the Data column of each

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:

A preview of Sheet1’s table, with Date, Sales Rep, Customer and the other headings in the first row under the default names Column1 to Column7

  • Add a custom column, called Custom, that applies Table.PromoteHeaders to 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

  • Add a step named ColNames that stacks the tables with Table.Combine and then reads the headers with Table.ColumnNames. Because Table.Combine keeps every column found in any of the tables, the list includes the columns that appear on only one sheet:

A list of nine column names, from Date to Region followed by Another Col, Category and New Col

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

The formula Table.SelectColumns(#“Added Custom”,{“Custom”, “Name”}), leaving a Custom column of tables and a Name column of sheet names

  • Finally, use Table.ExpandTableColumn to expand the Custom column, passing it the ColNames list instead of a fixed list of names:

The formula Table.ExpandTableColumn(#“Removed Other Columns”, “Custom”, ColNames), with the expanded columns Date, Sales Rep, Customer and Amount, and the Applied Steps from Source to Expanded Custom

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.

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 loaded Excel table with columns Sales Rep through New Col and Name, where the Sheet1 rows have values under Another Col and blanks under Category and New Col

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"