Smart Systems for Home

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

Power Query If Null: How to Replace or Handle Nulls

· 6 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 handle null values in Power Query, the fastest method is to use the Replace Values feature for a global change, or a Conditional Column for logic-based replacements. Power Query treats null as a specific marker for missing data, and because it is case-sensitive and distinct from an empty string or a zero, you must target the null keyword specifically to remove or replace it.

What You'll Need

  • Microsoft Excel or Power BI Desktop.
  • A dataset loaded into the Power Query Editor (Get & Transform Data).
  • Knowledge of which columns contain the null values you wish to address.

Step-by-Step

Method 1: Using Replace Values (Global Replacement)

Use this method when you want every single null in a specific column to be replaced by a consistent value, such as a zero for numeric columns or "Unknown" for text columns.

  1. Open the Power Query Editor window.
  2. Right-click on the header of the column containing the null values.
  3. Select Replace Values... from the context menu.
  4. In the Value To Find box, type null (all lowercase). Do not put it in quotes; Power Query recognizes the lowercase word null as the special null value.
  5. In the Replace With box, enter your desired replacement value. For example, enter 0 for a currency column or N/A for a category column.
  6. Click OK. Power Query will generate a Table.ReplaceValue step in your Applied Steps list.

Method 2: Using Conditional Columns (Logic-Based Replacement)

Use this method when the replacement value depends on another column, or when you want to keep the original column intact and create a new "cleaned" version of the data.

  1. Navigate to the Add Column tab on the top ribbon.
  2. Select Conditional Column.
  3. In the dialog box, give your new column a name (e.g., "Cleaned Sales").
  4. Under Column Name, select the column that contains the nulls.
  5. Under Operator, select equals.
  6. In the Value field, type null.
  7. In the Output field, enter the value you want to appear when the cell is null.
  8. In the Else field at the bottom, select Select a column from the dropdown and choose the original column. This ensures that if the value is NOT null, it simply copies the existing data.
  9. Click OK.

Method 3: Using a Custom Column (M Code)

For more complex scenarios, such as checking for nulls across multiple columns simultaneously, a custom column using the if...then...else syntax is most effective.

  1. Go to the Add Column tab and select Custom Column.
  2. Enter a name for the column.
  3. In the Custom column formula box, use the following syntax: if [ColumnName] = null then "Replacement Value" else [ColumnName].
  4. If you need to check two columns (e.g., use Column B if Column A is null), use: if [ColumnA] = null then [ColumnB] else [ColumnA].
  5. Click OK.

Method 4: Fill Down or Fill Up

In many time-series datasets, a null value actually means "same as the value above." Power Query has a built-in tool for this specific behavior.

  1. Select the column containing the nulls.
  2. Go to the Transform tab.
  3. Click the Fill dropdown menu.
  4. Select Down to replace nulls with the most recent non-null value above them, or Up to use the value below.

Common Mistakes

One of the most frequent errors is typing "Null" or "NULL" with capital letters. Power Query's formula language (M) is strictly case-sensitive. If you enter "Null" in the Replace Values dialog, Power Query will look for the literal text string "Null" rather than the system's null marker, and no values will be replaced.

Another common mistake is confusing a null value with an empty string. A null is the total absence of data, whereas an empty string is a piece of text data that happens to have zero characters. If you try to replace null but the cells are actually empty strings, the operation will do nothing. To fix this, you may need to use Transform > Replace Values and leave the "Value to Find" box completely empty to target empty strings.

Users also frequently forget to check the data type of the column before replacing nulls. If you replace a null in a "Decimal Number" column with the text "Missing", Power Query will change the entire column type to "Any" or "Text", which may break subsequent mathematical calculations or pivot tables in your report.

Tips

To quickly identify where your nulls are located in a large dataset, use the Column Quality tool. Go to the View tab and check the Column Quality box. This will display a small bar under each header showing the percentage of Valid, Error, and Empty (null) values, allowing you to target your cleaning efforts without scrolling through thousands of rows.

When using Conditional Columns, it is often better to create a new column rather than replacing values in place. This preserves the original data audit trail, allowing you to verify that the replacement logic worked correctly before you delete the original "dirty" column.

If you have a massive dataset and are performing multiple "Replace Value" steps on the same column, consider writing a single Custom Column formula instead. Each "Replace Value" step creates a new transformation in the Applied Steps list, which can slow down the refresh time of your query as the dataset grows.

Troubleshooting

If you followed the steps but the nulls are still there, first check if the cells are truly null. Click on a cell; if it says null in italicized grey text, it is a true null. If the cell is just blank, it is likely an empty string. To resolve this, right-click the column, select Transform, and then Trim or Clean to remove hidden whitespace that might be preventing a match.

If you receive a "Type Mismatch" error after replacing nulls, it is because your replacement value does not match the column's data type. For example, putting "N/A" (text) into a column formatted as "Whole Number" will cause an error. To fix this, change the column data type to "Text" before performing the replacement, or use a numeric replacement like 0.

If the Fill Down feature is not working, ensure that the values you want to fill are actually null. Fill Down ignores empty strings and zeros; it only acts upon true null markers. Use the Replace Values tool to replace empty strings with null first, then apply the Fill Down command.

Frequently Asked Questions

How is a null different from a zero?

A zero is a numerical value indicating a quantity of nothing. A null is a state indicating that the value is unknown or missing entirely. In calculations, 10 + 0 = 10, but 10 + null = null.

Can I replace nulls across multiple columns at once?

Yes. You can select multiple columns (hold Ctrl and click the headers), then right-click and select Replace Values. The replacement will be applied to every selected column simultaneously.

Does replacing nulls affect the original data source?

No. Power Query is a non-destructive transformation engine. It reads the data from the source and applies steps to a copy of that data. Your original Excel file or SQL database remains unchanged.

What is the best way to handle nulls in a date column?

Since you cannot put text like "Missing" in a date column, the best approach is to either leave them as nulls (which Power BI/Excel handles as blanks) or replace them with a "placeholder date" like 1/1/1900, though this can skew your averages and timelines.

Why does my conditional column show "null" even after I set a replacement?

This usually happens if the Else part of your logic is not configured correctly. Ensure the Else field is set to select the original column; otherwise, any row that isn't null might be assigned a default value or another null.