Tip #1322: Incorrect increment and decrement in Power Automate

&tl;dr

When using Increment variable or Decrement variable actions, use coalesce function in the parameter to avoid unexpected results.

Explanation

It was a dark and stormy night. The tax was calculated in a Power Automate flow by looping through the array of applicable records and incrementing a total variable by the tax field value. The client threw a book at me and said that total tax is off by a few dollars. True story. (Except the throw and the book, the storm was real).

It’s not uncommon in Common Data Service (lol) to imply that when a decimal or money field contains null it’s equivalent to 0. For example, null tax means no tax or zero tax. The fun begins when you try to calculate total over an array of records.

What’s the result of this flow?

6? 7? 8? 9?

We wouldn’t be here if it was 6, right? The answer is 8. Increment variable action implies the value of 1 for the Increment By parameter is not provided (value is null).

The fix is easy – use coalesce function.

coalesce(items('Apply_to_each'),0)

And the result is now 6 which is about as much as Facebook pays in tax in a year. The above applies to Decrement variable action as well. Duh…

Cover photo by George Becker

Leave a Reply

Your email address will not be published. Required fields are marked *