Table.Pivot 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.Pivot function in Power Query turns a long table into a wide one. It takes the values in one column, makes each of them a new column header, and fills those columns with the values from another column.

In this article, I’ll show you how to use it with practical examples, from a basic pivot to handling duplicate values, counting rows, combining text, and fixing the cases where you get blank cells instead of numbers.

Follow along with the example file

Table.Pivot Function Examples.xlsx

Download

Table.Pivot Function Syntax

= Table.Pivot(table as table, pivotValues as list, attributeColumn as text, valueColumn as text, optional aggregationFunction as nullable function) as table
  • table – the table you want to pivot. This is usually the previous step, such as Source.
  • pivotValues – a list of the values from the attribute column that should become new columns, such as {"Small","Medium","Large"}. The new columns appear in the same order as this list.
  • attributeColumn – the name of the column whose values become the new column headers, such as "Size".
  • valueColumn – the name of the column whose values fill the new columns, such as "Cups".
  • aggregationFunction (optional) – a function that combines the values when more than one row lands in the same cell, such as List.Sum or List.Count.

What it returns – a table with one row for each unique combination of the remaining columns (every column other than the attribute and value columns), and one new column for each item in pivotValues.

I’ve tested the Table.Pivot 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.Pivot Function

Use this function when you need to:

  • Turn a long list (like product, size, and quantity) into a grid with one column for each size
  • Build a summary table with a column for each month, category, or status
  • Add up or count values for each combination of a row label and a column label
  • Turn attribute-value pairs (like Color = Black, Wattage = 9W) into one row per item
  • Put several text values from the same group into one cell, like all the names signed up for a shift

Example 1: Turn Sizes into Columns

Let’s start with a basic example where every row label and column label pair shows up exactly once.

Below is an Excel table named CoffeeSales with the number of cups sold for each drink in three sizes, and I want one row for each drink with a separate column for each size.

CoffeeSales table with Drink, Size and Cups, one row per drink and size

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.Pivot(Source,{"Small","Medium","Large"},"Size","Cups")

Result: a table with the Drink, Small, Medium, and Large columns, and 3 rows

Drink, Small, Medium and Large columns with one row per drink

Here, "Size" is the column whose values become the new headers, and "Cups" is the column whose numbers fill them. The list {"Small","Medium","Large"} says which sizes to turn into columns, and in what order.

Drink is the only other column, so Table.Pivot makes one row for each drink. You get 3 rows (one per drink) instead of the 9 you started with.

Notice that the rows come out sorted A to Z by drink (Cold Brew, Latte, Mocha), and not in the order they were in the source. If the order matters to you, add a Table.Sort step after the pivot.

Table.Pivot does the opposite of the Table.UnpivotOtherColumns function, which turns columns back into rows.

Example 2: Missing Combinations Show Up as null

In real data, not every row label has a value for every column label. Table.Pivot handles that without any error.

Below is an Excel table named ExamScores with each student’s score in Math, Science, and English. Emma didn’t take the Science test and Sarah didn’t take the Math test, and I want one row per student with a column for each subject.

ExamScores table with Student, Subject and Score; Emma has no Science row and Sarah no Math row

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

= Table.Pivot(Source,{"Math","Science","English"},"Subject","Score")

Result: a table with one row per student, with null in Emma’s Science cell and Sarah’s Math cell

Student, Math, Science and English columns with null in Emma's Science and Sarah's Math cells

Here, there’s no row for Emma and Science in the source, so that cell has nothing to fill it and shows null. The same goes for Sarah and Math.

When you load this to a worksheet, the null cells come through as blank cells. If you’d rather see 0, you can replace the nulls in the subject columns with the Table.ReplaceValue function after the pivot.

Example 3: Create Columns Automatically with List.Distinct

In the first two examples, I typed the list of new columns. That works, but the list doesn’t update when your data gets a new value.

Below is an Excel table named WebsiteVisits with the number of visits from each channel for January, February, and March, and I want a column for each month without typing the month names.

WebsiteVisits table with Channel, Month and Visits for Jan to Mar

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

= Table.Pivot(Source,List.Distinct(Source[Month]),"Month","Visits")

Result: a table with the Channel, Jan, Feb, and Mar columns, and 3 rows

Channel, Jan, Feb and Mar columns created from List.Distinct

Here, Source[Month] gives you all the values in the Month column as a list, and the List.Distinct function removes the repeats. That leaves {"Jan","Feb","Mar"}, in the order they first show up in the data.

When you add April rows to the source table and refresh, List.Distinct picks up “Apr” and you get a new Apr column. You don’t have to edit the formula.

