Smart Systems for Home

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

Power Query Comparer.Ordinal Function: How to Use It

· 5 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

The Comparer.Ordinal function in Power Query performs a case-sensitive, culture-insensitive string comparison based on the binary values of characters. It returns -1, 0, or 1 to indicate whether one string is less than, equal to, or greater than another, making it essential for precise sorting and filtering in data transformations where case and character encoding matter. No external tools or installations are required—it's built into Power Query and available in all supported environments including Excel, Power BI, and Azure Data Factory.

What You'll Need

  • Microsoft Excel (2016 or later) or Power BI Desktop (latest version)
  • A table or query containing text columns to compare
  • Access to the Power Query Editor (via Data > Get Data or Transform Data)

Step-by-Step

  1. Open your Excel workbook or Power BI Desktop file and load the data you want to compare into Power Query by selecting your data range or table, then clicking Transform Data or Get Data > From Table/Range.
  2. In the Power Query Editor, identify the two columns you wish to compare. For example, if comparing product names from two sources, ensure both are text type—right-click a column header and choose Change Type > Text if needed.
  3. Go to the Add Column tab and click Custom Column. In the dialog box, enter a name like ComparisonResult in the New column name field.
  4. In the Custom column formula box, type: Comparer.Ordinal([Column1], [Column2]). Replace [Column1] and [Column2] with the actual names of your columns as they appear in Power Query. For example: Comparer.Ordinal([ProductName], [ProductName_Copy]).
  5. Click OK. A new column will appear with values of -1, 0, or 1. A 0 means the strings are identical in case and character order; -1 means the first string is lexicographically smaller; 1 means it is larger.
  6. To sort data based on this comparison, click the dropdown arrow in the ComparisonResult column header and select Sort Ascending or Sort Descending to arrange rows by their comparison outcome.
  7. To filter for only matching strings, click the filter dropdown in the ComparisonResult column, uncheck -1 and 1, and leave only 0 selected. This isolates rows where the two text fields are identical.
  8. To use this in a conditional column, go to Add Column > Conditional Column. Set the condition to ComparisonResult = 0 and return a value like "Match", otherwise return "Mismatch".
  9. Click Close & Apply to load the transformed data back into Excel or Power BI.

Common Mistakes

  • Using Comparer.Ordinal on non-text columns—this causes an error. Always ensure both inputs are text type before calling the function.
  • Typing Comparerordinal (lowercase) instead of Comparer.Ordinal. Power Query is case-sensitive for function names.
  • Confusing Comparer.Ordinal with Comparer.OrdinalIgnoreCase. The latter ignores case differences, which may give unintended results if case matters (e.g., "Apple" vs "apple").
  • Assuming the function returns true/false—it returns integers (-1, 0, 1). You must use comparison operators (like =, <, >) in conditions to interpret the result.
  • Trying to use it directly in a filter without creating a custom column first. The function must be evaluated in a column context.

Tips

  • Use Text.Trim before Comparer.Ordinal if whitespace differences might cause mismatches: Comparer.Ordinal(Text.Trim([Column1]), Text.Trim([Column2])).
  • For debugging, add a second custom column with Text.Length([Column1]) and Text.Length([Column2]) to spot hidden characters causing mismatches.
  • When comparing multilingual text, be aware that Comparer.Ordinal uses Unicode code points. A character like "é" (U+00E9) is different from "e" + combining acute (U+0065 U+0301), even if they look identical.
  • Combine with List.Sort for custom sorting: List.Sort(YourList, Comparer.Ordinal) sorts a list of strings in exact binary order.
  • Use this function when you need to detect differences in capitalization, accents, or encoding that other comparison functions might overlook.

Troubleshooting

  • Error: "We cannot apply operator Comparer.Ordinal to types Text and Number" — One of your columns is not text. Go back to the source table, right-click the column, and select Change Type > Text.
  • Result is always 0 even when strings look different — Check for invisible characters (like non-breaking spaces, zero-width spaces). Use Text.Replace to remove them or inspect with Text.ToBinary to view raw byte values.
  • The custom column doesn’t appear — Ensure you typed the formula correctly and clicked OK, not just pressed Enter. If you see a red error, hover over it to see the exact syntax issue.
  • Function not recognized — You may be using an outdated version of Excel or Power BI. Update to the latest version via Microsoft Update or the Power BI Desktop download page.
  • Sorting doesn’t work as expected — Check that the column type is set to Integer (right-click column > Change Type > Integer). If it’s still text, sorting will be alphabetical, not numerical.

Frequently Asked Questions

Can Comparer.Ordinal compare dates or numbers?

No. It only works with text. Convert dates or numbers to text first using Text.From() if you need binary comparison, but be aware that formatting (like "1/1/2024" vs "2024-01-01") will affect results.

Is Comparer.Ordinal faster than other comparison methods?

Yes, because it compares raw byte values without cultural rules or case conversions. It’s the fastest text comparison available in Power Query for exact matching.

Does it work with null values?

No. If either input is null, the function returns an error. Wrap inputs in if [Column1] <> null and [Column2] <> null then Comparer.Ordinal([Column1], [Column2]) else null to handle nulls gracefully.

Can I use Comparer.Ordinal in DAX?

No. This function is exclusive to Power Query M language. In DAX, use EXACT() for case-sensitive text comparison.

Why is "A" less than "a" in Comparer.Ordinal?

Because in Unicode, uppercase letters come before lowercase letters. "A" is U+0041 and "a" is U+0061, so "A" has a lower code point and returns -1 when compared to "a".

Can I use this for sorting non-English text like Chinese or Arabic?

Yes. It sorts by Unicode code point order, so Chinese characters will be ordered by their Unicode values, not by pronunciation or stroke count. This is useful for consistent, predictable sorting regardless of language.