Table.RowCount Function (Power Query M)

If you want to find out how many rows are in a table in Power Query, the Table.RowCount function is what you reach for. Give it a table, and it hands back how many rows that table has. Available in Excel (Power Query), Power BI Desktop, and Power BI Service.

Syntax of Table.RowCount Function

Table.RowCount(table as table) as number

where

  • table (required, table). The table whose rows you want to count.

Returns: a number, the count of rows in table. It is always a non-negative whole number, and an empty table returns 0.

In plain terms, you hand it a table and it tells you how many rows that table has.

Example 1: Count the rows of a table

Start with an Employees table and find out how many rows it holds.

Here is the starting data:

EmployeeIDNameDepartment
E-01AshaSales
E-02MarcoSupport
E-03PriyaSales
E-04TomFinance
let
Source = Excel.CurrentWorkbook(){[Name="Employees"]}[Content],
Result = Table.RowCount(Source)
in
Result

Result: 4

The table has four rows, so the function returns 4.

Example 2: Count how many rows match a condition

This is the most common real-world use. Filter the table down first with Table.SelectRows, then count what is left, and you have a count of how many rows match a condition.

Say you have an Orders table and want to know how many orders have shipped.

Here is the starting data:

OrderIDStatusAmount
O-1001Shipped120
O-1002Pending80
O-1003Shipped200
O-1004Cancelled50
O-1005Shipped95
let
Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content],
Shipped = Table.SelectRows(Source,each [Status]="Shipped"),
Result = Table.RowCount(Shipped)
in
Result

Result: 3

Three rows have a Status of Shipped, so the count comes back as 3.

Example 3: Count unique rows

Table.RowCount counts every row, duplicates included. To count only the unique rows, remove duplicates with Table.Distinct first, then count.

The Visits table below has two exact duplicates (Alice/Mon appears twice, Bob/Mon appears twice).

Here is the starting data:

VisitorDay
AliceMon
BobMon
AliceMon
CarolTue
BobMon
let
Source = Excel.CurrentWorkbook(){[Name="Visits"]}[Content],
Unique = Table.Distinct(Source),
Result = Table.RowCount(Unique)
in
Result

Result: 3

After the duplicates drop out, three distinct rows remain (Alice/Mon, Bob/Mon, Carol/Tue), so the count is 3.

Example 4: An empty result returns zero

When a filter matches no rows, you get an empty table, and Table.RowCount returns 0 rather than an error. That makes it safe to use in a guard before a step that would fail on no rows.

The Tickets table has no Critical priority, so filtering for it leaves nothing behind.

Here is the starting data:

TicketIDPriority
T-1Low
T-2Medium
T-3Low
let
Source = Excel.CurrentWorkbook(){[Name="Tickets"]}[Content],
Urgent = Table.SelectRows(Source,each [Priority]="Critical"),
Result = Table.RowCount(Urgent)
in
Result

Result: 0

No row matches, so the filtered table is empty and the count is 0.

Things to keep in mind with Table.RowCount

  • It counts every row, including blank or null ones. A row whose cells are all empty still counts. The function measures rows, not filled-in cells, so a fully blank row adds to the total.
  • It counts duplicates too. For a count of unique rows, run Table.Distinct first (Example 3).
  • An empty table returns 0, never an error (Example 4). You can rely on this in a check like if Table.RowCount(t)=0 then ... else ... before a step that needs at least one row.
  • Table.RowCount counts rows, Table.ColumnCount counts columns, and List.Count counts items in a list. If you have a single column as a list, use List.Count. For a whole table, use Table.RowCount. After you stack tables with Table.Combine, run it on the combined table to get the new total.
  • For very large foldable sources, Table.ApproximateRowCount is the faster, approximate alternative. Use Table.RowCount when you need the exact number.

Performance and query folding

Against a foldable backend like SQL Server, Table.RowCount can fold to a COUNT(*) aggregate, so the count runs on the server and only the single number comes back. That makes it cheap even on large tables.

On Excel, CSV, or folder sources, or after a step that has already broken folding (for example a Table.Buffer before it), it loads the rows locally and counts them in memory, which is fine for typical row volumes.

Common questions about Table.RowCount

What is the difference between Table.RowCount and List.Count?

Table.RowCount works on a table and returns how many rows it has. List.Count works on a list and returns how many items it has.

To count the values in one column, turn the column into a list (for example Source[Amount]) and use List.Count, or just use Table.RowCount on the table when you want the row total.

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.