A typed list works differently. Any value in the attribute column that’s not in your list gets left out, with no error, so new months would quietly go missing from the result.

Pro Tip: If you want the new columns in alphabetical order, wrap the list in List.Sort, like List.Sort(List.Distinct(Source[Month])). For month names, keep List.Distinct alone, since sorting would put Feb before Jan.

Example 4: Fix the “Too Many Elements” Error with List.Sum

This is a common error with Table.Pivot, and it happens when two or more rows land in the same cell of the result.

Below is an Excel table named ExpenseClaims with each employee’s expense claims by category. Megan has two Travel claims and Lillian has two Meals claims, and I want the total for each employee in each category.

ExpenseClaims table with two Travel claims for Megan and two Meals claims for Lillian

Let’s first see what happens with a plain pivot:

= Table.Pivot(Source,{"Travel","Meals","Office"},"Category","Amount")

Result: Error in Megan’s Travel cell and Lillian’s Meals cell, while the other cells show their values

Plain pivot result with Error in Megan's Travel and Lillian's Meals cells

Here, Megan has two Travel amounts (120 and 95), but there’s only one cell for Megan and Travel. Table.Pivot doesn’t know which one to keep, so that cell becomes an error.

If you click on the Error cell, you’ll see the message “There were too many elements in the enumeration to complete the operation.” The cells with a single value are fine, which makes this easy to miss in a large table.

When you load the query to a worksheet, the error cells come through as blank cells, so you could end up with wrong totals and not notice. The fix is to tell Table.Pivot how to combine the values, using its optional fifth argument (aggregationFunction).

If you added the plain pivot 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:

= Table.Pivot(Source,{"Travel","Meals","Office"},"Category","Amount",List.Sum)

Result: a table with each employee’s total for each category, with 215 for Megan’s Travel and 40 for Lillian’s Meals

List.Sum result with 215 for Megan's Travel and 40 for Lillian's Meals

Here, List.Sum is the aggregation function. When more than one value lands in a cell, Table.Pivot passes all of them to List.Sum as a list and puts the total in the cell, so Megan’s Travel becomes 120 + 95 = 215.

You can use any function that takes a list and returns one value, such as List.Average, List.Max, or List.Min. Note that you write the function name without parentheses, because you’re passing the function itself and not calling it.

Follow along with the example file

Table.Pivot Function Examples.xlsx

Download

Example 5: Count Rows with List.Count

Sometimes you don’t want to add anything up. You just want to count how many rows fall in each cell, like the number of survey responses for each rating.

Below is an Excel table named SurveyResponses with a response ID, the department of the person who responded, and the rating they gave. I want a count of Good, Okay, and Poor ratings for each department.

SurveyResponses table with Response ID, Department and Rating

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

= Table.Pivot(Source,{"Good","Okay","Poor"},"Rating","Response ID",List.Count)

Result: a table with the Department, Good, Okay, and Poor columns, with the number of responses in each cell

Department, Good, Okay and Poor columns with the count of responses, 0 where there are none

Here, Rating is the attribute column, and Response ID is the value column. The List.Count function counts the response IDs that land in each cell, so Support gets 3 for Good because three Support responses gave a Good rating.

Since Rating and Response ID are both used, Department is the only column left, and it becomes the row label.

Finance and Support have no Poor ratings, so those cells show 0. With List.Count, a combination with no rows gives 0 and not null, which is what you want in a count table.

Example 6: Pivot Text Values into Columns

Table.Pivot also works when the value column holds text, and you don’t need an aggregation function as long as each cell gets only one value.

Below is an Excel table named ProductSpecs with product details stored as pairs, the name of the attribute in one column and its value in the next. I want one row for each SKU with Color, Wattage, and Warranty as columns.

ProductSpecs table with SKU, Attribute and Value pairs

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

= Table.Pivot(Source,{"Color","Wattage","Warranty"},"Attribute","Value")

Result: a table with the SKU, Color, Wattage, and Warranty columns, with null in the Warranty cell for LMP-03

SKU, Color, Wattage and Warranty columns with null in LMP-03's Warranty

Here, each attribute name in the Attribute column becomes a column header, and the text in the Value column fills it. You see this attribute-value shape a lot in data exported from databases and online forms.

LMP-03 has no Warranty row in the source, so its Warranty cell is null, just like the missing scores in Example 2.

Example 7: Combine Text Values with Text.Combine

What if more than one text value lands in the same cell? List.Sum won’t help with text, but you can write your own aggregation function that joins the values together.

