List.Count Function in Power Query
The List.Count function is a fundamental metadata tool in the Power Query M language. It evaluates a list and returns the total number of items contained within it.
Syntax
List.Count(list as list) as number
Parameters
- list: The source list you want to measure. This list can contain any data type, including nested lists or records.
Example: Find the number of values in the list.
Power Query M
let
Source = {"India", "America", "Canada", "Japan", "Australia", "England"},
Return = List.Count(Source)
in
Return The output will be 6.
Example: Counting Rows in a Filtered Column
You can use List.Count on a specific column of a table to see how many entries are present after a transformation.
let
TotalOrders = List.Count(SalesTable[OrderID])
in
TotalOrders Common Use Cases
- Data Integrity Checks: Comparing the result of
List.Countto an expected value to ensure an entire dataset was imported. - Dynamic Indexing: Using the count to generate a sequence of numbers from 1 to N using
{1..List.Count(MyList)}. - Average Calculations: Manually calculating a mean by dividing
List.SumbyList.Count. - Loop Boundaries: Providing the upper limit for iterative functions that process lists item-by-item.
List.Count vs. List.NonNullCount
It is vital to choose the right counting function:
• List.Count: Counts every single item, including null values.
• List.NonNullCount: Only counts items that are not null.
Key Behaviors
- Empty List: If the input is an empty list (
{}), the function returns 0.