Generalised unpivoting
From two levels to any number
Section titled “From two levels to any number”The previous page unpivoted a report with two levels of headings on each axis. The same idea works for any number of levels, as long as the steps are counted from the number of levels rather than written for one layout.
In what follows, N is the number of levels of row headings and M is the number of levels of column headings. The example has three of each:
- rows are headed by country, then state or region, then city
- columns are headed by colour, then parent category, then category
The method at a glance
Section titled “The method at a glance”The diagrams follow a small sample through every step. The idea is to squeeze each axis into a single column of merged labels, unpivot once, and then split the labels back out.

Step 1: load the summary table
Section titled “Step 1: load the summary table”Load the table into the editor. It is sparse, because most combinations of city and category have no sales, and each heading appears only once at the top of its group.

In this example the range starts with an empty row, which the steps below deal with after transposing.
Step 2: fill down the outer row headings
Section titled “Step 2: fill down the outer row headings”Select the first N-1 columns, which here are Column1 and Column2, and choose Fill, then Down. Each city row now carries its country and state. The innermost level, the city, is already on every row, so it needs no filling.

Step 3: merge the row headings
Section titled “Step 3: merge the row headings”Select the first N columns and choose Merge Columns with Colon as the separator. Every row now has one label, such as United Kingdom:England:Liverpool.

Step 4: transpose
Section titled “Step 4: transpose”On the Transform tab, choose Transpose. The column headings now run down the columns on the left, and each merged row label heads a column of values.
The empty first row of the source has become Column1, which holds nothing but the colons left by merging three empty cells, so remove it. The column headings then occupy the first M columns, which here are Column2 to Column4.

Step 5: fill down the outer column headings
Section titled “Step 5: fill down the outer column headings”Fill down the first M-1 columns, which hold the colour and the parent category. As before, the innermost level, the category, is already complete.

Step 6: promote the headers
Section titled “Step 6: promote the headers”On the Home tab, choose Use First Row as Headers. The first M columns take headers built from the merged labels, such as ::Color and Country:State/Region:City \ Category, and every other column is headed by a merged row label.

Step 7: unpivot
Section titled “Step 7: unpivot”Select the first M columns, which are ::Color, ::Parent Category and Country:State/Region:City \ Category, then choose Unpivot Other Columns. Every non-empty value becomes a row, with its merged row label in Attribute.

Step 8: split the row labels
Section titled “Step 8: split the row labels”Select Attribute, then choose Split Column, then By Delimiter, with Colon as the delimiter. The label splits back into N columns.

Step 9: rename the columns
Section titled “Step 9: rename the columns”Give each column a name that says what it holds.

Step 10: load the table
Section titled “Step 10: load the table”Choose Close & Load. The sparse summary is now a flat table with one amount per row, and every row says which colour, category and place the amount belongs to.

The steps only ever refer to “the first N columns” or “the first M columns”, which is what makes them general. The next page turns them into a function that takes N and M from its inputs.