To handle NaN (Not a Number) values in Power Query, you must use the Replace Values feature to swap the specific "NaN" text or error with a null value or a zero, as Power Query does not treat NaN as a standard null. Because NaN is a floating-point representation of an undefined numerical result, it often bypasses standard "Remove Empty" filters, requiring a manual replacement to clean the dataset for analysis.

What You'll Need

  • Microsoft Excel or Power BI Desktop.
  • A dataset containing numerical columns imported via the Power Query Editor.
  • The data must be loaded into the Power Query window (Data > Get Data or Transform Data).

Step-by-Step

  1. Open the Power Query Editor: In Excel, go to the Data tab and select Queries & Connections. Right-click your query and choose Edit. In Power BI, click Transform Data from the Home ribbon.
  2. Identify the NaN Values: Scan your numerical columns. NaN values typically appear in columns formatted as Decimal Number or Fixed Decimal Number. Note that labels and menu options may differ slightly depending on your software version or region.
  3. Select the Target Column: Click the header of the column containing the NaN values. If multiple columns have this issue, hold the Ctrl key and click each column header to select them simultaneously.
  4. Access the Replace Values Tool: Right-click the selected column header and select Replace Values from the context menu. Alternatively, go to the Transform tab in the top ribbon and click Replace Values.
  5. Configure the Replacement: In the Replace Values dialog box, enter "NaN" exactly as it appears (case-sensitive) in the Value To Find field. In the Replace With field, enter the desired replacement. Common choices include:
    • Leave it blank to replace NaN with a null value.
    • Enter 0 if the NaN represents a zero value for calculation purposes.
    • Enter a specific mean or median value if you are performing data imputation.
  6. Apply the Change: Click OK. Power Query will add a "Replaced Value" step to the Applied Steps pane on the right side of the screen.
  7. Verify Data Types: After replacing NaN, check the data type icon to the left of the column header. Ensure it is still set to Decimal Number or Whole Number. If the replacement turned the column into a "Any" or "Text" type, click the icon and manually re-select the appropriate numerical type.
  8. Load the Cleaned Data: Once the NaN values are gone, click File > Close & Apply (Power BI) or Home > Close & Load (Excel) to bring the cleaned data into your worksheet or model.

Common Mistakes

One of the most frequent errors is attempting to use the "Remove Empty" filter to get rid of NaN values. In Power Query, "Empty" refers specifically to null values. Since NaN is technically a numeric value (representing an undefined result), Power Query does not consider it empty, and the filter will ignore it entirely. This leaves the NaN values in your data, which can cause errors in DAX measures or Excel pivot tables.

Another common mistake is case sensitivity. If you type "nan" or "NAN" in the Replace Values box but the data displays as "NaN", the software will not find any matches. You must match the casing exactly as it appears in the cell. If you are unsure, you can right-click a cell containing the value and copy it directly into the search box.

Users also frequently forget to check the data type after a replacement. If you replace NaN with a text string like "Missing", Power Query will automatically change the entire column type to Text. This prevents you from performing sums, averages, or other mathematical operations on that column. Always ensure the column returns to a numerical type after the replacement is complete.

Tips

For large datasets with dozens of columns containing NaN, avoid replacing values one by one. Instead, select all columns (Ctrl + A) and use the Replace Values tool. However, be cautious: this will replace the text "NaN" anywhere it appears, including in text columns where "NaN" might actually be a valid piece of information (though this is rare in most business datasets).

If you are dealing with NaN values that are actually resulting from a calculation error within Power Query (such as dividing by zero), it is more efficient to use a "Try... Otherwise" block in a Custom Column. This allows you to catch the error at the moment of creation rather than cleaning it up after the fact. For example, a formula like try [ColumnA] / [ColumnB] otherwise 0 will automatically replace any resulting NaN or Error with 0.

Consider using the "Replace Errors" feature if the NaN is being flagged as a Power Query Error (indicated by a red bar under the cell) rather than a value. Right-click the column and select Replace Errors. This is a faster way to handle systemic calculation failures across a dataset.

Troubleshooting

If the Replace Values step does not seem to remove the NaN, first check if the value is actually a string "NaN" or a system error. If there is a red line under the cell, it is an error, not a value. In this case, the "Replace Values" tool will not work; you must use the "Replace Errors" tool found in the right-click menu of the column header.

If you receive a "Type Mismatch" error after replacing NaN with a number, it is likely because the column was temporarily converted to a Text type during the replacement process. To fix this, look at your Applied Steps pane. Delete the "Changed Type" step that occurred immediately after the "Replaced Value" step, and then manually set the column type to Decimal Number again.

If the NaN values reappear after refreshing the data, check your source file. If the source is a CSV, ensure that the delimiter has not changed, causing numerical data to be shifted into text columns. If the source is a database, check if the SQL query is returning "NaN" as a string or as a floating-point null, as this changes how Power Query perceives the value.

Frequently Asked Questions

What is the difference between NaN and null in Power Query?

Null represents the total absence of a value (an empty cell), while NaN (Not a Number) is a specific numeric state indicating that a mathematical operation failed or is undefined, such as 0 divided by 0.

Can I use a formula to find NaN values?

Yes, you can create a Custom Column using the Number.IsNaN([ColumnName]) function. This returns TRUE if the value is NaN and FALSE otherwise, allowing you to filter or flag these rows specifically.

Will replacing NaN with 0 skew my average calculations?

Yes. Replacing NaN with 0 includes those rows in the denominator of an average, which will pull the mean down. If you want the average to ignore these rows, replace NaN with null instead.

Why does my column change to Text type when I replace NaN?

This happens if you replace the NaN value with a non-numeric string. Power Query automatically updates the column type to accommodate the new data. You can fix this by changing the type back to Decimal Number in the Transform tab.

Does the Replace Values tool work across multiple tables?

No, the Replace Values tool only works within the specific query/table you are currently editing. You must apply the step to each table individually or create a custom function to reuse the logic across multiple queries.

Can I remove rows containing NaN instead of replacing them?

Yes. The easiest way is to use the filter dropdown on the column header, uncheck "NaN" from the list of values, and click OK. This will generate a "Filtered Rows" step that excludes all NaN entries from your final output.