How-to recipes · Cleaning data
How to unpivot data in Excel with Power Query
Turn a wide table with one column per month into a long list with Power Query's Unpivot Columns, or with TOCOL and HSTACK in Microsoft 365.
Reports are often laid out wide: one row per region and one column per month. That is easy to read but hard to analyse. You cannot filter by month, SUMIFS needs a different column for every month, and a PivotTable sees Jan, Feb and Mar as three unrelated fields. Analysis tools want the data long: one row per region and month, with columns Region, Month and Sales. Turning wide into long is called unpivoting. Power Query does it in a few clicks and repeats it on every refresh.
The example data
Sales by region in A1:D3, with months as column headers. The goal is a six-row list.
Power Query (Excel 2016 and later, Microsoft 365)
- Click inside the data and choose Data > From Table/Range. Confirm that the table has headers; Excel converts the range into a table and opens the Power Query editor.
- Click the header of the column that should stay as it is, here Region. Hold Ctrl to select more if several columns identify a row.
- Go to Transform > Unpivot Columns > Unpivot Other Columns.
- Power Query creates two columns, Attribute (the old headers: Jan, Feb, Mar) and Value (the numbers). Double-click the headers and rename them to Month and Sales.
- Check the data types: click the icon left of each header and set Sales to Whole Number or Decimal.
- Choose Home > Close & Load. The long list appears in a new sheet as a table.
Use Unpivot Other Columns rather than selecting the month columns and choosing Unpivot Columns. The Other version keeps working when April is added next month, because it unpivots every column except Region. When the source changes, right-click the result and choose Refresh. Power Query is introduced in Power Query basics: import, clean, append, merge, refresh.
Microsoft 365: a formula alternative
If you prefer a live formula without a query, TOCOL can flatten each part of the table into a column, and HSTACK puts the three columns side by side. Regions and months must be repeated to the size of the value block; IF with a SEQUENCE does that broadcasting:
| A | B | C | D | E | F | G | H | |
|---|---|---|---|---|---|---|---|---|
| 1 | Region | Jan | Feb | Mar | ||||
| 2 | North | 120 | 135 | 150 | North | Jan | 120 | |
| 3 | South | 95 | 110 | 105 | North | Feb | 135 | |
| 4 | North | Mar | 150 | |||||
| 5 | South | Jan | 95 | |||||
| 6 | South | Feb | 110 | |||||
| 7 | South | Mar | 105 |
Six rows: every region with every month and its value, read row by row.
IF(SEQUENCE(,3),r)repeats the region column across three columns, giving a 2×3 block of region names.IF(SEQUENCE(2),c)repeats the month header down two rows, giving a 2×3 block of month names.- TOCOL reads each 2×3 block row by row into a single column, so the three columns line up.
- LET names the ranges so the formula is easier to adapt.
The formula updates instantly when numbers change, but you must extend the ranges when months are added. For large or regularly imported data, Power Query is the better tool.
Excel 2010 and 2013
Power Query is available as a free Microsoft add-in for Excel 2010 and 2013, with the same Unpivot command. Without it, the old trick is the PivotTable and PivotChart Wizard (Alt, D, P) with Multiple consolidation ranges: create a PivotTable from the wide data and double-click the grand total; Excel writes the long list to a new sheet. It works but is clumsy compared with Power Query.
After unpivoting
- A PivotTable with Region in Rows, Month in Columns and Sales in Values rebuilds the wide view, now with filters and totals (PivotTables: summarise thousands of rows in seconds).
- SUMIFS works with one formula for any month:
=SUMIFS(Sales,Region,"North",Month,"Feb"). - Charts can switch between regions and months with a slicer instead of separate series.
Pitfalls
- Totals in the source. A Total column or row in the wide table becomes data after unpivoting and doubles every sum. Remove it in Power Query (Remove Columns, or filter out the Total row) before unpivoting.
- Blank cells are dropped by Unpivot; if a blank means zero, replace nulls with 0 first (Transform > Replace Values).
- Month names as text sort alphabetically (Feb, Jan, Mar). Convert them to real dates in Power Query, or add a month-number column for sorting.
- Merged or multi-row headers must be cleaned up first: promote one header row and fill merged cells down (Transform > Fill > Down).