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 builds on the data types chapter. Power BI models and visuals work best with tall, narrow tables, and unpivoting is how you turn a wide table into one.
Why cross-tab data causes problems
A cross-tab (also called a matrix or wide table) puts one category across the column headers. Spreadsheets often look like this:
| Product | Jan | Feb | Mar |
|---|---|---|---|
| Widget | 10 | 12 | 9 |
| Gadget | 4 | 6 | 8 |
The month names are data, but they are stored as column names. That causes three problems in Power BI:
- You cannot put "Month" on a chart axis, because no column called Month exists.
- A measure such as total sales has to add three separate columns instead of summing one.
- When April arrives, a new column appears. Report visuals and relationships that depend on the old column list can break.
Unpivoting rotates the selected columns into rows. The old column headers become values in a new attribute column, and the old cell values go into a new value column.
| Product | Month | Sales |
|---|---|---|
| Widget | Jan | 10 |
| Widget | Feb | 12 |
| Widget | Mar | 9 |
| Gadget | Jan | 4 |
| Gadget | Feb | 6 |
| Gadget | Mar | 8 |
The three unpivot commands
Select columns in the editor, then open the Transform tab and choose Unpivot Columns. The drop-down offers three commands.
| Command | What it unpivots | M function |
|---|---|---|
| Unpivot Columns | Only the columns you selected | `Table.Unpivot` |
| Unpivot Other Columns | Every column except the ones you selected | `Table.UnpivotOtherColumns` |
| Unpivot Only Selected Columns | Only the selected columns (same effect as Unpivot Columns) | `Table.UnpivotOtherColumns` or `Table.Unpivot`, depending on version |
In practice you will use the first two. The difference matters when the source changes.
A worked example
The following query builds the small table from above and unpivots it. It uses #table so it runs on its own. Paste it into a blank query through Home, New Source, Blank Query, then 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
UnpivotedTable.UnpivotOtherColumns takes four arguments:
- The input table.
- A list of the columns to keep as they are. These are the identifier columns.
- The name for the new attribute column, which receives the old headers.
- The name for the new value column, which receives the old cell contents.
In the interface, you would right-click the Product header and choose Unpivot Other Columns. The editor names the new columns Attribute and Value. Double-click each header to rename them, or edit the M code as shown.
Choosing between selected and other columns
The two commands behave differently when new columns arrive in the source.
- Unpivot Other Columns writes the list of columns to keep. If April is added later, it is not in that list, so it is unpivoted automatically. This is the right choice for date-style or period-style columns that grow over time.
- Unpivot Columns (
Table.Unpivot) writes the list of columns to rotate. A new April column is not in that list, so it stays as a column and your report will miss it.
The reverse risk also exists. If the source gains an extra descriptive column, such as Category, Unpivot Other Columns will rotate it into the Month attribute as if it were a month. Decide which side of the table is stable and list that side.
Fixing the result
Unpivoting creates new columns, and several details need attention afterwards.
Set data types
The new Attribute column is text, even if the old headers were dates like 2026-01. The Value column is usually typed as Any until you set it. Change the types explicitly, and use a locale when the headers or values depend on regional formats. For example, if the headers are real month names you may want to convert them to dates in a later step.
Mixed content in value cells
If the source cells contain a mix of numbers and text such as "n/a", the Value column cannot become a number without errors. Clean or replace those values first, or handle them with the error techniques from the earlier chapter on finding and replacing errors.
Nulls become rows
Empty cells in the wide table are unpivoted too. In the default operation, null cells are dropped. Rows with null values do not appear in the output of Table.Unpivot and Table.UnpivotOtherColumns. If you need to keep a row for every product and month, replace nulls with 0 or another value before unpivoting. Check the row count after the step to see which behavior you got.
Header promotion first
Unpivoting depends on real column names. If the first row still holds the headers, the Attribute values will be wrong. Promote headers and remove junk rows before this step.
Multi-level headers
Some sources have two header rows, such as a year above each month. Unpivot cannot read two levels at once. The usual approach is:
- Transpose the table so the header rows become columns.
- Fill down the year values, because only the first cell of each year is filled.
- Merge the year and month columns into one column, for example
2026-Jan. - Transpose back, promote the merged row to headers, and then unpivot.
This takes several steps, so keep an eye on the Applied Steps pane and rename each step so you can follow it later.
Reversing the operation
Pivot Column does the opposite. It turns the values of one column into new column headers. You rarely need it in a cleaning workflow, because the tall shape is the one you want in the model. Use it only when a specific output requires a wide shape.
Add more transformations
A common next step is splitting or adding columns on the unpivoted result. The following query unpivots and then converts the attribute to a number and the value to a type.
let
Source = #table(
{"Product", "Jan", "Feb", "Mar"},
{{"Widget", 10, 12, 9}, {"Gadget", 4, 6, 8}}
),
Unpivoted = Table.UnpivotOtherColumns(Source, {"Product"}, "Month", "Sales"),
ChangedType = Table.TransformColumnTypes(
Unpivoted,
{{"Month", type text}, {"Sales", Int64.Type}},
"en-US"
)
in
ChangedTypeCommon errors
| Symptom | Likely cause | Fix |
|---|---|---|
| New month columns are missing from the report | Used Unpivot Columns with a fixed list | Reapply with Unpivot Other Columns |
| An extra descriptive column appears as a month | Used Unpivot Other Columns and a new column was added | Move the column out first, or switch the approach |
| Attribute shows `Column2`, `Column3` | Headers were not promoted | Promote headers before unpivoting |
| Rows are missing for empty cells | Nulls are dropped | Replace nulls before unpivoting |
| Errors in the Sales column | Text such as `n/a` in value cells | Replace or clean the text, then set the type |
Exercise
A monthly report table has columns Region, Jan, Feb, and Mar, and a new month column is added to the source each month. Which unpivot command should you use on it, which column should you select first, and why?
Show answer
Select the Region column and use Unpivot Other Columns. The step stores the list of columns to keep (Region). Any new month column is not in that list, so it is unpivoted automatically on the next refresh. Unpivot Columns would store a fixed list of months and miss the new one.
Exercise
After unpivoting a table, the row count is lower than the number of products times the number of months. What is the most likely cause, and how would you keep a row for every combination?
Show answer
Empty cells (nulls) in the wide table are dropped by the unpivot step. To keep every combination, replace the nulls with 0 or another value before the unpivot step, then compare the row count again.