Sub topic 9
Unpivoting cross-tab data into tidy tables
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set data types. It also assumes you know how to fill down blank cells, which the previous chapter covers. Here you turn a wide, cross-tab layout into a tidy table, one that formulas, PivotTables, and Group By can use.
Why cross-tab layouts cause trouble
A cross-tab (or wide) table spreads one kind of value across many columns. A typical example is a sales report with one column per month:
| Region | Product | Jan | Feb | Mar |
|---|---|---|---|---|
| East | Widget | 120 | 135 | 150 |
| Gadget | 80 | 95 | 90 | |
| West | Widget | 60 | 70 | 75 |
This layout is easy to read, but hard to analyze. To total units by month, you have to reference each month column separately. Adding April means adding a column, and every formula that depends on the layout must change.
A tidy table has one column per variable and one row per observation. In this example the variables are Region, Product, Month, and Units. The same data becomes:
| Region | Product | Month | Units |
|---|---|---|---|
| East | Widget | Jan | 120 |
| East | Widget | Feb | 135 |
| East | Widget | Mar | 150 |
Unpivoting is the operation that converts the first layout into the second. Pivoting is the reverse.
Prepare the data before unpivoting
Unpivot works on the table as it stands, so fix the structure first.
- Make sure the header row is promoted, so the month names are column headers and not data.
- Fill down any blank cells in the identifier columns. In the example above, Region is blank on the second row. If you unpivot without filling down, the blank (null) Region is carried into every unpivoted row.
- Remove total rows and total columns. A "Grand Total" column would otherwise be unpivoted as if it were another month, and your sums would double count.
The first two steps matter most. Fill down belongs before unpivot because unpivot multiplies each row by the number of value columns. Fixing nulls afterward is possible but harder to verify.
Unpivot in the editor
Select the columns you want to keep as identifiers, which are Region and Product here. Then choose Transform > Unpivot Columns > Unpivot Other Columns. Power Query turns every remaining column into rows. It creates two new columns, named Attribute and Value. Double-click each header to rename them to Month and Units.
The Transform menu offers three commands:
| Command | What it unpivots | Use it when |
|---|---|---|
| Unpivot Columns | Only the columns you selected | The set of value columns is fixed |
| Unpivot Other Columns | Every column you did not select | New value columns may appear later |
| Unpivot Only Selected Columns | Only the selected columns | Same effect as Unpivot Columns, for a selection you made explicitly |
Prefer Unpivot Other Columns when the source may gain columns, such as a new month each period. It names the columns to keep, so new columns are unpivoted automatically on the next refresh.
The M code
The following query reads the table SalesByMonth, fills down Region, and unpivots everything except Region and Product:
let
Source = Excel.CurrentWorkbook(){[Name="SalesByMonth"]}[Content],
Filled = Table.FillDown(Source, {"Region"}),
Unpivoted = Table.UnpivotOtherColumns(Filled, {"Region", "Product"}, "Month", "Units")
in
UnpivotedTable.UnpivotOtherColumns takes four arguments: the table, the columns to keep, the name for the new attribute column, and the name for the new value column. Passing the names here saves you the rename step.
The command that unpivots only selected columns is Table.Unpivot. It takes a list of the columns to unpivot instead of the columns to keep. Because it names the value columns, it fails with an error on refresh if one of those columns disappears from the source.
Set types after unpivoting
The new Month column contains text such as "Jan". The Units column takes its type from the mixed source columns, so it often arrives as "any" type. Set Units to a whole number or decimal number explicitly. If the headers are real dates, convert Month to a date type, which keeps month order correct when you sort or chart.
Setting types after the unpivot is more reliable than before. Before the unpivot there are many value columns to type. Afterward there is exactly one.
How the process flows
Pivoting back
Sometimes you need the wide layout for a report. Select the column that holds the new headers (Month), then choose Transform > Pivot Column. In the dialog, pick Units as the Values Column. Open Advanced options and choose an aggregation. If each Region, Product, and Month combination appears only once, choose Don't Aggregate. If duplicates exist and you choose Don't Aggregate, you get an error saying there are too many elements in the enumeration.
For most analysis you do not need to pivot in Power Query. Load the tidy table and build a PivotTable from it, which gives you the cross-tab view and keeps your data tidy.
Common errors and edge cases
- Nulls in the Region column. You skipped Fill Down, or it ran on the wrong column. Check for null values after the unpivot.
- A "Total" row or column in the output. Remove totals before unpivoting. After the unpivot, a total column appears as a Month named "Total", and you can filter it out, but the cleaner fix is to remove it earlier.
- Blank cells in the value columns. Blank cells in the data become null values, and Unpivot drops rows whose value is null. This is useful when blanks mean "no data", but it hides a combination from your output. If you need a row for every combination, replace nulls with 0 before unpivoting.
- Headers that are not unique or not text. Month headers that are real dates in the source can become date or number column names. Unpivot handles them, but the Month column may need a type change afterward.
- Duplicate names in the Attribute column. Power Query requires column names to be unique, so duplicates get a suffix like
Jan_1. Clean this in the source or rename before unpivoting. - Step order matters. Because each Applied Step builds on the previous one, inserting a step before the unpivot can break the later steps. Insert steps in the middle only when you understand what the later steps expect.
Multi-row headers
Some reports have two header rows, such as a year above each month. Unpivot cannot handle this directly. First combine the header rows into one, for example "2025 Jan". Transpose the table, fill down the year, merge the two columns, transpose back, and promote the combined row as headers. After that, unpivot as usual and split the Attribute column if you need Year and Month separately.
Exercise
A table has the columns Store, Q1, Q2, Q3, Q4, and Total. You select Store and use Unpivot Other Columns. What problem will appear in the result, and what is the best fix?
Show answer
The Total column is unpivoted too, so the Attribute column contains "Total" rows alongside the quarters, and summing Value double counts the sales. The best fix is to remove the Total column before the unpivot step. Filtering out the "Total" rows after the unpivot also works, but removing the column first keeps the query simpler.
Exercise
Write the M step that unpivots all columns except Region and Product from a table in a step named Filled, naming the new columns Month and Units.
Show answer
Unpivoted = Table.UnpivotOtherColumns(Filled, {"Region", "Product"}, "Month", "Units")