Comparisons are extremely useful in any programming language or platform, and Power Automate is not the exception. The "less" function is the mirror image of the "greater" function. It takes two values and tells you if the first one is smaller than the second one, returning a boolean that you can use to drive your conditions.
So let's check it in more detail.
Usage
It follows a simple pattern.
- Integer, Float, or String to compare
- Integer, Float, or String for the comparison item.
Let's start with a simple example:
less(10,100)
will return
true
It also works with float numbers:
less(12.2,12.3)
will return
true
Now let's look at strings. They are much more complex to compare, and the results can be strange in some areas. For example:
less('a','b')
returns
true
Be careful using this to determine the alphabetical order. Things may not behave as expected when comparing strings, so this should be used with care for strings. If you want to sort, use the "sort" function, otherwise you can stay away (in my opinion) from strings to avoid issues.
It makes sense since we know that "a" comes before "b", but so does:
less('.a','b')
returns
true
Depending on how you look at it, it makes sense, or it's wrong. Another example is with a string that contains a number.
less('1','b')
returns
true
Depending on where you look, numbers come before or after when you sort alphabetically, so in this case, you can see that numbers are considered first. We can continue to find examples, but I think this proves my point. You can use it to understand if something is ordered, like if "Manuel" is "before" another name, but be careful and test your Flow. You may get unexpected results.
Let's look at an array:
less(createArray(1,2,3),createArray(4,5))
will return an error:
Flow run failed. Action 'Compose' failed: Unable to process template language expressions in action 'Compose' inputs at line '0' and column '0': 'The template language function 'less' expects all of its parameters to be either integer or decimal numbers. Found invalid parameter types: 'Array'.'.
No arrays allowed. Finally, let's try with boolean values:
less(true,false)
will return an error:
Flow run failed. Action 'Compose' failed: Unable to process template language expressions in action 'Compose' inputs at line '0' and column '0': 'The template language function 'less' expects all of its parameters to be either integer or decimal numbers. Found invalid parameter types: 'Boolean'.'.
The specification is apparent here. Boolean values are not comparable, but it's nice to try and be sure.
Limitations
Depending on the size of your string, your expression may return an error, even if it's correct. Please note that the expressions have a max size of 8,192 characters. If you have an expression that is even bigger than 1000, I would strongly advise breaking it into smaller, manageable formulas.
Finally, as we've seen above, arrays, boolean values, and other objects outside strings, integers, and floats are not allowed. I can't consider this as a limitation since it doesn't make sense to compare them, but I wanted to stress it here since, in some cases, we can use variables or the "Compose" action, for example, so that these values may exist.
Recommendations
Here are some things to keep in mind.
Use "debug" compose actions
Since the comparison will return true or false, sometimes it's tricky to understand how the calculation is done, depending on how complex the expression is. So I recommend using the "Compose" action to have the values that go "in" the function. This way, if the value doesn't make sense, you can understand, based on the parameters, why it was calculated that way.
Please don't use it on strings
Comparing if a string is "less" than another works, but it's pretty hard to understand the outcome, as we've seen above. So even if the string contains a number, use the "int" function first and then compare. Otherwise, there's no real reason to compare strings like this.
Don't nest
There's no real reason to do it. Furthermore, if you try to nest "less" functions, you'll receive an error since boolean values are not comparable. So be very careful to avoid breaking Flows.
Pair it with "lessOrEquals" when you need the boundary
If you need to include the boundary value in your comparison, the "less" function will leave it out, since it only returns true when the first value is strictly smaller. In those cases, the "lessOrEquals" function is the one you want.
Always add a comment
Adding a comment will help others understand your formula. Indicate what the function is doing and why, especially if the expression is complex. A short note like "checks if the order total is below the free shipping threshold" goes a long way when you come back to the Flow a few months later.
Final Thoughts
The "less" function is a small but reliable piece of your Power Automate toolbox. Use it for numeric comparisons, keep strings out of the picture when you can, and lean on the "Compose" action to inspect the values that feed into the expression. With those habits in place, your conditions will stay predictable.
Sources
Microsoft's "less" Function Reference
Back to the Power Automate Function Reference.
Photo by the blowup on Unsplash
No comments yet
Be the first to share your thoughts on this article!