Table.UnpivotOtherColumns Function (Power Query M)

If you want to reshape a wide table into a long one, Table.UnpivotOtherColumns is the function you reach for. It turns columns into rows, keeping only the columns you name and unpivoting everything else.

Available in Excel (Power Query), Power BI Desktop, and Power BI Service.

Syntax of Table.UnpivotOtherColumns Function

Table.UnpivotOtherColumns(table as table, pivotColumns as list, attributeColumn as text, valueColumn as text) as table

where

  • table (required, table). The source table you want to reshape from wide to long.
  • pivotColumns (required, list). The list of column names to KEEP. Every column NOT in this list gets unpivoted into rows.
  • attributeColumn (required, text). The name for the new column that holds the old column headers.
  • valueColumn (required, text). The name for the new column that holds the old cell values.

Returns: a table reshaped from wide to long. Any cell with a null value is dropped, so it produces no output row.

The one thing to get straight: you name the columns to KEEP, not the ones to unpivot. Power Query handles the rest.

Example 1: Unpivot all the other columns

Start with a small attendance table where each weekday is its own column.

Here is the starting data:

ClassMonTueWed
Yoga12159
Spin201822

Keep Class and unpivot the three day columns into rows.

let
Source = Excel.CurrentWorkbook(){[Name="ClassAttendance"]}[Content],
Unpivoted = Table.UnpivotOtherColumns(Source, {"Class"}, "Day", "Attendees")
in
Unpivoted

The result produces:

ClassDayAttendees
YogaMon12
YogaTue15
YogaWed9
SpinMon20
SpinTue18
SpinWed22

Each source row expands into one row per day column, and Class repeats down those rows.

Example 2: Name the attribute and value columns

The third and fourth arguments are free text, so make them meaningful instead of the default Attribute and Value.

Here is the starting data:

OriginLightMediumDark
Ethiopia402510
Colombia303520

Keep Origin and label the new columns RoastLevel and Kilograms.

let
Source = Excel.CurrentWorkbook(){[Name="RoastBatches"]}[Content],
Unpivoted = Table.UnpivotOtherColumns(Source, {"Origin"}, "RoastLevel", "Kilograms")
in
Unpivoted

The result produces:

OriginRoastLevelKilograms
EthiopiaLight40
EthiopiaMedium25
EthiopiaDark10
ColombiaLight30
ColombiaMedium35
ColombiaDark20

The old headers land in RoastLevel and their values in Kilograms, which reads far better than generic names.

Example 3: Keep two key columns

pivotColumns is a list of any length, so you can hold more than one column in place.

Here is the starting data:

SiteYearQ1Q2Q3Q4
North2024110180200130
South202495160210120

Keep both Site and Year, then unpivot the four quarter columns.

let
Source = Excel.CurrentWorkbook(){[Name="SolarOutput"]}[Content],
Unpivoted = Table.UnpivotOtherColumns(Source, {"Site", "Year"}, "Quarter", "MWh")
in
Unpivoted

The result produces:

SiteYearQuarterMWh
North2024Q1110
North2024Q2180
North2024Q3200
North2024Q4130
South2024Q195
South2024Q2160
South2024Q3210
South2024Q4120

Both Site and Year repeat across the four unpivoted rows for each source row.

Example 4: Null value cells are dropped

When a value cell is blank, no row is created for it.

Here is the starting data:

GenreDowntownUptown
Fiction320280
History150
SciFi210

Keep Genre and unpivot the two branch columns.

let
Source = Excel.CurrentWorkbook(){[Name="BranchCheckouts"]}[Content],
Unpivoted = Table.UnpivotOtherColumns(Source, {"Genre"}, "Branch", "Checkouts")
in
Unpivoted

The result produces:

GenreBranchCheckouts
FictionDowntown320
FictionUptown280
HistoryDowntown150
SciFiUptown210

There are six value cells but only four output rows, because the two blank cells are skipped.

Example 5: Future-proof against new columns

Because you only name the key column, any value column added later is unpivoted automatically.

Here is the starting data:

ParkSpringSummerAutumn
Riverside12003400900
Hilltop8002600700

Keep Park and unpivot whatever season columns exist.

let
Source = Excel.CurrentWorkbook(){[Name="ParkVisitors"]}[Content],
Unpivoted = Table.UnpivotOtherColumns(Source, {"Park"}, "Season", "Visitors")
in
Unpivoted

The result produces:

ParkSeasonVisitors
RiversideSpring1200
RiversideSummer3400
RiversideAutumn900
HilltopSpring800
HilltopSummer2600
HilltopAutumn700

If a Winter column showed up next month, this same step would unpivot it with no edit needed.

Things to keep in mind with Table.UnpivotOtherColumns

  • You name the columns to KEEP, not the ones to unpivot. This is the whole point. It makes the step future-proof: any new column added to the source later gets unpivoted automatically, with no edit to the M. The same is not true of a step that targets columns by name, like one built with Table.AddColumn.
  • Null value cells produce no output row. A blank cell is skipped entirely (Example 4), so the result can have fewer rows than the source rows multiplied by the unpivoted columns.
  • Output order is predictable. For each source row, in row order, you get one row per unpivoted column in left-to-right column order. The kept columns repeat down those rows. If you need a different order afterward, follow this step with Table.Sort.
  • Pick meaningful names for attributeColumn and valueColumn. They are plain text. RoastLevel and Kilograms read better than the default Attribute and Value.
  • Passing an empty list {} unpivots the entire table. With no columns to keep, every header goes into the attribute column and every cell into the value column.

Performance and query folding

Against a foldable source, an unpivot step can sometimes fold to the database, but in practice it often runs locally. On Excel, CSV, or folder sources it always runs locally, which is fine for the typical wide tables you reshape this way.

Common questions about Table.UnpivotOtherColumns

What is the difference between Table.UnpivotOtherColumns and Table.Unpivot?

With Table.Unpivot you list the columns you want to unpivot, so anything not listed stays put. With Table.UnpivotOtherColumns you list the columns to KEEP, and everything else is unpivoted. The second one is the robust choice, because new value columns added later are picked up automatically instead of being ignored.

How do I expand the long table back out again?

Unpivoting collapses your data into rows. If you later need to widen a column that holds nested tables, reach for Table.ExpandTableColumn instead.

List of All Power Query Functions

Related Power Query Functions / Articles:

I am a huge fan of Microsoft Excel and love sharing my knowledge through articles and tutorials. I work as a business analyst and use Microsoft Excel extensively in my daily tasks. My aim is to help you unleash the full potential of Excel and become a data-slaying wizard yourself.