Power Query Comparer.Ordinal Function: How to Use It
In this article
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
- 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.
- 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.
- Go to the Add Column tab and click Custom Column. In the dialog box, enter a name like
ComparisonResultin the New column name field. - 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]). - 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.
- To sort data based on this comparison, click the dropdown arrow in the
ComparisonResultcolumn header and select Sort Ascending or Sort Descending to arrange rows by their comparison outcome. - To filter for only matching strings, click the filter dropdown in the
ComparisonResultcolumn, uncheck -1 and 1, and leave only 0 selected. This isolates rows where the two text fields are identical. - To use this in a conditional column, go to Add Column > Conditional Column. Set the condition to
ComparisonResult = 0and return a value like"Match", otherwise return"Mismatch". - Click Close & Apply to load the transformed data back into Excel or Power BI.
Common Mistakes
- Using
Comparer.Ordinalon non-text columns—this causes an error. Always ensure both inputs are text type before calling the function. - Typing
Comparerordinal(lowercase) instead ofComparer.Ordinal. Power Query is case-sensitive for function names. - Confusing
Comparer.OrdinalwithComparer.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.TrimbeforeComparer.Ordinalif whitespace differences might cause mismatches:Comparer.Ordinal(Text.Trim([Column1]), Text.Trim([Column2])). - For debugging, add a second custom column with
Text.Length([Column1])andText.Length([Column2])to spot hidden characters causing mismatches. - When comparing multilingual text, be aware that
Comparer.Ordinaluses 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.Sortfor 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.Ordinalto 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.Replaceto remove them or inspect withText.ToBinaryto 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 usingText.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 inif [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, useEXACT() for case-sensitive text comparison.