Table.ToRows 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.ToRows function in Power Query turns a table into a list of rows. Each row becomes its own small list that holds that row’s values, in the same order as the columns.

Once each row is a list, you can use all the List functions on it, like counting the filled cells in a row, finding the largest value in a row, or feeding a mapping table into a function that wants a list of pairs.

In this article, I’ll show you how Table.ToRows works with practical examples.

Follow along with the example file

Table.ToRows Function Examples.xlsx

Download

Table.ToRows Function Syntax

= Table.ToRows(table as table) as list
  • table – the table you want to convert. Each row of this table becomes one inner list.

What it returns – a list of lists, with one inner list for each row. Each inner list holds that row’s values in column order, without the column names.

The Table.ToRows function works in Power Query in Excel for Microsoft 365, 2024, 2021, 2019, 2016, and Excel for Mac (Microsoft 365), as well as in Power BI.

When to Use the Table.ToRows Function

Use this function when you need to:

  • Look at or work with the values of each row as a list
  • Use a two-column mapping table (old value, new value) in a function that needs a list of pairs, like Table.RenameColumns
  • Count, check, or calculate across the columns of each row
  • Get every value in a table as one list
  • Rebuild a table row by row with Table.FromRows

Example 1: Convert a Table into a List of Rows

Let’s start with the basic use, so you can see what Table.ToRows actually gives you.

Below is a Books table with the title, genre, and number of copies of each book, and I want each row as a list.

A Books table with title, genre and copies

In the example file, each example’s table is on its own sheet, and the finished queries are under Data > Queries & Connections.

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 in this article.

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

= Table.ToRows(Source)

Result: a list of four items, and each item shows List

Table.ToRows returns a list of four items, each shown as List

Here, Table.ToRows goes through the table one row at a time and turns each row into a list. Since the Books table has four rows, you get a list with four items, and each of those items is itself a list.

Power Query doesn’t show what’s inside a nested list in the preview. It just shows List.

To peek inside one, click on the empty space next to List (not on the word itself), and its values show up in a small pane at the bottom.

You can also get one row by its position. Use this formula to get the first row:

= Table.ToRows(Source){0}

Result: a list with The Silent Harbor, Mystery, and 12

Table.ToRows(Source){0} gives the first row's values: The Silent Harbor, Mystery, 12

Here, {0} picks the first item of the list, because positions in Power Query start at 0. So {1} would give you the Ocean of Stars row, and {4} gives an error, because there is no fifth row.

Notice that the row’s values keep their types, so 12 is still a number and a date in your table stays a date. But the column names are gone, so the list only has the values.

If you only need one row and want to keep the column names, Source{0} is simpler. It gives the first row as a record, with each value next to its column name.

For the whole table, the Table.ToRecords function gives every row as a record in the same way. And if you want the values column by column instead of row by row, there’s the Table.ToColumns function.

Example 2: Rename Columns Using a Mapping Table

This is one of the most practical uses of Table.ToRows.

Suppose you get a sales export with system-style column names, like cust_nm and qty, and you keep a small table that maps each old name to the name you want. Below is the sales data, on the Example 2 – Sales sheet.

A sales export with system-style column names like cust_nm and qty

And here is the mapping table, on the Example 2 – Header Map sheet, with each old column name and its new name.

A HeaderMap table with each old column name and its new name

The two tables are Excel Tables named SalesExport and HeaderMap. The code below reads them by those names, so for your own data, change the names to match your tables.

Create a new blank query (Data > Get Data > From Other Sources > Blank Query), open the Advanced Editor (Home > Advanced Editor), and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="SalesExport"]}[Content],
HeaderMap = Excel.CurrentWorkbook(){[Name="HeaderMap"]}[Content],
#"Renamed Columns" = Table.RenameColumns(Source,Table.ToRows(HeaderMap))
in
#"Renamed Columns"

Result: the sales table with the columns renamed to Customer, Product Code, Quantity, and Amount

The columns are renamed to Customer, Product Code, Quantity and Amount

