Smart Systems for Home

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

Power Query Number.IsNaN Function: Usage and 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

The Number.IsNaN function in Power Query is used to determine whether a specific value is "Not a Number" (NaN). It returns a logical TRUE if the value is NaN and FALSE if the value is any other number or data type. This is essential for cleaning datasets where mathematical errors—such as dividing zero by zero—have introduced NaN values that would otherwise break calculations or cause errors in your final report.

What You'll Need

  • Microsoft Excel or Power BI Desktop.
  • A dataset loaded into the Power Query Editor.
  • A column containing numeric data (or data that is expected to be numeric) where NaN values may be present.

Step-by-Step

  1. Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
  2. Identify the Target Column: Locate the column containing the numeric values you wish to check. Ensure the column data type is set to "Decimal Number" or "Fixed Decimal Number," as Number.IsNaN is designed specifically for numeric types.
  3. Create a Custom Column: Navigate to the Add Column tab on the top ribbon and select Custom Column. This allows you to apply the function to every row in your table.
  4. Enter the Function Formula: In the Custom Column dialog box, give your new column a name (e.g., "Is it NaN?"). In the Custom column formula box, type the following expression: = Number.IsNaN([YourColumnName]). Replace [YourColumnName] with the actual name of the column you are testing.
  5. Verify the Results: Click OK. Power Query will generate a new column containing TRUE for every row where the value was NaN and FALSE for all other numeric values.
  6. Filter or Replace Values: Now that the NaNs are identified, you can use the filter dropdown on the new column to isolate the TRUE values. Alternatively, you can use a Conditional Column (Add Column > Conditional Column) to say: "If [Is it NaN?] equals TRUE, then replace with 0 (or null), else keep [YourColumnName]."
  7. Clean Up: Once you have handled the NaN values, right-click the helper column ("Is it NaN?") and select Remove to keep your dataset lean.

Common Mistakes

One of the most frequent mistakes is confusing NaN (Not a Number) with null. In Power Query, a null represents the absence of a value or an empty cell, whereas NaN is a specific numeric state resulting from an undefined mathematical operation. If you use Number.IsNaN on a cell that is simply empty (null), the function will return FALSE because a null is not a "Not a Number" numeric value; it is simply nothing. To catch both, you must use a combined statement like Number.IsNaN([Column]) or [Column] = null.

Another common error is attempting to use this function on a column that is typed as "Text." If the column data type is set to Text, Power Query may throw an expression error because Number.IsNaN expects a numeric input. Always ensure your data type is converted to a number before applying this specific function.

Users also often forget that Power Query is case-sensitive. Writing number.isnan or Number.Isnan will result in an "Expression.Error: The name 'number.isnan' wasn't recognized." You must use the exact casing: Number.IsNaN.

Tips

For those dealing with very large datasets, avoid creating multiple helper columns. Instead of creating a column to check for NaN and then another to replace it, you can write a single nested if statement in the Custom Column box: if Number.IsNaN([Amount]) then 0 else [Amount]. This reduces the number of steps in your Applied Steps list and improves query performance.

If you are importing data from a web source or a complex API, NaNs often appear during the conversion of floating-point numbers. Using Number.Round in conjunction with Number.IsNaN can help you standardize your data. For example, you can check for NaN first, and if the result is FALSE, then apply the rounding function to the value.

Consider using the "Replace Errors" feature found in the Transform tab as a first line of defense. While Number.IsNaN is a precise surgical tool for specific numeric states, "Replace Errors" can quickly swap out any calculation error (including NaNs) with a default value like 0 or null without needing to write a custom M formula.

Troubleshooting

If the function returns an error instead of TRUE or FALSE, first check the data type of the column. If the column is "Any" or "Text," the function may fail. Change the type to "Decimal Number" by clicking the icon next to the column header. If the error persists, check for hidden characters or whitespace in your data that might be preventing the column from being treated as a true numeric type.

If Number.IsNaN is returning FALSE for values that look like they should be NaNs, double-check if those values are actually the text string "NaN" rather than the numeric NaN state. If a source system exported the word "NaN" as text, Power Query treats it as a string. In this case, you should use a text filter or Text.Contains([Column], "NaN") instead of the numeric function.

If your query is running slowly after adding the custom column, try moving the Number.IsNaN check as early as possible in your Applied Steps. By filtering out or replacing NaNs immediately after the source step, you prevent subsequent transformations from having to process invalid numeric states, which can speed up the overall refresh time.

Frequently Asked Questions

What is the difference between NaN and null?

Null is a marker for missing data (an empty cell). NaN (Not a Number) is a numeric value that represents an undefined or unrepresentable result, such as the result of 0 divided by 0 in certain computing environments.

Can I use Number.IsNaN in a filter?

Yes, but you cannot do it via the standard filter dropdown menu. You must go to the formula bar and wrap your filter step in a function, or create a custom column using Number.IsNaN first and then filter that column for TRUE.

Does Number.IsNaN work with integers?

Yes, it works with any numeric type, including integers and decimals, although NaNs are almost exclusively found in floating-point (decimal) data types.

Will this function remove my data?

No, the function itself only returns a TRUE or FALSE value. It does not delete or modify your data unless you follow it up with a "Remove Rows" or "Replace Values" step.

Is there a way to find all non-numeric values at once?

While Number.IsNaN finds specific numeric errors, the best way to find all non-numeric values in a numeric column is to use the "Remove Errors" or "Replace Errors" tool, or to attempt a type conversion to "Decimal Number" and then filter for the resulting errors.