List.Count

Updated on

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.

Learn more about List.Count in the following articles:

Other functions related to List.Count are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/list-count