Power Query Problems

How to Replace a Power Query Value Based on Two Conditions

Huda QurayshiHuda Qurayshi Nov 10, 2026 869 views

Question details

The user needs to replace a value in a specific column only when two exact conditions are met across two different columns.

Replace a Power Query Value Based on Two Conditions
Product
Power Query (Excel)
Device & OS
not provided
Scenario
Editing data transformation steps in Power Query where an initial not-equal condition inadvertently affected incorrect rows.
Observed behavior
The replacement logic affected the wrong rows. The goal is to accurately target and replace values such as 'D26 Plumbing' with 'D26 Plumbing & Bath' only when the 'SiteCollection' is 'en_ca'.
Before you start

Ensure you have your data loaded into the Power Query Editor and that the column names in your M expression exactly match the headers in your dataset.

Solution 1Recommended

Use Power Query M Expression with Equality Tests

Apply a custom Table.ReplaceValue formula with an 'if...and...then' statement to ensure values are only replaced when both conditions are strictly met.

Using 'not-equal' logic in conditional replacements can often yield unexpected results because it applies broadly to anything outside the condition. Defining exact equality tests prevents the replacement logic from affecting the wrong rows.

1
Open Power Query Editor

Select your data table in Excel, go to the 'Data' tab, and click 'From Table/Range' to open the Power Query Editor.

2
Add a Custom Step

Click the 'fx' icon next to the formula bar to insert a new step, or go to the 'Advanced Editor' on the Home tab to modify your existing script.

3
Enter the M Expression

Input the following formula: =Table.ReplaceValue(#"Filtered Rows", each [Subsite], each if [SiteCollection] = "en_ca" and [Subsite] = "D26 Plumbing" then "D26 Plumbing & Bath" else [Subsite], Replacer.ReplaceValue, {"Subsite"})

4
Verify and Apply

Press 'Enter' to apply the formula. Check the preview grid to ensure only the targeted rows in the 'Subsite' column have updated. Click 'Close & Load' to return your transformed data to the spreadsheet.

Use Power Query M Expression with Equality Tests
Syntax check: Make sure your previous step name (e.g., #"Filtered Rows") exactly matches the first argument in the Table.ReplaceValue function.
Free Microsoft Office alternative

Try WPS Office for Seamless Data Management

While Power Query is a specific Microsoft Excel feature, WPS Office provides a highly compatible, free, and lightweight alternative for everyday spreadsheet tasks, complex data filtering, and analysis.

Fully compatible with Microsoft Excel (.xlsx, .xls, .csv) formatsBuilt-in advanced filtering and data replacement toolsFree, lightweight, and uses minimal system resourcesFamiliar tabbed interface for an easy transition without a steep learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Why did my original not-equal condition replace the wrong rows in Power Query?

A not-equal condition (like <> 'en_ca') applies to every single row that doesn't match, which often includes rows you didn't intend to target. Using exact equality tests (if A = 'x' and B = 'y') ensures only specific rows are modified.

Can I add a third condition to my Power Query replace value formula?

Yes. You can expand the 'if' statement by adding another 'and' operator. For example: if [Column1] = "A" and [Column2] = "B" and [Column3] = "C" then...

How do I conditionally replace multiple different values in the same column?

You can chain multiple conditions using 'else if'. For example: if [Subsite] = "D26 Plumbing" then "Bath" else if [Subsite] = "D27 Electrical" then "Wiring" else [Subsite].