Skip to content

Generalised unpivoting

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 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.

A diagram of the first five steps on a small sample: fill down the first N-1 row-heading columns, merge the N row-heading columns with a delimiter, transpose, fill down the first M-1 column-heading columns, and use the first row as headers, which leaves values such as United Kingdom: England: Liverpool across the top A diagram of the last three steps: select the M column-heading columns and unpivot the others, split the Attribute column by the delimiter, and rename the columns, which gives one row per value with colour, parent category, category, country, region and city beside it

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.

The source table in the editor, with Color, Parent Category and City \ Category labels in Column3, colours, parent categories and categories across rows 2 to 4, and countries, states and cities down Column1 to Column3 from row 5

In this example the range starts with an empty row, which the steps below deal with after transposing.

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.

Column1 and Column2 after Fill Down, with United Kingdom and England repeated against every English city and United States and California against the Californian ones

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.

The Merged column, holding labels such as United Kingdom:England and United States:California, beside the value columns

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.

The transposed table, with colours in Column2, parent categories in Column3 and categories in Column4, where each colour and parent category appears only on the first row of its group

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.

The same columns after Fill Down, with Black, Blue, Multi, Red and Silver repeated against each of their categories, and each parent category repeated against its categories

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.

The table after promoting headers, with columns ::Color, ::Parent Category and Country/Region \ Category, followed by columns headed United Kingdom:England and United Kingdom:England

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.

The unpivoted table, with colour, parent category and category columns, an Attribute column holding labels such as United Kingdom:England, and a Value column

Select Attribute, then choose Split Column, then By Delimiter, with Colon as the delimiter. The label splits back into N columns.

The table after splitting, with Attribute.1 holding the country, Attribute.2 the state or region and Attribute.3 the city

Give each column a name that says what it holds.

The renamed table, with Color, Parent Category, Sub Category, Country, Region, City and Amount columns

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 loaded worksheet table with Color, Parent Category, Sub Category, Country, Region, City and Amount columns, starting with black helmets in Liverpool, London and Woolston

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.