Here, the Table.RenameColumns function needs the renames as a list of pairs, like {{"cust_nm","Customer"},{"qty","Quantity"}}.

That’s exactly what Table.ToRows gives you from the HeaderMap table. It has two columns, with the old name first, so each row becomes a two-item list like {“qty”,”Quantity”}.

The rows in the mapping table don’t have to be in the same order as the columns. Table.RenameColumns matches each pair by the old name, so qty gets renamed to Quantity wherever that column sits.

The big benefit is that the renames now live in a table on the worksheet. If the export changes or you want a different name, you edit the HeaderMap table and refresh the query, without touching any M code.

If the mapping table has an old name that isn’t in the data, you get an error like “The column ‘region’ of the table wasn’t found.” To skip such names, add MissingField.Ignore as the third argument: Table.RenameColumns(Source,Table.ToRows(HeaderMap),MissingField.Ignore).

Example 3: Count the Filled Cells in Each Row

Since each row is a list, you can run any List function on it. Here’s a simple one.

Below is a Survey table with each respondent’s rating for four questions, where some questions were skipped, and I want a list of how many questions each person answered.

A Survey table with ratings for four questions, some skipped

Add a new step and use this formula:

= List.Transform(Table.ToRows(Source),each List.NonNullCount(List.Skip(_)))

Result: a list of four numbers: 3, 4, 1, and 2

A list of answered-question counts: 3, 4, 1 and 2

Here, Table.ToRows turns each survey response into a list, like {“Lori Moore”,4,5,null,3}. The skipped questions are blank cells, and they come into Power Query as null.

The List.Transform function then runs the same steps on each of those row lists. _ is the current row, the List.Skip function drops its first item (the name), and the List.NonNullCount function counts the values that aren’t null.

List.Skip works by position, so this relies on the Respondent column being the first column. If you add a column before it, that column would get skipped instead.

The result is a list of counts without the names. If you want the count as a new column next to each respondent, add a custom column with List.NonNullCount(List.Skip(Record.FieldValues(_))) instead.

In a custom column, _ is the row as a record (like Source{0} in Example 1), not a list. The Record.FieldValues function turns that record into a list of its values, so the same List functions work on it.

Example 4: Count Rows with a Missing Value

This next one is a quick data-quality check.

Below is a Contacts table with each person’s name, email, phone, and city, where some contacts are missing an email or a phone, and I want to know how many rows are incomplete.

A Contacts table where one contact has no email and one has no phone

Add a new step and use this formula:

= List.Count(List.Select(Table.ToRows(Source),each List.Contains(_,null)))

Result: 2

The query returns 2, the number of rows with a missing value

Here, the List.Contains function checks whether a row list has a null anywhere in it. The List.Select function keeps only the rows where that check is true, and the List.Count function counts them.

Diana Allen has no email and Stuart Murray has no phone, so you get 2.

The nice thing is that this checks every column, without you naming any of them. So if the table gets a new column later, the check covers it too.

This gives you only the count. If you want to see which rows are incomplete, use Table.SelectRows(Source,each List.Contains(Record.FieldValues(_),null)), which keeps those rows as a table.

Here too, Record.FieldValues turns each row record into a list.

Note that this only catches truly blank cells (null). A cell that holds an empty text value (“”) or a space isn’t null, so it won’t be counted as missing.

Follow along with the example file

Table.ToRows Function Examples.xlsx

Download

Example 5: Get Every Value in a Table as One List

Sometimes the values you need are spread across several columns, and you want them all in one list.

Below is a ShiftRoster table with who works the morning and evening shift each day, and I want a list of every person on the roster, with each name only once.

A ShiftRoster table with the morning and evening staff for each day

Add a new step and use this formula:

= List.Distinct(List.Combine(Table.ToRows(Table.RemoveColumns(Source,{"Day"}))))

Result: Tyler Bailey, Brianna Hall, Isaac King, Christina Hughes, and Travis Sanders

A list of the five people on the roster, each listed once

Here, the Table.RemoveColumns function first drops the Day column, since we only want the names. Table.ToRows then gives one list per day, like {“Tyler Bailey”,”Brianna Hall”} for Monday.