Below is an Excel table named VolunteerShifts with the volunteers who signed up for the Morning and Afternoon shifts on Saturday and Sunday, and I want each shift’s names in one cell.

VolunteerShifts table with Day, Shift and Volunteer

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

= Table.Pivot(Source,{"Morning","Afternoon"},"Shift","Volunteer",each Text.Combine(_,", "))

Result: a table with the Day, Morning, and Afternoon columns, with “Connor, Tammy, Gregory” in Sunday’s Afternoon cell

Day, Morning and Afternoon columns with the volunteer names joined by commas

Here, each Text.Combine(_,", ") is a small function that Table.Pivot runs for every cell. The underscore stands for the list of names that landed in that cell, and the Text.Combine function joins them with a comma and a space.

The each keyword is what turns that expression into a function, so Table.Pivot can call it for every cell. That’s why the parentheses are fine here, while List.Sum in Example 4 had none.

The names come out in the same order as the rows in the source. A cell with only one volunteer, like Saturday’s Afternoon shift, just shows that one name.

Example 8: Fix Blank Columns When the Headers Are Numbers

This one is easy to miss because there’s no error. You get the right columns, but every cell is null.

Below is an Excel table named LibraryMembers with the number of members at each library branch in 2025 and 2026, and I want a column for each year.

LibraryMembers table with Branch, Year as numbers, and Members

Let’s first try a plain pivot with the years as the new columns:

= Table.Pivot(Source,{"2025","2026"},"Year","Members")

Result: a table with the Branch, 2025, and 2026 columns, with null in every year cell

Plain pivot result with 2025 and 2026 columns full of null

Here, the values in the Year column are numbers, but the list {"2025","2026"} has text. The number 2025 doesn’t match the text “2025”, so no value lands in any cell.

If you opened the table with From Table/Range, its Changed Type step makes Year a whole number too, so you’ll see the same thing there.

You can spot this in the source table, where the years sit on the right side of the cell like other numbers. Text would sit on the left.

You might think of typing the list as numbers instead, like {2025,2026}. That gives you an error like “We cannot convert the value 2025 to type Text.” because column names must be text.

The fix is to change the Year column to text before you pivot. If you added the plain pivot step above, delete it first (right-click it in the Applied Steps pane and choose Delete).

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

= Table.TransformColumnTypes(Source,{{"Year",type text}})

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

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

= Table.Pivot(#"Year to Text",{"2025","2026"},"Year","Members")

Result: a table with the Branch, 2025, and 2026 columns, with the member counts filled in

Pivot after converting Year to text, with the member counts filled in

Here, the Table.TransformColumnTypes function turns 2025 and 2026 into the text “2025” and “2026”. Now they match the items in the list, and each member count lands in the right cell.

Example 9: Remove Extra Columns Before You Pivot

Table.Pivot groups the rows by every column other than the attribute and value columns. So if your table has a column you didn’t think about, like a unique ID, it changes the result.

Below is an Excel table named Timesheet with the hours each employee worked from Monday to Wednesday, where every row also has its own Entry ID. I want one row per employee with a column for each day.

Timesheet table with Entry ID, Employee, Day and Hours

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

= Table.Pivot(Source,{"Mon","Tue","Wed"},"Day","Hours")

Result: a table with 6 rows, one for each Entry ID, with only one day filled in on each row

Plain pivot result with 6 rows, one per Entry ID, and one day filled per row

Here, Entry ID is one of the remaining columns, so Table.Pivot uses it to group the rows. Every Entry ID is different, so every source row stays its own row, and you get a staircase of values with null everywhere else.

The fix is to remove the Entry ID column first. If you added the plain pivot step above, delete it first (right-click it in the Applied Steps pane and choose Delete).

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

= Table.RemoveColumns(Source,{"Entry ID"})

Rename this step to Removed 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 and pivot the result of that step:

= Table.Pivot(#"Removed Columns",{"Mon","Tue","Wed"},"Day","Hours")

Result: a table with the Employee, Mon, Tue, and Wed columns, and 2 rows

Pivot after removing Entry ID, with one row each for Blake and Troy

Here, the Table.RemoveColumns function drops Entry ID, so Employee is the only column left to group by. Now Blake and Troy each get one row with their hours for all three days.

Before you pivot any table, check which columns are left besides the attribute and value columns. Every one of them becomes part of the row label, so remove any you don’t want to group by.

If new columns could be added to your source later, the Table.SelectColumns function is the sturdier choice. Keep only Employee, Day, and Hours with it, and any column added later gets left out automatically.

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.