Power Query Number.NaN: A Practical How-To
In this article
Number.NaN is Power Query's built-in constant that represents an invalid or undefined numeric result — it is what you get when a calculation has no meaningful number to return, such as dividing zero by zero or converting a non-numeric string. You do not enable it or install it: you reference it in a formula, compare against it, or filter it out. The shortest working approach is to test a column with Number.IsNaN, replace flagged values with null or a fallback number, and then let your downstream steps work on clean data.
One important distinction up front: in Power Query, NaN is a numeric value, not text and not null. That matters because null propagates through arithmetic silently, while NaN can appear in results and confuse totals, charts and relationships. Treating NaN as its own case, rather than assuming it is the same as a blank, is the core skill this guide covers.
What You'll Need
- Power BI Desktop, Excel with Power Query (the Data > Get & Transform route), or another host that exposes the Power Query Editor. Menu labels differ slightly by version, host and region, so read the equivalent option if a name does not match exactly.
- A query with at least one numeric column, or any query where you can add a custom column.
- Basic familiarity with the Power Query Editor: the Applied Steps pane, the formula bar and the Advanced Editor.
- No add-ins or external tools. Number.NaN is part of the core M language library.
Step-by-Step
- Open the query in the Power Query Editor. In Power BI Desktop, select your table and choose Transform Data. In Excel, select a cell in your table and use Data > From Table/Range, or open an existing query from the Queries pane. You need the Editor, not the worksheet grid, because NaN handling is done in M.
- Identify where NaN can appear. NaN usually comes from three places: a division where the denominator is zero, a conversion of a non-numeric text value, or a calculation on a column that already contains NaN. Click through your Applied Steps and look at the numeric columns you have created or transformed.
- Add a diagnostic custom column. On the Add Column tab, choose Custom Column. Name it something like IsNaN and enter
Number.IsNaN([YourColumn]). This returns true or false for each row and gives you a quick count of affected rows. If the function name is not recognised in your version, check that you are using a current build rather than an older host. - Filter or replace the flagged rows. Two safe options. To remove them, filter the IsNaN column to false and then delete the helper column. To keep the rows, use Replace Values on the original column, or add another custom column such as
if Number.IsNaN([YourColumn]) then null else [YourColumn]. Replacing with null is usually better than replacing with zero, because zero is a real value and will distort averages. - Guard the calculation itself. If NaN is being produced by a division step, fix it at the source. Wrap the division in a conditional, for example
if [Denominator] = 0 or [Denominator] = null then null else [Numerator] / [Denominator]. This prevents NaN from being created in the first place and is cleaner than cleaning up afterwards. - Check the data type. Select the column header and confirm the type is a decimal or whole number, not Text or Any. A column typed as Any can hold NaN alongside text and numbers, which makes later steps unpredictable. Setting an explicit numeric type forces Power Query to evaluate the values consistently.
- Apply and close. Choose Close & Apply (or Home > Close & Apply) to load the cleaned query. Re-open the Editor and step through the Applied Steps once more to confirm no step reintroduces NaN after your cleanup.
Common Mistakes
Treating NaN as null. They are different values. A filter for null will not remove NaN, and an if [Column] = null test will not catch it. Use Number.IsNaN for NaN and a separate null check for blanks.
Replacing NaN with zero. This silently changes your averages, sums and ratios. Unless zero is genuinely the correct business value, replace with null and handle nulls in your report layer.
Comparing with equals. Writing [Column] = Number.NaN is unreliable because NaN is not equal to anything, including itself. Always use Number.IsNaN.
Cleaning too late. If NaN enters early and you clean at the end, every intermediate step has to cope with it. Fix it as close to the source as possible.
Assuming the error is in Power Query. Sometimes the source system stores a literal string such as "N/A" and the conversion step produces NaN. Check the raw values before blaming the function.
Tips
Use Number.IsNaN in a conditional column rather than a filter when you want to keep the rows for auditing. Name helper columns clearly, such as _IsNaN_Sales, so they are easy to spot and remove before loading.
If you work in the Advanced Editor, keep NaN handling in its own step with a descriptive name like "Replace NaN with null" so the Applied Steps pane reads like documentation. That makes the query far easier to maintain months later.
Remember that NaN can also appear after a merge or append if one source supplies text where another supplies numbers. Set types explicitly after those operations rather than relying on the detected type.
Troubleshooting
The formula returns an error instead of true or false. Check that the column reference is correct and that the function name is spelled exactly as Number.IsNaN. In the Custom Column dialog, function names are case-sensitive in practice even though M is largely case-insensitive for keywords.
NaN still appears after replacement. A later step is probably recreating it. Walk the Applied Steps from top to bottom and click each one, watching the column profile at the bottom of the window. The step where NaN reappears is your culprit.
The column will not change to a number type. There are likely text values mixed in. Use Replace Values or a conditional to convert them to null first, then set the type.
Totals look wrong even though NaN is gone. Nulls are ignored by most aggregations, which is usually what you want, but confirm your measure handles nulls explicitly if you need a zero-filled total.
If that did not work, duplicate the query, remove all steps after the source, and rebuild the transformation one step at a time, checking the column profile after each. This isolates whether the NaN originates in the source data or in your logic.
Frequently Asked Questions
Is Number.NaN the same as null in Power Query?
No. Null means no value was supplied; NaN means a numeric operation produced an undefined result. They behave differently in filters, comparisons and aggregations, so handle them separately.
Why does my NaN comparison fail?
Because NaN is defined as not equal to any value, including another NaN. Use Number.IsNaN instead of an equals test.
Can I type Number.NaN directly into a custom column?
Yes. Number.NaN is a valid constant and can be returned from a conditional expression, which is useful for deliberately marking rows that fail a validation rule.
Does replacing NaN with null affect my totals?
Most aggregation functions skip nulls, so totals and averages are computed from the remaining values. That is usually the desired behaviour, but verify it against your reporting requirements.
Where does NaN usually come from?
Division by zero, converting non-numeric text to a number, and calculations applied to columns that already contain NaN. Fixing the source step is more reliable than cleaning up afterwards.
Do I need a specific Power Query version?
Number.NaN and Number.IsNaN are part of the core M library and are available in current Power BI Desktop and Excel Power Query builds. If a function is not recognised, update your host application through its official update channel.