I have to admit that the “sub function” and all the other analogous functions (add, mul and div) are quite strange. This function will subtract two numbers. Having functions to do basic arithmetic is not common in programming languages or platforms. Usually, we can do things like “number – number,” but not in Power Automate. This is a source of many “head-scratching moments” since the function looks correct with a minus sign, but Power Automate will return an error.
It follows a simple pattern.
The function will return the first minus the second number.
sub(4,2) will return 2
It will always return a number.
There’s a hard limitation on the size of the numbers you can pass, in this case, 19. Power Automate will return an error for:
But it won’t return an error for:
However, not returning an error doesn’t mean that the calculation is correct. In the first case, we would expect “1234567890123456788,” but instead, we get “1234567890123456800”. Notice that after the 18th number, all is truncated to zero. This may be a source of errors since no error is returned, but instead, you get an invalid calculation. Be very careful with the size of your number since it won’t return an error, but the result is incorrect.
- Keep the numbers as small as possible and all in variables. Don’t put the numbers directly in the function, at least the first one. It’s quite useful to have the second hardcoded when performing some pre-defined actions like decrementing a variable.
- Don’t nest it. You may nest operations, but it will make debugging a lot harder. Do the calculations in individual steps to see the results in each one and see if you get the desired value.
- Please note that formulas may have localization-based differences. For example, you should write “sub” with “separating each of the arguments, but if your regional settings are set to Portugal, you should use “;” instead.
Microsoft’s sub Function Reference
Back to the Power Automate Function Reference.
Photo by Samuel Regan-Asante on Unsplash