List.Count is a Power Query M function that calculates the number of items in a given list. The function returns the total item count for the list.
Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365
Syntax
List.Count( list as list ) as number
Description
The List.Count function determines the number of items within a list. A distinct feature of this function is its robust error-handling capability. Even if a list includes errors, List.Count successfully counts all items, including those causing errors, ensuring an accurate total.
Examples
Counting Items in a Simple List
This example illustrates the basic usage of the List.Count function. When dealing with a straightforward list, such as one containing five values, List.Count returns the total count of these values.
List.Count( { "a", "b", "c", "d", "e" } ) // Output: 5
Here, the function accurately counts the number of string elements in the list.
Counting in a Mixed-Type List
List.Count demonstrates can also handle lists composed of diverse data types. The following example includes boolean values, a string, a number, another list, and a record. The function treats each distinct type as a separate item, thus providing the total number of elements within the mixed-type list.
List.Count( { true, false, "Power BI", 100, {1, 2, 3}, [Key="Value"] } ) // Output: 6
In this case, each item, regardless of its data type, is counted individually.
Counting with Errors in the List
Another characteristic of List.Count is that it disregards any errors within the list. It is smart enough to count the number of items in there without throwing an error. For instance:
List.Count( { 1, 2, 3, null, "text", 5/"a" } ) // Output: 6
In this scenario, despite the presence of an error (caused by 5/"a"
) and a null value, the function effectively counts all elements, including those causing errors.
Related articles
Learn more about List.Count in the following articles:
- Lists in Power Query M / List Functions (200+ Examples)
The complete guide to Lists in Power Query M. Learn from practical examples and master Power Query’s most powerful List functions. » Read more
Related functions
Other functions related to List.Count are:
- List.Average
- List.Covariance
- List.Median
- List.Mode
- List.Modes
- List.NonNullCount
- List.Percentile
- List.Product
- List.StandardDeviation
- List.Sum
2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy