November 29, 2024

Variables are one of the most valuable actions in Power Automate , and today, we’ll look at how the “decrement variable” action works.

The “decrement variable” action will get an “Integer” or “Float” variable and decrement it by the value that you provide it. It’s pretty simple but super powerful since having an action that we can do this safely can save us from some issues.

So the concept is simple. You have a variable with a value, and after the “decrement variable,” the variable will contain a value that is the difference between it and the value provided.

Where to find it?

You can find it in the “Build-in” action.

You may need to extend it and search for “Variables”.

Select “variables”.

Select the “decrement variable” action.

Here’s what it looks like.

Pro Tip: Power Automate tends to save the most common actions in the main screen, so check there before going through the full hierarchy. Also, you can use the search to find it quickly.

Now that we know how to find it let’s understand how to use it.

Usage

The first thing to understand is that the “decrement variable” action needs a variable to decrement. It may look obvious, but sometimes it would be helpful to define a variable and decrement it “on the spot,” but it’s not possible.

If you don’t have a variable, here’s what it would look like.

Not very useful, right?

Another thing to consider is that the “Decrement Variable” action will only work if the variable is an “Integer” or a “Float.” Even if you have a string variable that contains a number, it won’t work. We can easily see it here:

Let’s see it in action now that we know what we can use. First, let’s see how to decrement it to a numeric value:

Notice that we’re not even initializing the variable with a value. This is a nice touch from Microsoft, where if the variable doesn’t contain an initial value, then Power Automate considers it zero.

How about if we want to decrement an “Integer” variable with a floating point number? Like this:

Power Automate will do the “right thing” and block us from doing it since we cannot have a valid result.

How about the opposite? If we have a “Float” variable and want to add a numeric value?

No problem since the value makes sense in the end:

Finally, let’s try the floating point value on a “Float” variable.

No problem. The value is possible to decrement, so Power Automate does it without an issue.

Non-intuitive behaviors

Usually, in Power Automate, we typically use the actions’ outputs where we get the data. For example, if we have a “compose” action, we fetch the result of the expression on the “compose” action output. But in the case of the “decrement variable” action, we are not doing that. We are changing the content of the variable that exists elsewhere, meaning that if we want the decremented value, we need to access the variable.

It makes sense if you think about it, but I wanted to highlight this because it’s something that escapes a bit from the “norm” on how Power Automate works.

Limitations

As far as I know, there’s no severe limitation to the “decrement variable” action, but if you’re aware of one, I would like to know about it. Please email me , and I’ll include it here.

Recommendations

Here are some things to keep in mind.

Name it correctly

The name is super important in this case since we can have a lot of variables per Flow. Always build the name so that other people can understand what you are using without the need to open the action and check the details.

Always add a comment

Adding a comment will also help avoid mistakes. Indicate the variable’s usage and why you’re decrementing the value. Also include the meaning of the value itself, for example, “decrements the value by 1 to count the number of items left to parse in the array”. It’s essential to enable faster debugging when something goes wrong.

Always deal with errors

Have your Flow fail graciously and notify someone that something failed. It’s horrible to have failing Flows in Power Automate since they may go unlooked-for a while or generate even worse errors. I have a template that you can use to help you make your Flow resistant to issues. You can check all details here.

Am I missing something? Leave a comment or interact on Twitter. Let’s work together to make this reference as complete as we can

Back to the Power Automate Action Reference.

Photo by Patrick Tomasso on Unsplash

 

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

Leave a Reply

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

Mastodon