Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query List.Count Function: A Complete Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To use the List.Count function in Power Query, you must apply the formula List.Count(list) within a custom column or a manual M code step, where 'list' represents the specific list object you wish to measure. This function returns a single integer representing the total number of items contained within a provided list, including null values but excluding the list itself.

In the context of Power Query (M language), lists are fundamental data structures that hold a sequence of values. Unlike a table, which is a collection of rows and columns, a list is a single-dimensional array. The List.Count function is essential when you are performing advanced data transformations, such as counting the number of elements in a nested list within a table cell, or determining the size of a list generated by a function like Text.Split or Table.Column. This guide will walk you through the technical implementation, common use cases, and error-handling strategies required to master this function.

What You'll Need

  • Microsoft Excel (with Power Query enabled) or Power BI Desktop.
  • A dataset containing either a column of lists or a column of text/values that can be converted into a list.
  • Basic familiarity with the Power Query Editor interface.
  • An understanding of the difference between a Table and a List in the M language.

Step-by-Step

  1. Load Your Data: Open Excel or Power BI, go to the Data tab, and select Get Data to import your source. Once the Power Query Editor window opens, ensure your data is visible.
  2. Identify the Target List: Determine which column contains the data you want to count. Note that List.Count only works on a List object. If your data is in a standard column of text, you cannot use List.Count directly on the column; you must first transform that data into a list format (for example, by splitting a comma-separated string into a list).
  3. Create a Custom Column: Navigate to the Add Column tab on the top ribbon and click on Custom Column. This will open a dialog box titled "Custom Column".
  4. Enter the Formula: In the "Custom column formula" box, begin typing the function. If you are counting a column that already contains list objects (often seen when expanding nested data), the formula will look like this: = List.Count([ColumnName]). Replace [ColumnName] with the actual name of your column by double-clicking it from the list on the right.
  5. Handle Text-to-List Conversion (If Necessary): If your column contains text strings like "Apple, Orange, Banana" and you want to count the items, you must first convert them. In the Custom Column box, enter: = List.Count(Text.Split([ColumnName], ",")). This tells Power Query to split the text by the comma delimiter to create a list, and then immediately count the items in that newly created list.
  6. Apply and Review: Click OK. A new column will appear in your preview window showing integer values. Review these numbers to ensure they align with your expectations.
  7. Finalize the Step: Once satisfied, go to the Home tab and select Close & Apply to load the transformed data back into your spreadsheet or data model.

Common Mistakes

One of the most frequent errors is attempting to use List.Count on a Table instead of a List. If you try to run List.Count(MyTable), the engine will return an error because a Table is a multi-dimensional structure, whereas List.Count expects a one-dimensional sequence. To count rows in a table, you should use Table.RowCount(MyTable) instead.

Another common mistake involves misunderstanding how null values are treated. List.Count counts every item in the list, including null. If your list is {1, 2, null, 4}, the result will be 4. If your intention was to count only non-null values, List.Count will provide an inflated number. In such cases, you must first filter the list using List.NonNullCount, which is a specific variation designed to ignore null entries.

Users also often forget the syntax requirements of the M language. M is case-sensitive. Writing list.count or List.count will result in an expression error. It must be exactly List.Count.

Tips

To optimize performance when working with large datasets, avoid creating unnecessary intermediate steps. Instead of creating a column that splits text into a list and then a second column that counts that list, combine them into a single custom column step as shown in the Text.Split example above. This reduces the memory overhead of the Power Query engine.

If you need to count items based on specific criteria (e.g., counting only numbers greater than 10), do not try to force List.Count to do the filtering. Instead, use List.Count(List.Select([ColumnName], each _ > 10)). This nesting approach is highly efficient and keeps your M code clean and readable.

Troubleshooting

If you encounter an error, follow these steps in order:

  1. Check the Error Message: Hover over the word "Error" in the cell. You must transform the data into a list first.
  2. Verify Case Sensitivity: Ensure the function is written as List.Count with a capital L and a capital C.
  3. Validate the Data Type: Go to the step immediately preceding your count. Look at the icon next to the column header. If it does not show the list icon (two small arrows or a list symbol), the engine does not recognize it as a list.
  4. Check for Empty Lists: If the result is 0, ensure the list isn't actually empty or that the split delimiter you used in Text.Split matches the actual character in your data (e.g., using a semicolon instead of a comma).

Frequently Asked Questions

How do I count only unique items in a list?

To count only unique values, wrap your list in the List.Distinct function. The formula would be = List.Count(List.Distinct([ColumnName])).

What is the difference between List.Count and List.NonNullCount?

List.Count counts every single element in the list, including null values. List.NonNullCount specifically ignores any null entries, providing a count of only the actual data present.

Can I use List.Count to count rows in a table?

No. While a column in a table can be treated as a list, the table itself is not a list. Use Table.RowCount to get the number of rows in a table.

Why does my List.Count return an error on a text column?

Because a text column contains individual text strings, not lists. You must first use a function like Text.Split to turn that text into a list of items before List.Count can process it.

Does List.Count include empty strings?

Yes. An empty string ("") is considered a valid item in a list. If your list is { "", "A" }, List.Count will return 2.

How can I count items that match a specific word?

Use List.Count(List.Select([ColumnName], each _ = "TargetWord")). This filters the list to only include the target word and then counts the resulting list.