Power Query course · Lesson 7 of 17
Unpivot and pivot: reshaping tables
Turn cross-tab reports with one column per month into tidy lists with Unpivot, and turn lists back into columns with Pivot Column, with the right options.
Many spreadsheets that people send around are built for reading, not for analysis: one row per department and one column per month, or one row per product and one column per store. These cross-tab or wide tables look nice, but they are awkward to filter, impossible to feed correctly into a PivotTable, and they grow sideways every month. Power Query's Unpivot turns them into long, tidy tables with one row per fact. Pivot Column does the reverse when you really need a wide layout.
Wide versus long
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Department | Jan | Feb | Mar |
| 2 | Marketing | 12500 | 13800 | 11900 |
| 3 | Sales | 48200 | 51000 | 49750 |
| 4 | IT | 22100 | 22100 | 23400 |
Before: a cost budget in wide format. Adding April means adding a column, and a PivotTable sees three separate fields Jan, Feb and Mar.
| A | B | C | |
|---|---|---|---|
| 1 | Department | Month | Amount |
| 2 | Marketing | Jan | 12500 |
| 3 | Marketing | Feb | 13800 |
| 4 | Marketing | Mar | 11900 |
| 5 | Sales | Jan | 48200 |
| 6 | Sales | Feb | 51000 |
| 7 | Sales | Mar | 49750 |
| 8 | IT | Jan | 22100 |
| 9 | IT | Feb | 22100 |
After (first 8 of 9 rows): the same data in long format. Month is now a field you can filter, chart or put on columns in a PivotTable, and April simply adds rows.
The rule of thumb for analysis tables: every column is one variable, every row is one observation. Month is a variable, so it belongs in a column of its own, not spread across headers.
Unpivot step by step
- Load the budget with Data › From Table/Range and open the editor.
- Select the column(s) that identify a row and must stay as they are, here
Department. - Choose Transform › Unpivot Columns › Unpivot Other Columns (also on the header right-click menu).
- Power Query creates two columns:
Attribute(the old headers) andValue(the numbers). Double-click to rename themMonthandAmount. - Set the types:
Amountto Decimal Number or Currency;Monthstays text for now.
= Table.UnpivotOtherColumns(#"Changed Type", {"Department"}, "Month", "Amount")Three unpivot commands
| Command | You select | M function | When a new month column arrives |
|---|---|---|---|
| Unpivot Other Columns | The ID columns to keep | Table.UnpivotOtherColumns | It is unpivoted automatically |
| Unpivot Columns | The month columns | Table.UnpivotOtherColumns (with the other columns) | It is unpivoted automatically |
| Unpivot Only Selected Columns | The month columns | Table.Unpivot | It is left as a column |
Surprisingly, Unpivot Columns also writes Table.UnpivotOtherColumns: Power Query records the columns you did not select, so the query keeps working when April appears. Use Unpivot Only Selected Columns only when you deliberately want a fixed list.
Turning month names into dates
A Month column with Jan, Feb, Mar sorts alphabetically (Feb, Jan, Mar). Convert it to a real date: Add Column › Custom Column with the formula below, then set the type to Date. With a real date you can use the date tools from Dates, durations and a calendar table.
= Table.AddColumn(#"Unpivoted Other Columns", "Month start", each Date.FromText("1 " & [Month] & " 2026", [Culture="en-GB"]), type date)Unpivoting a report with two header rows
A harder case: a sales report where the first header row holds the store and the second the measure.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Athens | Athens | Lisbon | Lisbon | |
| 2 | Product | Units | Revenue | Units | Revenue |
| 3 | Router X2 | 40 | 3960 | 25 | 2475 |
| 4 | Switch 8P | 18 | 1062 | 30 | 1770 |
Before: two header rows. Neither row alone makes unique column names.
- Import without promoting headers (delete the Promoted Headers step if Power Query added it).
- Choose Transform › Transpose. Rows become columns: now the store and measure are in the first two columns.
- Select the first column and choose Transform › Fill › Down to repeat
AthensandLisbon. - Select the first two columns and choose Transform › Merge Columns with separator
|. - Transform › Transpose again, then Home › Use First Row as Headers. Headers are now
Athens|Units,Athens|Revenueand so on; the first one isProductor|Product, depending on how the empty cell was merged. - Make sure the first column is named
Product, select it and choose Unpivot Other Columns. - Split
Attributeby the delimiter|intoStoreandMeasure.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Product | Store | Measure | Value |
| 2 | Router X2 | Athens | Units | 40 |
| 3 | Router X2 | Athens | Revenue | 3960 |
| 4 | Router X2 | Lisbon | Units | 25 |
| 5 | Router X2 | Lisbon | Revenue | 2475 |
| 6 | Switch 8P | Athens | Units | 18 |
After (first rows): one row per product, store and measure. One last step, Pivot Column on Measure, brings Units and Revenue back as two columns.
Pivot Column
Transform › Pivot Column takes the distinct values of one column and turns them into new column headers, filling them from a values column. Select the column whose values become headers, then choose the command.
- Values Column: the column that supplies the numbers.
- Advanced options › Aggregate Value Function: what to do when several rows end up in the same cell: Sum, Count (All), Count (Not Blank), Minimum, Maximum, Median, Average, or Don't Aggregate.
Continuing the example above: select Measure, choose Pivot Column, Values Column Value, and keep Sum. Each product and store gets one row with Units and Revenue side by side, which is the ideal shape for calculating average price per store.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Product | Store | Units | Revenue |
| 2 | Router X2 | Athens | 40 | 3960 |
| 3 | Router X2 | Lisbon | 25 | 2475 |
| 4 | Switch 8P | Athens | 18 | 1062 |
| 5 | Switch 8P | Lisbon | 30 | 1770 |
After Pivot Column: one row per product and store, measures as columns.
= Table.Pivot(#"Split Column by Delimiter", List.Distinct(#"Split Column by Delimiter"[Measure]), "Measure", "Value", List.Sum)Don't Aggregate: key-value lists
Some systems export records as attribute–value pairs, for example an HR system that lists each field on its own row. Pivoting with Don't Aggregate rebuilds the record.
| A | B | C | |
|---|---|---|---|
| 1 | Emp ID | Field | Value |
| 2 | E-210 | Name | Sofia Marin |
| 3 | E-210 | Department | Legal |
| 4 | E-210 | Grade | B2 |
| 5 | E-305 | Name | Omar Haddad |
| 6 | E-305 | Department | IT |
| 7 | E-305 | Grade | C1 |
Before: a key-value export.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Emp ID | Name | Department | Grade |
| 2 | E-210 | Sofia Marin | Legal | B2 |
| 3 | E-305 | Omar Haddad | IT | C1 |
After: Pivot Column on Field, values from Value, Don't Aggregate.
Emp ID and Field.Pivot in Power Query or in a PivotTable?
If the end result is a report for people to read, load the long table and let a PivotTable (see PivotTables: summarise thousands of rows in seconds) or PIVOTBY do the pivoting: users can then change the layout themselves. Pivot inside Power Query when the next step needs columns side by side (a calculation between Units and Revenue, or a file layout someone else requires).
Check yourself
1. You select Department and choose Unpivot Other Columns. What happens when an Apr column appears next month?
Unpivot Other Columns lists only the columns to keep, so new columns are unpivoted.
2. Which command writes Table.Unpivot with a fixed list of columns?
Only Unpivot Only Selected Columns records the selected columns themselves.
3. What does Unpivot do with null values?
Nulls produce no row in the unpivoted output.
4. A key-value export has two Grade rows for E-305. You pivot with Don't Aggregate. What do you get?
Don't Aggregate cannot put two values in one cell.
5. Why is long format better for PivotTables?
One column per variable lets the PivotTable treat Month as a field.