Power Query Comparer.Equals: How to Use It
In this article
Comparer.Equals is a Power Query M function that compares two values using a specified comparer and returns true or false. You use it when you need a comparison that is case-insensitive, culture-aware, or otherwise different from M's default behaviour. The function takes a comparer (such as Comparer.OrdinalIgnoreCase or Comparer.FromCulture) and returns a function that accepts two values and returns a logical result. It is most useful inside custom columns, table transformations, and when you need consistent comparison logic across a query.
What You'll Need
- Power BI Desktop, Excel with Power Query, or another tool that hosts Power Query (the M engine is the same, but menu labels may differ slightly by version, app, or region).
- A query with at least one table loaded, or the ability to create a blank query and enter M code manually.
- Basic familiarity with the Power Query Editor: the formula bar, the Advanced Editor, and adding custom columns.
Step-by-Step
- Open the Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, go to Home > Transform data. The exact label can differ slightly by version, but the editor opens the same way.
- Load the table you want to compare. Select your source and load it into the editor. If you only want to test the function, create a blank query and enter a record or list manually.
- Decide which comparer you need. The comparer controls how values are matched. Common choices are Comparer.Ordinal (case-sensitive, culture-independent), Comparer.OrdinalIgnoreCase (case-insensitive), Comparer.FromCulture("en-US", true) for a specific culture with case-insensitivity, and Comparer.Equals itself as the base function. Pick the one that matches your business rule.
- Add a custom column. Go to Add Column > Custom Column. Give the column a name, then enter a formula that calls Comparer.Equals. The pattern is: Comparer.Equals(comparer, value1, value2). For example, to compare two text columns case-insensitively: Comparer.Equals(Comparer.OrdinalIgnoreCase, [Column1], [Column2]).
- Check the result. Click OK. The new column should contain true or false for each row. If you see errors, check that both arguments are the same type and that the comparer is valid.
- Use it inside a larger expression if needed. Comparer.Equals returns a function when you supply only the comparer, so you can also write: let cmp = Comparer.Equals(Comparer.OrdinalIgnoreCase) in cmp([Column1], [Column2]). This is useful when you want to reuse the same comparer in several places.
- Apply the query. When the column looks correct, close and load the query. The comparison result becomes a normal column you can filter, count, or use in further steps.
Common Mistakes
- Forgetting the comparer argument. Comparer.Equals needs a comparer as its first argument. Calling it with only two values returns a function, not a logical result, which causes confusing errors downstream.
- Assuming case-insensitivity by default. The default comparison in many M operations is ordinal and case-sensitive. If you need "Apple" to match "apple", pass Comparer.OrdinalIgnoreCase explicitly.
- Mixing types. Comparing text to number returns false or an error depending on the comparer. Convert values to the same type first.
- Using Comparer.Equals where a simple = would do. For straightforward equality, the = operator is simpler and faster. Use Comparer.Equals only when you need a specific comparer.
- Ignoring culture. Comparer.FromCulture requires a valid culture name such as "en-US" or "de-DE". An invalid name produces an error.
Tips
- Store your comparer in a variable at the top of the query so you can change it in one place.
- Use Comparer.OrdinalIgnoreCase for identifiers, emails, and codes that should not be case-sensitive.
- Use Comparer.FromCulture when sorting or matching text that follows language-specific rules, such as accented characters.
- Test on a small table before applying the logic to a large dataset, since comparer choice can change which rows match.
- Combine Comparer.Equals with Table.SelectRows or Table.AddColumn for clean, readable transformations.
Troubleshooting
If the custom column shows errors, open the step in the formula bar and check the argument order: comparer first, then the two values. If the result is always false, confirm both columns are the same type and that the comparer matches your intent. If you see a function instead of true/false, you likely omitted one of the values. If culture-based comparison fails, verify the culture string. If the query is slow, consider whether a simple = comparison would work instead, since comparers add overhead. If labels in the ribbon differ from these steps, use the formula bar or Advanced Editor to enter the M code directly.
Frequently Asked Questions
What does Comparer.Equals return?
It returns true or false when given a comparer and two values. Given only a comparer, it returns a function that accepts two values.
Is Comparer.Equals case-sensitive?
It depends on the comparer you pass. Comparer.Ordinal is case-sensitive; Comparer.OrdinalIgnoreCase is not.
Can I use Comparer.Equals with numbers?
Yes, but the comparer still applies. For simple numeric equality, the = operator is usually clearer.
What is the difference between Comparer.Equals and Comparer.FromCulture?
Comparer.Equals is the function that performs the comparison. Comparer.FromCulture is a comparer you can pass into it to get culture-aware behaviour.
Why do I get a function instead of true or false?
You supplied only the comparer. Add the two values you want to compare as the second and third arguments.
Does Comparer.Equals work in Excel Power Query?
Yes. The M engine is the same in Excel and Power BI, so the function behaves the same way.