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.

Intermediate 16 min read

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

ABCD
1DepartmentJanFebMar
2Marketing125001380011900
3Sales482005100049750
4IT221002210023400

Before: a cost budget in wide format. Adding April means adding a column, and a PivotTable sees three separate fields Jan, Feb and Mar.

ABC
1DepartmentMonthAmount
2MarketingJan12500
3MarketingFeb13800
4MarketingMar11900
5SalesJan48200
6SalesFeb51000
7SalesMar49750
8ITJan22100
9ITFeb22100

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

  1. Load the budget with Data › From Table/Range and open the editor.
  2. Select the column(s) that identify a row and must stay as they are, here Department.
  3. Choose Transform › Unpivot Columns › Unpivot Other Columns (also on the header right-click menu).
  4. Power Query creates two columns: Attribute (the old headers) and Value (the numbers). Double-click to rename them Month and Amount.
  5. Set the types: Amount to Decimal Number or Currency; Month stays text for now.
Power Query M
= Table.UnpivotOtherColumns(#"Changed Type", {"Department"}, "Month", "Amount")
The step lists the columns to keep, not the ones to unpivot.

Three unpivot commands

CommandYou selectM functionWhen a new month column arrives
Unpivot Other ColumnsThe ID columns to keepTable.UnpivotOtherColumnsIt is unpivoted automatically
Unpivot ColumnsThe month columnsTable.UnpivotOtherColumns (with the other columns)It is unpivoted automatically
Unpivot Only Selected ColumnsThe month columnsTable.UnpivotIt 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.

Tip. Unpivot also removes nulls: a department with no value for March simply has no March row. That is usually what you want; if you need a row with 0, replace nulls with 0 before unpivoting.

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.

Power Query M
= Table.AddColumn(#"Unpivoted Other Columns", "Month start", each Date.FromText("1 " & [Month] & " 2026", [Culture="en-GB"]), type date)
Builds the text '1 Jan 2026' and converts it with an English culture; the year would normally come from the file or a parameter.

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.

ABCDE
1AthensAthensLisbonLisbon
2ProductUnitsRevenueUnitsRevenue
3Router X2403960252475
4Switch 8P181062301770

Before: two header rows. Neither row alone makes unique column names.

  1. Import without promoting headers (delete the Promoted Headers step if Power Query added it).
  2. Choose Transform › Transpose. Rows become columns: now the store and measure are in the first two columns.
  3. Select the first column and choose Transform › Fill › Down to repeat Athens and Lisbon.
  4. Select the first two columns and choose Transform › Merge Columns with separator |.
  5. Transform › Transpose again, then Home › Use First Row as Headers. Headers are now Athens|Units, Athens|Revenue and so on; the first one is Product or |Product, depending on how the empty cell was merged.
  6. Make sure the first column is named Product, select it and choose Unpivot Other Columns.
  7. Split Attribute by the delimiter | into Store and Measure.
ABCD
1ProductStoreMeasureValue
2Router X2AthensUnits40
3Router X2AthensRevenue3960
4Router X2LisbonUnits25
5Router X2LisbonRevenue2475
6Switch 8PAthensUnits18

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.

ABCD
1ProductStoreUnitsRevenue
2Router X2Athens403960
3Router X2Lisbon252475
4Switch 8PAthens181062
5Switch 8PLisbon301770

After Pivot Column: one row per product and store, measures as columns.

Power Query M
= Table.Pivot(#"Split Column by Delimiter", List.Distinct(#"Split Column by Delimiter"[Measure]), "Measure", "Value", List.Sum)
The second argument lists the new column names; List.Distinct takes them from the data, so new measures become new columns.

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.

ABC
1Emp IDFieldValue
2E-210NameSofia Marin
3E-210DepartmentLegal
4E-210GradeB2
5E-305NameOmar Haddad
6E-305DepartmentIT
7E-305GradeC1

Before: a key-value export.

ABCD
1Emp IDNameDepartmentGrade
2E-210Sofia MarinLegalB2
3E-305Omar HaddadITC1

After: Pivot Column on Field, values from Value, Don't Aggregate.

Watch out. With Don't Aggregate, if two rows have the same Emp ID and Field, the cell becomes an Error with a message about too many elements in the enumeration. That is a data quality signal: find the duplicate with Keep Duplicates on 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?

2. Which command writes Table.Unpivot with a fixed list of columns?

3. What does Unpivot do with null values?

4. A key-value export has two Grade rows for E-305. You pivot with Don't Aggregate. What do you get?

5. Why is long format better for PivotTables?