Combining stacked headers
Overview
Section titled “Overview”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.

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.
1. Load the data into Power Query
Section titled “1. Load the data into Power Query”In the Power Query Editor, the stacked headers arrive as the first rows of data, under generic column names.

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 function takes three arguments:
- the table to work on
- the number of rows, counted from the top, to combine
- 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.
3. Call the function
Section titled “3. Call the function”- 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.

4. Remove the column of header names
Section titled “4. Remove the column of header names”- Remove the column that held the stacked header names, which now contains only nulls.

5. Unpivot the table
Section titled “5. Unpivot the table”- 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.

6. Split the Attribute column
Section titled “6. Split the Attribute column”- Split the Attribute column by the same delimiter, choosing Each occurrence of the delimiter.

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

7. Rename the new columns
Section titled “7. Rename the new columns”- 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.

8. Check the finished query
Section titled “8. Check the finished query”The finished query in the Advanced Editor looks like this:

9. Load the result
Section titled “9. Load the result”- Choose Close & Load.
The worksheet now shows every header field side by side as an ordinary column, with each value on its own row.

See also
Section titled “See also”- Finance: Financial Planning reaches a similar result with an older method, which takes more steps and more queries. Read the Financial Planning page