Table.UnpivotOtherColumns Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The Table.UnpivotOtherColumns function in Power Query turns a wide table into a long one. You tell it which columns to keep as they are, and it turns every other column into rows, with one column for the old header and one for the value.

Since you name the columns to keep and not the ones to unpivot, a new month or week column that shows up in your data later gets unpivoted too. You don’t have to change the query.

In this article, I’ll show you how to use it with practical examples, from a basic unpivot to handling blank cells, setting data types, and splitting the headers after you unpivot.

Follow along with the example file

Table.UnpivotOtherColumns Function Examples.xlsx

Download

Table.UnpivotOtherColumns Function Syntax

= Table.UnpivotOtherColumns(table as table, pivotColumns as list, attributeColumn as text, valueColumn as text) as table
  • table – the table you want to unpivot. This is usually the previous step, such as Source.
  • pivotColumns – a list of the columns you want to keep as they are, such as {"Region"}. Every column that’s not in this list gets unpivoted. Column names are case-sensitive, and each one must exist in the table.
  • attributeColumn – the name of the new column that holds the old column headers, such as "Month".
  • valueColumn – the name of the new column that holds the values that were under those headers, such as "Sales".

What it returns – a table with the columns you kept, followed by the attribute column and the value column. Each source row becomes one row for every unpivoted column that has a value in it.

I’ve tested the Table.UnpivotOtherColumns function in Power Query in Excel for Microsoft 365. It’s also part of the Power Query M language that Power BI uses.

When to Use the Table.UnpivotOtherColumns Function

Use this function when you need to:

  • Turn month, week, or year columns into rows so you can use the data in a Pivot Table or a chart
  • Unpivot a table that keeps getting new columns, without editing the query each time
  • Turn a sign-up grid or checklist into a simple list of who picked what
  • Unpivot a cross-tab report where the headers hold two pieces of information, like “Jan Target”
  • Keep a few identifier columns (like an ID and a name) and unpivot everything else

Example 1: Unpivot Month Columns into Rows

Let’s start with the most common use, turning a column for each month into rows.

Below is an Excel table named ClassBookings with the number of bookings for each fitness class in January, February, and March, and I want a table with one row for each class and month.

ClassBookings table with a Class column and one column each for Jan, Feb and Mar bookings

To follow along, select any cell in the table and go to Data > From Table/Range to open it in the Power Query Editor. If Power Query adds a Changed Type step, use #"Changed Type" in place of Source in the formulas below.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.UnpivotOtherColumns(Source,{"Class"},"Month","Bookings")

Result: a table with the Class, Month, and Bookings columns, and 12 rows

Class, Month and Bookings columns with 12 rows, one per class and month

Here, {"Class"} tells the function to keep the Class column as it is. The Jan, Feb, and Mar columns are the “other” columns, so they get unpivoted.

The old headers (Jan, Feb, Mar) go into a new column called Month, and the numbers under them go into a column called Bookings. Since there are 4 classes and 3 months, you get 12 rows.

The rows come out in the same order as the source. You get all the months for Yoga first, then all the months for Spin, and so on.

The last two arguments are just the names of the new columns, so you can call them whatever you like.

Both are required, and if you type "Attribute" and "Value", you get the same column names that the Unpivot Other Columns option in the ribbon gives you.

This long shape is what Pivot Tables and charts work best with. If you want to see how to do the same thing with the menu options, I’ve covered that in this tutorial on how to unpivot data in Excel.

Example 2: Keep More Than One Column

Most real tables have more than one column that identifies a row, like an ID and a name. You can keep all of them by listing them in the second argument.

Below is an Excel table named ExamScores with each student’s ID, name, and score in three subjects, and I want one row for each student and subject.

ExamScores table with Student ID, Student and Math, Science and English scores

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.UnpivotOtherColumns(Source,{"Student ID","Student"},"Subject","Score")

Result: a table with the Student ID, Student, Subject, and Score columns, and 12 rows

Student ID, Student, Subject and Score columns with 12 rows

Here, both Student ID and Student are in the list, so both stay as they are and repeat on every row for that student. The Math, Science, and English columns get unpivoted into the Subject and Score columns.

The columns you keep must exist with exactly that name. If someone renames Student to “Student Name” in the source, the query stops with the error “The column ‘Student’ of the table wasn’t found.”

The same thing happens if the case doesn’t match. Writing {"student"} gives you “The column ‘student’ of the table wasn’t found.”

