Sub topic 12
Unpivoting cross-tab data
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set column data types. It covers one reshaping step: turning a cross-tab layout into a tall table that Power BI can model.
Why cross-tab data causes trouble
A cross-tab (wide) table spreads one kind of value across many columns. A sales report might have one column per month: Jan, Feb, Mar. Each new month adds a column. A report visual cannot easily put "Month" on an axis, because the months are column names and not values in a column.
Unpivoting rotates those columns into rows. You end up with one column for the old column names (the attribute) and one for the cell values (the value). Tall tables with one fact per row are the shape that Power BI models and visuals work best with.
| Wide shape | Tall shape |
|---|---|
| Product, Jan, Feb, Mar | Product, Month, Sales |
| 1 row per product | 1 row per product and month |
| New month = new column | New month = new rows |
Unpivot in the editor
- Select the column or columns that should stay as they are, such as Product.
- On the Transform tab, open Unpivot Columns and choose Unpivot Other Columns.
- Double-click the new Attribute and Value headers and rename them, for example Month and Sales.
The editor records the step in M. Here is a complete query you can paste into a blank query in the Advanced Editor:
let
Source = #table(
{"Product", "Jan", "Feb", "Mar"},
{{"Widget", 10, 12, 9}, {"Gadget", 4, 6, 8}}
),
Unpivoted = Table.UnpivotOtherColumns(Source, {"Product"}, "Month", "Sales")
in
UnpivotedThe result has six rows, such as Widget, Jan, 10 and Gadget, Feb, 6. The second argument lists the columns to keep. The last two arguments name the new columns.
Choosing the right unpivot command
The editor offers three commands. They differ in how they treat columns that appear later.
| Command | What it unpivots | Use when |
|---|---|---|
| Unpivot Columns | Only the columns you selected | The set of columns is fixed |
| Unpivot Other Columns | Everything except the selected columns | New columns may appear, such as new months |
| Unpivot Only Selected Columns | Same as Unpivot Columns, with the column names written into the step | You want to be explicit |
Prefer Unpivot Other Columns for data that grows sideways. Next month's column is then unpivoted automatically on refresh. With the fixed-column commands, the new column stays wide.
Common errors
- Unpivoting before cleaning headers. If the month names are still in the first data row, promote headers first. Otherwise the Attribute column holds values like Column2.
- Mixed types in the Value column. The Value column often comes out as type Any. Set its type, such as whole number or decimal, after unpivoting, so text in the source shows up as errors you can handle.
- Selecting the wrong columns. Unpivot Other Columns unpivots everything you did not select. Leaving a total column unselected turns it into rows named Total, which double counts sales. Remove such columns first.
- Attribute values that need conversion. Month names arrive as text. Convert them to a date or add a month number column if you need chronological sorting.
Exercise
A table has the columns Region, Q1, Q2, Q3 and Q4. Next year the source will add Q5 for a fifth period. Which unpivot command should you use, which column do you select first, and why?
Show answer
Select Region, then choose Unpivot Other Columns. This command unpivots every column except Region, so Q5 is picked up automatically on the next refresh. Unpivot Columns would leave Q5 as a wide column.