Power Query Value.Compare Function: Guide and Usage
In this article
The Value.Compare function in Power Query is used to determine the relationship between two values, returning -1, 0, or 1 based on whether the first value is less than, equal to, or greater than the second. This is primarily used in custom sorting logic, complex conditional columns, or within the Table.Sort function when a standard ascending or descending sort is insufficient for the business requirements.
What You'll Need
- Microsoft Excel (with Power Query/Get & Transform) or Power BI Desktop.
- A dataset loaded into the Power Query Editor.
- Basic familiarity with the Power Query formula bar and the "Add Custom Column" feature.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Identify the Columns to Compare: Ensure you have at least two columns (or one column and a static value) that you wish to compare. For example, a "Current Price" column and a "Target Price" column.
- Create a Custom Column: Navigate to the Add Column tab on the top ribbon and select Custom Column. This opens the Custom Column dialog box where you can enter your M code.
- Enter the Value.Compare Syntax: In the Custom column formula box, type the function using the following structure:
Value.Compare([Column1], [Column2]). Replace[Column1]and[Column2]with the actual names of your columns by double-clicking them from the available columns list on the right. - Evaluate the Result: Click OK. Power Query will generate a new column. You will notice the results are limited to three specific integers:
- -1: The value in the first column is smaller than the value in the second column.
- 0: The values in both columns are identical.
- 1: The value in the first column is larger than the value in the second column.
- Integrate into a Conditional Statement (Optional): Since
Value.Comparereturns a number, it is often wrapped in anifstatement for better readability. For example:if Value.Compare([Price], 100) = 1 then "Expensive" else "Budget". - Apply to Table.Sort (Advanced): If you are editing the M code in the Advanced Editor, you can use
Value.Compareinside aTable.Sortfunction to create a custom sort order. Instead of choosingOrder.Ascending, you can provide a custom comparison function that dictates exactly how rows should be ordered based on multiple complex criteria.
Common Mistakes
One of the most frequent errors when using Value.Compare is attempting to compare incompatible data types. Power Query is strictly typed. If you attempt to compare a Text value with a Number value, the function will return an error rather than attempting to convert the types automatically. Always ensure that both inputs are of the same data type (e.g., both Decimal Number or both Text) before applying the function.
Another common mistake is confusing Value.Compare with a simple logical comparison like [Column1] > [Column2]. While a logical comparison returns a Boolean (True or False), Value.Compare returns a numeric indicator. If you use Value.Compare inside an if statement but forget to compare the result to a number (e.g., writing if Value.Compare([A], [B]) then...), Power Query will throw an error because it expects a True/False value, not -1, 0, or 1.
Users also frequently forget that Value.Compare is case-sensitive when dealing with text. In Power Query, "Apple" and "apple" are not the same. If you need a case-insensitive comparison, you must first transform both columns to lowercase using Text.Lower() or uppercase using Text.Upper() before passing them into the Value.Compare function.
Tips
To make your Value.Compare results more intuitive for end-users, use a conditional column to map the numeric outputs to descriptive text. For example, create a rule where -1 becomes "Below Target", 0 becomes "On Target", and 1 becomes "Above Target". This transforms a technical numeric output into a business-ready report.
When working with dates, Value.Compare is extremely useful for calculating chronological relationships without needing to subtract dates to find the number of days between them. It provides a clean, standardized way to check if a date has passed a deadline or is still in the future.
For those working in the Advanced Editor, combining Value.Compare with List.Sort allows for the creation of highly specialized sorting logic that can handle nulls in a specific way (e.g., always forcing nulls to the bottom regardless of whether the sort is ascending or descending), which is often difficult to achieve with the standard GUI sort buttons.
Troubleshooting
If your custom column is filling with "Error" messages, the first step is to check the data types of the columns involved. Right-click the column header and select Change Type to ensure both are identical. If one is "Any" and the other is "Number", the comparison may fail inconsistently.
If the function is returning 0 when you expect 1 or -1, check for hidden whitespace in text columns. A value of "100 " (with a trailing space) is not the same as "100". Use the Transform > Trim function on your text columns to remove leading and trailing spaces before running the comparison.
If you are receiving a "Token Literal Expected" error in the formula bar, check your syntax. Ensure that column names are enclosed in square brackets [] and that the function name Value.Compare is capitalized exactly as shown. M is a case-sensitive language; value.compare (lowercase) will not be recognized by the engine.
In cases where you are comparing null values, be aware that Value.Compare handles nulls based on the underlying type. If you need specific behavior for nulls (such as treating a null as a zero), use the replacer function or a coalesce style approach: Value.Compare([Column1] ?? 0, [Column2] ?? 0).
Frequently Asked Questions
How is Value.Compare different from using the > or < operators?
The greater-than and less-than operators return a Boolean (True/False). Value.Compare returns a three-state integer (-1, 0, 1), which is more efficient when you need to distinguish between three possible outcomes (less, equal, greater) in a single function call rather than writing multiple nested if statements.
Does Value.Compare work with dates and times?
Yes, it works with any comparable data type, including Date, DateTime, DateTimeZone, and Duration. It evaluates them chronologically, where an earlier date is considered "less than" a later date.
Can I use Value.Compare to sort a table in a custom way?
Yes. By using it within the Table.Sort function in the M code, you can define a custom comparison logic that determines the order of rows, which is useful for non-alphabetical sorting (like sorting by a specific priority list).
What happens if I compare a number to a text string that looks like a number?
Power Query will return an error. It does not perform implicit type conversion. You must explicitly convert the text string to a number using Number.FromText() before passing it to Value.Compare.
Is Value.Compare case-sensitive for text?
Yes, it is case-sensitive. It compares the Unicode values of the characters. To perform a case-insensitive comparison, wrap both arguments in Text.Lower().