The new column names can’t match a column you’re keeping. If you use "Student" as the attribute column name here, you get the error “The field ‘Student’ already exists in the record.”

And you have to leave at least one column to unpivot. If you list every column of the table as one to keep, you get the error “There weren’t enough elements in the enumeration to complete the operation.”

Example 3: New Columns Get Unpivoted Automatically

This example shows why you’d pick Table.UnpivotOtherColumns over listing the columns yourself. When your source gets a new column, the query picks it up on its own.

Below is an Excel table named StoreVisitors with the weekly visitor count for three stores. I built the query when the table had three weeks, and then the Week 4 column was added.

StoreVisitors table with Store and Week 1 to Week 4 visitor counts

First, let’s see what happens when you name the columns to unpivot instead. The Table.Unpivot function works the other way around, and you give it the list of columns to unpivot:

= Table.Unpivot(Source,{"Week 1","Week 2","Week 3"},"Week","Visitors")

Result: Week 4 stays as a separate column, and only Week 1 to Week 3 are unpivoted

Table.Unpivot result where Week 4 stays as its own column and only Weeks 1 to 3 become rows

Here, the list was written when the table had three weeks, so Week 4 isn’t in it. It stays as a separate column and repeats its value on every row for that store.

That breaks any summary you build on it. Week 4 never shows up in the Week column, and if you add up the Week 4 column, each store’s number gets counted three times.

Now use Table.UnpivotOtherColumns instead, and name only the column you want to keep:

= Table.UnpivotOtherColumns(Source,{"Store"},"Week","Visitors")

Result: a table with the Store, Week, and Visitors columns, with all four weeks unpivoted

Store, Week and Visitors columns with all four weeks unpivoted into 12 rows

Here, the formula only says “keep Store”, so every other column gets unpivoted. That includes Week 4, and it will include Week 5 and Week 6 when they’re added.

Pro Tip: If your source gets a new column every week or month, list the few columns that stay fixed and let Table.UnpivotOtherColumns unpivot the rest. Then a refresh is all you need when new data arrives.

Example 4: Turn a Sign-Up Grid into a List

Table.UnpivotOtherColumns skips blank cells. Each blank (null) value in an unpivoted column simply doesn’t get a row, and that’s very handy when you have a grid that’s mostly empty.

Below is an Excel table named ShiftSignups with the shift each volunteer picked on Friday, Saturday, and Sunday, and I want a list of only the shifts people signed up for.

ShiftSignups grid with each volunteer's Fri, Sat and Sun shift, with null cells where no shift was picked

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.UnpivotOtherColumns(Source,{"Volunteer"},"Day","Shift")

Result: a table with the Volunteer, Day, and Shift columns, and 8 rows

Volunteer, Day and Shift list with 8 rows; Audrey Ross does not appear

Here, the grid has 15 cells under the three day columns, but only 8 of them have a shift. The 7 blank cells don’t produce a row, so you get a clean list of the shifts that were picked.

Audrey Ross didn’t pick any shift, so all three of her cells are blank. That means she doesn’t appear in the result at all.

Follow along with the example file

Table.UnpivotOtherColumns Function Examples.xlsx

Download

Example 5: Keep Blank Cells as Zero

Skipping blanks isn’t always what you want. If a blank means zero, dropping it can hide a row you need.

Below is an Excel table named DailyOrders with the number of orders for three products from Monday to Thursday, where a blank cell means no orders that day. I want a row for every product and every day.

DailyOrders table with Mon to Thu order counts and null cells, including an all-null Planner row

Let’s first see what a plain unpivot gives you:

= Table.UnpivotOtherColumns(Source,{"Product"},"Day","Orders")

Result: a table with only 5 rows, and no rows at all for Planner

Plain unpivot result with only 5 rows and no Planner rows

Here, the blank cells are skipped, just like in the previous example. Planner had no orders all week, so it disappears from the result, and Notebook and Pen Set lose their zero days.

If you later count the products or average the daily orders, you’d get the wrong answer. The fix is to replace the blanks with 0 before you unpivot.

If you added the plain unpivot step above, delete it first (right-click it in the Applied Steps pane and choose Delete), so the next step starts from Source again.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula, which replaces the blank cells in every column except Product with 0:

= Table.ReplaceValue(Source,null,0,Replacer.ReplaceValue,List.RemoveItems(Table.ColumnNames(Source),{"Product"}))

Power Query names a step added this way Custom1, so right-click it in the Applied Steps pane, choose Rename, and call it Replaced Value. The next formula refers to it by that name.

