Skip to content

Combining stacked headers

This page shows how to turn a table whose headers are stacked over several rows into a clean, unpivoted table. The layout is common in exports from ERP and consolidation systems, where each value column carries more than one label, such as a date with a scenario and a source system stacked above it.

In the example below, the first four columns have ordinary headers (Country, Manufacturer, Model and Currency). Every value column has three stacked headers instead: a system in the top row, a scenario (actual or forecast) in the second, and a month-end date in the third.

An Excel sheet with Country, Manufacturer, Model and Currency columns, and twelve value columns each headed by three rows labelled System, Scenario and Date, showing SAP BFC, ACTUAL or FORECAST, and a month-end date in 2023

The method uses M code to reshape this into an unpivoted table, which is far easier to work with. It copes with any number of stacked header rows, not only one, and the result can be pivoted again on whichever header field is needed.

In the Power Query Editor, the stacked headers arrive as the first rows of data, under generic column names.

The data in the Power Query Editor under the names Column1 to Column7, where rows 1 and 2 hold the System and Scenario labels, row 3 holds the ordinary headers and the dates, and the car rows follow

2. Write a function that combines the top rows

Section titled “2. Write a function that combines the top rows”

Next, create a function that merges a given number of rows at the top of a table into a single header row. The function used here is called fnCombineTopXRows.

The Advanced Editor showing the fnCombineTopXRows function, which splits the table, converts the top rows to text, joins each column’s top values with the delimiter and promotes the result to headers

The function takes three arguments:

  1. the table to work on
  2. the number of rows, counted from the top, to combine
  3. the delimiter to place between the combined values, which defaults to a line feed if it is left out

It works by splitting the table in two with Table.SplitAt. It then joins the values in each column of the top part with List.Transform and Text.Combine, puts the combined row back on top of the rest with Table.Combine, and promotes it to headers.

  • Add a step to the main query that calls the function on the source table. Here it combines the top three rows, using ~^~ as the delimiter.

Each column now has a single header, made from its stacked values joined by the delimiter. The column that held the stacked header names becomes System~^Scenario^~Date, and the first value column becomes SAP BFC~^ACTUAL^~31/01/2023.

The formula bar showing fnCombineTopXRows(Workbook_Data, 3, “^”), with combined headers such as System~^Scenario^Date and SAP BFC^ACTUAL^~31/01/2023

  • Remove the column that held the stacked header names, which now contains only nulls.

The table after Table.RemoveColumns removes System~^Scenario^~Date, leaving the four attribute columns and the combined value columns

  • Select the four ordinary columns (Country, Manufacturer, Model and Currency) and choose Unpivot Other Columns.

Each value now sits on its own row, with its combined header in an Attribute column beside it.

The unpivoted table, with the four attribute columns, an Attribute column holding text such as SAP BFC~^ACTUAL^~31/01/2023 00:00, and a Value column

  • Split the Attribute column by the same delimiter, choosing Each occurrence of the delimiter.

The Split Column by Delimiter dialog, with a custom delimiter of ^ and Each occurrence of the delimiter selected

The stacked labels now sit in three separate columns, one for each header row.

The table after the split, with the columns Attribute.1 holding SAP BFC, Attribute.2 holding ACTUAL or FORECAST, and Attribute.3 holding the date

  • Rename the three columns after the stacked headers they came from: System, Scenario and Date. In this example, the Date column is also changed to a date type.

The table with the columns renamed System, Scenario and Date, where Date now holds dates without a time

The finished query in the Advanced Editor looks like this:

The Advanced Editor showing the Data Transformation query, from Excel.CurrentWorkbook through the function call, the column removal, the unpivot, the split, the renames and the two type changes

  • Choose Close & Load.

The worksheet now shows every header field side by side as an ordinary column, with each value on its own row.

The loaded Excel table with the columns Country, Manufacturer, Model, Currency, System, Scenario, Date and Value