Sub topic 9
Unpivoting cross-tab data into tidy tables
This chapter assumes you can open the Power Query Editor, read Applied Steps, and set data types. It also assumes you know how to remove blank rows and promote headers.
Why cross-tab layouts cause trouble
A cross-tab has one column per period, such as Jan, Feb, and Mar. It is easy to read but hard to filter, group, or chart. A tidy table has one column per variable and one row per observation. For monthly sales, that means Region, Product, Month, and Units.
Unpivoting turns column headers into values in a new column, and turns the cells beneath them into values in another new column.
Prepare the data first
Cross-tabs often show a region name once and leave the cells below it blank. Fill those blanks before you unpivot. Otherwise the blank cells become nulls, and the rows lose their region.
- Select the Region column.
- Choose Transform, then Fill, then Down.
Unpivot the month columns
- Select the columns you want to keep as they are, here Region and Product.
- Right-click a selected header and choose Unpivot Other Columns.
- Rename the new columns from Attribute and Value to Month and Units.
The generated M code looks like this:
let
Source = Excel.CurrentWorkbook(){[Name="SalesByMonth"]}[Content],
Filled = Table.FillDown(Source, {"Region"}),
Unpivoted = Table.UnpivotOtherColumns(Filled, {"Region", "Product"}, "Month", "Units")
in
UnpivotedChoose the right unpivot command
| Command | What it unpivots | When to use it |
|---|---|---|
| Unpivot Other Columns | Every column except the ones you selected | Columns may be added later, such as a new month |
| Unpivot Columns | Only the columns you selected | The set of columns is fixed |
| Unpivot Only Selected Columns | Same as Unpivot Columns, with the column names recorded in the step | You want that choice stated explicitly |
Common errors
- Totals unpivoted as data. A Total column or row becomes extra Month values. Remove it before the unpivot step.
- Wrong types after unpivoting. Month is text and Units may be text too. Set the types after the unpivot step, not before.
- Nulls in Units. Empty cells are dropped by the unpivot, so you may see fewer rows than expected. This is usually the result you want.
- Month names that are not dates. Labels such as Jan stay as text. Convert them with a custom step if you need to sort by date.
Pivoting back
The reverse is Transform, then Pivot Column. Choose the column whose values become headers, then the values column. Pivoting is useful for a summary view, but you should usually keep your stored data tidy and build the cross-tab in a PivotTable.
Exercise
A cross-tab has columns Region, Product, Jan, Feb, and Mar. Region is blank on every row after the first row of each group. In what order should you fill and unpivot, and which unpivot command keeps new month columns working on refresh?
Show answer
Fill down Region first, so the blank cells get values. Then select Region and Product and use Unpivot Other Columns. That command unpivots every column not selected, so a new month column is included on the next refresh.
Exercise
After unpivoting, the Units column cannot be summed in a PivotTable. What is the likely cause, and how do you fix it?
Show answer
The column is probably text. Select Units in the Power Query Editor and set its data type to Whole Number or Decimal Number, then load the query again.