Table.RowCount 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.RowCount function in Power Query counts the rows in a table and gives you that count as a number. You’ll use it most after another step, like a filter, a Group By, or a merge, to count the rows that step left you with.

In this article, I’ll show you how to use it with practical examples, from a basic row count to counting rows that match conditions, counting per group, and checking that a file has all its rows.

Follow along with the example file

Table.RowCount Function Examples.xlsx

Download

Table.RowCount Function Syntax

= Table.RowCount(table as table) as number
  • table – the table whose rows you want to count. This is usually the previous step, such as Source, or a filtered version of it.

If you give it a single column instead, like Source[Status], you get the error “We cannot convert a value of type List to type Table.”

What it returns – a number, the count of rows in the table. The header row isn’t counted.

The Table.RowCount function works in Power Query in Excel and in Power BI.

When to Use the Table.RowCount Function

Use this function when you need to:

  • Find out how many rows a table has
  • Count the rows that meet one or more conditions
  • Count unique combinations, like each employee and course pair
  • Count the rows in each group, like calls per agent
  • Show what share of all rows each group makes up
  • Count how many rows in another table match each row
  • Find how many duplicate rows a table has
  • Check that a file has the number of rows you expect before you use it

Example 1: Count All the Rows in a Table

Let’s start with the basic use, counting every row in a table.

Below is a list of registrations for Excel workshops, and I want to count how many people registered.

Workshop registrations with Attendee, Workshop, Ticket and Price

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.RowCount(Source)

Result: 8

Table.RowCount returns 8

Here, the function counts the data rows in the Source table, which gives 8. The header row isn’t counted.

The result is a single number, so Power Query shows just the 8 in the preview instead of a table.

It counts every row, including a row where all the cells are blank. So if your table has empty rows in it, remove them first if you don’t want them counted.

Example 2: Count the Rows That Meet a Condition

Most of the time, you don’t want all the rows, just the ones that match something.

Below is a list of IT help desk tickets with their status, and I want to count how many tickets are still Open.

IT help desk tickets with a Status column

Load this table into Power Query the same way as in Example 1, then add a new step (by clicking on the fx icon) and use this formula:

= Table.RowCount(Table.SelectRows(Source,each [Status]="Open"))

Result: 3

Counting only the Open tickets returns 3

Here, the Table.SelectRows function keeps only the rows where the Status is “Open”. Table.RowCount then counts what’s left, which is 3 tickets.

This is the Power Query version of the COUNTIF function, where you filter first and then count.

The text comparison is case-sensitive, so a ticket with the status “open” wouldn’t be counted. If your data isn’t consistent, use each Text.Lower([Status])="open" instead.

When there’s no match at all, the count is 0. If you only need a yes or no, put the Table.IsEmpty function in place of Table.RowCount around the same Table.SelectRows part.

Table.IsEmpty returns TRUE when the filtered table has no rows, which means nothing matched.

Example 3: Count the Rows That Meet Multiple Conditions

Here’s the Power Query version of the COUNTIFS function, where a row has to pass more than one test.

Below is a list of car rental bookings, and I want to count the bookings at the Airport branch that are for 5 days or more.

Car rental bookings with Branch and Days

Load this table into Power Query the same way as in Example 1, then add a new step (by clicking on the fx icon) and use this formula:

= Table.RowCount(Table.SelectRows(Source,each [Branch]="Airport" and [Days]>=5))

Result: 3

Airport bookings of 5 days or more: 3

Here, and means a row is kept only when both conditions are true. Five bookings are at the Airport branch, but only three of them (7, 5, and 10 days) are for 5 days or more.

To count rows that meet either condition, use or instead of and. For example, [Branch]="Airport" or [Days]>=5 counts every Airport booking plus the long Downtown ones.

The [Days]>=5 part needs Days to be a number. If your Days column comes in as text, this step fails with the error “We cannot apply operator < to types Number and Text.”

To fix it, change the type of the Days column to whole number before you filter.

Example 4: Count the Unique Combinations of Two Columns