Then add another step and unpivot the result of that step:

= Table.UnpivotOtherColumns(#"Replaced Value",{"Product"},"Day","Orders")

Result: a table with 12 rows, one for each product and day, with 0 where the cell was blank

12 rows, one per product and day, with 0 where the cell was blank

Here, the Table.ReplaceValue function turns every null in the day columns into 0. Now there are no blank cells left, so every cell becomes a row, and Planner shows up with 0 orders on each day.

Table.ColumnNames(Source) gives the list of all the column names, and List.RemoveItems takes Product out of it. So the replacement covers every day column, including ones added later.

Example 6: Set the Data Types After Unpivoting

When you unpivot, the old headers always come out as text. And unless all the unpivoted columns had the same data type, the value column gets the “any” type (shown as ABC123 in the column header).

Below is an Excel table named CourseEnrollment with the number of students in each course for 2023, 2024, and 2025, and I want a table with Year and Students as proper whole numbers.

CourseEnrollment table with Course and the 2023, 2024 and 2025 student counts

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.UnpivotOtherColumns(Source,{"Course"},"Year","Students")

Rename this step to Unpivoted Other Columns (right-click Custom1 in the Applied Steps pane and choose Rename), because the next formula refers to it by that name.

Then add another step to set the data type of each column:

= Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Course",type text},{"Year",Int64.Type},{"Students",Int64.Type}})

Result: a table with Course as text, and Year and Students as whole numbers

Course as text and Year and Students as whole numbers after the Changed Type step

Here, the first step gives you a Year column with the text “2023”, “2024”, and “2025”, because those were column headers.

The year columns read through Source have no data type yet, so Students comes out as any too.

The second step uses the Table.TransformColumnTypes function to make Year and Students whole numbers, so you can sort and filter by year correctly.

If you loaded the table with From Table/Range and kept the Changed Type step, your three year columns are already whole numbers. Then the Students column comes out as a whole number on its own, but Year is still text and still needs converting.

Example 7: Split the Headers After Unpivoting

Sometimes a column header holds two pieces of information, like “Jan Target” (a month and a measure). After you unpivot, you can split that column into two.

Below is an Excel table named RepTargets with each sales rep’s target and actual sales for January and February, and I want separate Month and Measure columns.

RepTargets table with Rep and Jan Target, Jan Actual, Feb Target and Feb Actual columns

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.UnpivotOtherColumns(Source,{"Rep"},"Attribute","Amount")

Rename this step to Unpivoted Other Columns (right-click Custom1 in the Applied Steps pane and choose Rename), because the next formula refers to it by that name.

Then add another step that splits the Attribute column at the space:

= Table.SplitColumn(#"Unpivoted Other Columns","Attribute",Splitter.SplitTextByDelimiter(" "),{"Month","Measure"})

Result: a table with the Rep, Month, Measure, and Amount columns, and 12 rows

Rep, Month, Measure and Amount columns with 12 rows

Here, the first step gives you an Attribute column with values like “Jan Target” and “Feb Actual”. The second step uses the Table.SplitColumn function to split each of those at the space, so “Jan” goes into Month and “Target” goes into Measure.

Now you can filter by month or by measure, or put Target and Actual side by side in a Pivot Table.

Example 8: Keep the First Columns by Position

All the examples so far typed the names of the columns to keep. But what if those names change from one file to the next?

Below is an Excel table named CustomerSpend with a customer ID, the customer name, and the spend for three months. The first two columns are always the customer details, but their headers aren’t always the same (one export says “Cust ID”, another says “Customer ID”).

CustomerSpend table with Cust ID, Customer and Jan, Feb and Mar spend

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.UnpivotOtherColumns(Source,List.FirstN(Table.ColumnNames(Source),2),"Month","Spend")

Result: a table with the Cust ID, Customer, Month, and Spend columns, and 9 rows

Cust ID, Customer, Month and Spend columns with 9 rows

Here, Table.ColumnNames(Source) gives the list of all the column names, and List.FirstN(...,2) takes the first two. Whatever those two columns are called, they’re the ones that stay, and everything after them gets unpivoted.

This only works if the columns to keep are always the first two. If someone moves a month column to the front, the formula keeps that month column and unpivots the customer name instead, and you get no error.

So use this only when the column order is reliable.

If the names are reliable but the order isn’t, type the names instead, as in Example 2.

Other Excel Articles You May Also Like:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.