The List.Combine function joins all those day lists into one long list, and the List.Distinct function removes the repeats.

Since the values are read row by row, the names come out in the order people first show up on the roster. Tyler and Brianna work on Monday, so they come first.

If you use Table.ToColumns instead of Table.ToRows here, you get the same five names, but in column order (all the morning names first, then the evening names). So pick the one that gives the order you want.

With just two shift columns, List.Distinct(Source[Morning] & Source[Evening]) also works and also gives the column order. The Table.ToRows version saves you from naming every column when there are many of them.

Example 6: Get the First Available Phone Number

Now let’s use Table.ToRows to build a new table, row by row.

Below is a CustomerPhones table with a mobile, work, and home number for each customer, where some numbers are missing, and I want a table with each customer and their first available number.

A CustomerPhones table with mobile, work and home numbers, some missing

Add a new step and use this formula:

= Table.FromRows(
List.Transform(Table.ToRows(Source),each {_{0},List.First(List.RemoveNulls(List.Skip(_)))}),
{"Customer","Best Phone"}
)

Result: a two-column table where Keith Cook gets 555-0187, Marcus Wright gets 555-0142, Patrick Smith gets 555-0164, and Linda Taylor gets null

Each customer with their first available phone number, and null for Linda Taylor

Here, for each row list, _{0} is the first value (the customer name). List.Skip(_) gives the three phone numbers, the List.RemoveNulls function drops the blank ones, and the List.First function picks the first number that’s left.

So each row becomes a new two-item list, like {“Keith Cook”,”555-0187″}. Linda Taylor has no number on file at all (that row is blank on purpose), so List.First has nothing to pick and returns null.

The Table.FromRows function is the reverse of Table.ToRows. It takes the list of rows and turns it back into a table, with the column names you give it in the second argument.

If you skip those column names, the new table gets generic names like Column1 and Column2. Also, the new columns don’t carry any data types, so set the types if you need them.

With only three fixed columns, you could also add a custom column with List.First(List.RemoveNulls({[Mobile],[Work],[Home]})). It does the same thing, but you list the columns by name, in the order you want them checked.

The Table.ToRows version is handy when there are more columns, because it checks all of them after the name without listing them.

Example 7: Find the Month with the Highest Sales for Each Rep

Here’s a more advanced one that uses the position of a value in the row.

Below is a RepSales table with the monthly sales of each rep from January to April, and I want a table that shows each rep’s best month.

A RepSales table with each rep's sales from January to April

Add a new step and use this formula:

= Table.FromRows(
List.Transform(Table.ToRows(Source),each {_{0},Table.ColumnNames(Source){List.PositionOf(_,List.Max(List.Skip(_)))}}),
{"Rep","Best Month"}
)

Result: a two-column table where Christopher Scott gets Feb, Megan Powell gets Apr, and Brandon Wallace gets Jan

Each rep's best month: Feb, Apr and Jan

Here, List.Max(List.Skip(_)) finds the highest sales number in the row, leaving out the rep’s name. The List.PositionOf function then finds where that number sits in the row list.

For Christopher Scott, the highest number is 5100, and it’s at position 2 (positions start at 0, and the name is at position 0).

Since the values in each row list are in the same order as the columns, position 2 is also the third column. The Table.ColumnNames function gives the column names as a list, so Table.ColumnNames(Source){2} gives that column’s name, which is Feb.

If two months tie for the highest number, List.PositionOf returns the first one. Brandon Wallace has 5200 in both Jan and Apr, so you get Jan.

Like Example 6, Table.FromRows then turns these two-item lists into a table with Rep and Best Month columns.

This formula looks at positions, so the rep’s name must be in the first column and every other column must be a month.

Also, a rep with no sales at all (every month blank) gets Jan instead of nothing. All the values are null, so the highest value is null too, and the first null sits in the Jan position.

On a large or changing dataset, unpivoting the month columns (Transform > Unpivot Other Columns) and then grouping by rep is the sturdier way to do this.

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.