This next one comes up when some rows repeat and you only want to count each one once.

Below is a training log where some employees took the same course twice (a retake), and I want to count how many employee and course combinations there are.

Training log where two employees retook a course

Load this table into Power Query the same way as in Example 1, then add a new step (by clicking on the fx icon) and use this formula:

= Table.RowCount(Table.Distinct(Source,{"Employee","Course"}))

Result: 8

Unique employee and course combinations: 8

Here, the Table.Distinct function keeps one row for each unique pair of Employee and Course. Wayne Powell took Excel Basics twice and Chad Murphy took Data Privacy twice, so 10 rows become 8, and Table.RowCount counts those 8.

The Score column is different on the retakes, which is why the column list matters. Without {"Employee","Course"}, Table.Distinct compares whole rows, and all 10 rows count as unique.

To count the unique values in just one column, like the number of employees, List.Count(List.Distinct(Source[Employee])) is the simpler choice, and it returns 6 here.

Table.RowCount with Table.Distinct is the one to use when the combination spans two or more columns.

Example 5: Count the Rows in Each Group

This is the one you’ll probably use the most, because it’s what Group By does when you count rows.

Below is a log of support calls with the agent who took each call, and I want to count how many calls each agent took in a new Calls column.

Support calls with the agent who took each one

Load this table into Power Query the same way as in Example 1, then add a new step (by clicking on the fx icon) and use this formula:

= Table.Group(Source,{"Agent"},{{"Calls",each Table.RowCount(_),Int64.Type}})

Result: a table with Kenneth Tucker 4, Karen Wood 4, and Ryan Murray 2

Calls per agent from Table.Group with Table.RowCount

Here, the Table.Group function splits the calls into one small table per agent. For each of those tables, each Table.RowCount(_) counts its rows, where the underscore _ stands for the group’s table.

Int64.Type sets the Calls column to whole numbers. The agents show up in the order they first appear in the data, not sorted.

If you’d rather click than type, Transform > Group By with the Count Rows operation gives you the same counts per agent.

Follow along with the example file

Table.RowCount Function Examples.xlsx

Download

Example 6: Show Each Group’s Share of All the Rows

Once you can count each group, you can also show what percentage of the total it is.

Below are the responses to an event feedback survey, and I want to count the responses for each rating and show its share of all responses.

Event feedback responses with a Rating

This query has more than one step, so it goes in the Advanced Editor. Go to Data > Get Data > From Other Sources > Blank Query, then open the Advanced Editor (Home > Advanced Editor) and use this code.

The table is named EventFeedback, and the code reads it by name, so you don’t need to load it into Power Query first.

let
Source = Excel.CurrentWorkbook(){[Name="EventFeedback"]}[Content],
TotalRows = Table.RowCount(Source),
Grouped = Table.Group(Source,{"Rating"},{{"Responses",each Table.RowCount(_),Int64.Type},{"Share",each Table.RowCount(_)/TotalRows,Percentage.Type}})
in
Grouped

Result: a table with Good 3 (30.00%), Excellent 5 (50.00%), and Poor 2 (20.00%)

Responses per rating and each rating's share of all rows

Here, the TotalRows step counts all 10 responses once. Inside the Group By, Table.RowCount(_) counts the rows for each rating, and dividing it by TotalRows gives the share.

So Excellent has 5 of the 10 responses, which is 50%. Percentage.Type makes Power Query show the Share column as a percentage instead of a decimal like 0.5.

Counting the total in its own step means it’s counted once, not again for every group. It also keeps the formula easier to read.

Example 7: Count Matching Rows From Another Table

Here’s how to count, for every row in one table, how many rows match it in another table.

Below is a list of gym members, and I want to count how many classes each member checked in to, in a new Visits column.

Gym members with Member ID and Member

And here’s the check-in log, with the Member ID of the person who checked in to each class.

Class check-in log with the Member ID of each visit

The two tables are named GymMembers and ClassCheckIns. Create a new blank query and open the Advanced Editor the same way as in Example 6, then use this code:

let
Source = Excel.CurrentWorkbook(){[Name="GymMembers"]}[Content],
CheckIns = Excel.CurrentWorkbook(){[Name="ClassCheckIns"]}[Content],
Merged = Table.NestedJoin(Source,{"Member ID"},CheckIns,{"Member ID"},"Visits",JoinKind.LeftOuter),
Counted = Table.TransformColumns(Merged,{{"Visits",Table.RowCount,Int64.Type}})
in
Counted

Result: a table with each member and their number of visits, including 0 for Curtis Turner

Visits per member, with 0 for Curtis Turner

Here, the Merged step adds a Visits column where each cell holds a small table with that member’s check-ins. Harold Powell’s table has 4 rows.

JoinKind.LeftOuter keeps every member, even one with no matching check-ins. So Curtis Turner stays in the table with an empty Visits table, which is what later becomes 0.

The Table.TransformColumns function then runs Table.RowCount on each of those tables, so every cell becomes a number. The function name goes in without brackets or each, because Table.TransformColumns passes each cell to it directly.

You could also group the check-in log by Member ID and count the rows. But Curtis Turner has no check-ins, so he isn’t in that log, and he’d be missing from the counts instead of showing 0.

Example 8: Find How Many Duplicate Rows a Table Has

This is a quick check before you clean up a list that might have repeats.

Below is a list of vendor invoices where some invoices were entered twice, and I want to find how many extra copies there are.

Vendor invoices where two invoices were entered twice

Load this table into Power Query the same way as in Example 1, then add a new step (by clicking on the fx icon) and use this formula:

= Table.RowCount(Source)-Table.RowCount(Table.Distinct(Source))

Result: 2

Total rows minus distinct rows returns 2 extra copies

Here, the first Table.RowCount counts all 8 rows. Table.Distinct(Source) keeps one copy of each row (INV-7711 and INV-7712 appear twice), leaving 6.

The difference, 2, is the number of extra copies, which are the rows Remove Duplicates would delete.

Table.Distinct only treats rows as duplicates when every column matches. So if the same invoice was entered twice with a different amount, this formula doesn’t count it.

To catch those too, use Table.Distinct(Source,{"Invoice No"}) in place of Table.Distinct(Source) in the formula. The result is then one total, the number of rows that repeat an invoice number already listed above.

Example 9: Check That a File Has All Its Rows Before Using It

This last one uses the row count as a safety check in your query.

Below is a year of monthly electricity bills, one row per month, and I want the query to stop with a clear message if any month is missing.

Twelve monthly electricity bills

Load this table into Power Query the same way as in Example 1, then add a new step (by clicking on the fx icon) and use this formula:

= if Table.RowCount(Source)=12 then Source else error ("Expected 12 months but found "&Text.From(Table.RowCount(Source))&" rows")

Result: the same table with all 12 months

The 12-row check passes and returns the table

Here, the formula counts the rows, and since there are 12, it returns the Source table unchanged. Your later steps carry on as normal.

If a month is missing, the step returns an error instead, with the message “Expected 12 months but found 11 rows”. The Text.From function turns the count into text so it can be joined into the message with &.

The check is for exactly 12, so a file with a 13th row stops with the same kind of message.

Without this check, a file with a missing month loads without any warning, and the yearly total is just lower. The error tells you what’s wrong before anyone uses that total.

Other Excel Articles You May Also Like:

  • Table.FirstN Function in Power Query M
  • List.Count – Use List.Count in Power Query M to get the total number of items in a list.
  • List.NonNullCount – Use List.NonNullCount in Power Query M to count the filled, non-null values in a list.
  • Table.RemoveRows – Use Table.RemoveRows in Power Query M to delete rows by position.
  • Table.Skip – Use Table.Skip in Power Query M to remove rows from the top of a table.
  • List.Sum – Use List.Sum in Power Query M to add up a list of numbers.
  • List.IsEmpty – Use List.IsEmpty in Power Query M to check whether a list has values or is empty.
  • Table.NestedJoin – Use Table.NestedJoin in Power Query M to merge two tables.

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.