Power Query List.NonNullCount: How to Use It
In this article
The List.NonNullCount function in Power Query counts how many items in a list are not null. It returns a number. You use it when you need to know how many non-empty values exist in a column, a list, or a split text, especially when nulls would otherwise skew your count. You can call it from the formula bar, inside a custom column, or within another M expression.
What You'll Need
- Power Query Editor open (in Excel: Data > Get Data > Launch Power Query Editor; in Power BI: Home > Transform Data).
- A data source with at least one column or list that contains nulls (or might contain nulls).
- Basic familiarity with the Power Query formula bar (the bar at the top of the editor where you can type or edit M code).
Step-by-Step
- Open Power Query Editor with your data loaded. If you haven't loaded data yet, do that first.
- Identify the column or list you want to count non-null values from. For example, a column named "Notes" that has some empty cells.
- Add a custom column (the easiest way to test the function): go to the Add Column tab and click Custom Column.
- In the Custom Column dialog, give the new column a name like "NonNullCount" and enter the formula:
List.NonNullCount(YourColumnName)– replaceYourColumnNamewith the actual column name. If the column name has spaces or special characters, wrap it in square brackets, likeList.NonNullCount([My Column]). - Click OK. The new column will appear with a number for each row. That number is the count of non-null values in that column for that row? Wait – no. Important:
List.NonNullCountexpects a list, not a single value. If you reference a column directly, you're referencing a single value per row, not a list. So the above step would return an error. Let's correct that. - Correct approach – count non-nulls in a column (aggregate): To count how many non-null values exist in an entire column, you use
List.NonNullCounton the column's values. In Power Query, you can do this in a few ways:- In the formula bar when you're on a new query or a blank step: type
= List.NonNullCount(YourTable[YourColumn]). For example,= List.NonNullCount(Employees[Notes])returns a single number. - In a custom column that aggregates the whole table: you'd need to reference the table itself, like
= List.NonNullCount(YourTable[YourColumn])– but that would put the same total in every row, which is usually not what you want.
- In the formula bar when you're on a new query or a blank step: type
- Correct approach – count non-nulls in a list that is in a cell: If you have a column that contains lists (for example, from splitting text by delimiter), you can use
List.NonNullCount([ListColumn])in a custom column. That works because each cell contains a list. - Alternative – count non-nulls per row across multiple columns: Use
List.NonNullCount({[Col1], [Col2], [Col3]})– note the curly braces{}create a list from the three column values. This is a common use case. - Check the result – the function returns an integer. If the list is empty, it returns 0. If the list contains only nulls, it returns 0.
Common Mistakes
- Passing a column instead of a list:
List.NonNullCount(ColumnName)without square brackets or braces is invalid. You need[ColumnName]to reference a column's values as a list, or{...}to create a list from multiple values. - Confusing with
List.Count:List.Countcounts all items including nulls.List.NonNullCountexcludes nulls. They differ when nulls exist. - Using it on a single value: If you try
List.NonNullCount(5), you'll get an error because 5 is not a list. - Forgetting that empty text is not null:
List.NonNullCountcounts empty strings ("") as non-null. If you want to exclude empty text, you need a different approach, like filtering.
Tips
- To count non-empty text values (excluding both nulls and empty strings), use:
List.Count(List.Select(YourList, each _ <> null and _ <> "")). - Use
List.NonNullCountin combination withTable.Columnto get a column's values as a list:List.NonNullCount(Table.Column(YourTable, "ColumnName")). - If you're counting non-nulls in a column for a summary, consider using the Group By feature instead – it's often faster.
- Remember that
List.NonNullCountworks on any list, including lists created from functions likeText.SplitorList.Transform.
Troubleshooting
If you get an error like "We cannot convert the value null to type List": you're passing a null value instead of a list. Check that the column or expression you're referencing actually contains lists or a valid column reference.
If you get "Expression.Error: The name 'YourColumn' wasn't recognized": the column name is wrong. Check the exact name in the column header, and if it has spaces, use square brackets: Table[Column Name].
If the result is always 0: your list might actually be empty, or all values are null. Check by selecting the column and looking at the preview – do you see nulls? Also check that you're referencing the right column.
If the result seems too high: remember that empty strings are counted. If you expected to exclude blanks, use the tip above to filter out empty strings.
If you're trying to count non-null values per row across columns and get an error: make sure you use curly braces {} to create the list. For example: List.NonNullCount({[A], [B], [C]}).
Frequently Asked Questions
Does List.NonNullCount count empty text strings?
Yes, it counts empty text strings ("") as non-null. Only actual null values are excluded.
Can I use List.NonNullCount on a table column directly?
Yes, but you need to reference the column as a list, like Table[Column] or Table.Column(Table, "Column"). You cannot pass a single cell value.
What's the difference between List.Count and List.NonNullCount?
List.Count returns the total number of items in a list, including nulls. List.NonNullCount returns only the number of items that are not null.
Can I use List.NonNullCount in a calculated column in Power BI?
No, Power Query functions like this are used in the Power Query Editor, not in DAX calculated columns. In DAX, use COUNTX or COUNTA instead.
How do I count non-null values per row across multiple columns?
Create a custom column with: List.NonNullCount({[Col1], [Col2], [Col3]}). Replace Col1, Col2, Col3 with your actual column names.
What happens if the list is empty?
The function returns 0.