September 25, 2022

Many think that the not function is useless. Usually, the “not” function is used in comparisons, so if you’re comparing if something is not something else, why not compare if something is different? It makes sense when you think about it a bit further the not function can make things a lot easier to understand.

Let’s explore how to use the “not” function.

Usage

It follows a simple pattern. It receives true or false and negates it. For example:

not(true)

will return 

false

It’s a dumb example, but it’s essential to understand that the parameter needs a boolean value. If you try with something else, for example, a 1, you’ll get the following error:

Unable to process template language expressions in action 'Compose' inputs at line '0' and column '0': 'The template language function 'not' expects its parameter to be a boolean. The provided value is of type 'Integer'. Please see https://aka.ms/logicexpressions#not for usage details.'.

The 1 and 0 are usually alternative representations of true and false, respectively, but in this case, it’s considered as integers. Thes same happens with the string “true”. For example:

not('true')

You’ll get the same error as above.

What about arrays with boolean values? To test it, let’s use the createArray function.

not(createArray(true))

We’ll get the following error:

Unable to process template language expressions in action 'Compose' inputs at line '0' and column '0': 'The template language function 'not' expects its parameter to be a boolean. The provided value is of type 'Array'. Please see https://aka.ms/logicexpressions#not for usage details.'.

Always the same issue. We need to provide boolean values or functions that return a boolean value.

So let’s look at some examples where it makes sense to have the not function.

The first is to validate data. Let’s imagine that we want to check if a task is not completed. We have a percentage that can be anything between 0 and 100%. So we would like to check if the percentage is not 100%. The alternative would be too complex to check if it’s equal to 1 and 2 and 3 and so on. It is cumbersome, to say the least, even if you don’t think about the decimal numbers possible. It’s a lot easier to check if it’s not 100%. Any other value is ok, like:

not(equals(item()?['Completed'], '1'))
You can see a “live” example of this function in action on my Power Automate: Multiple Conditions in Filter Array article.

Another example can be to check if something is incorrect. Notice the phrasing if something is incorrect. The mindset is if something is wrong, we’ll do something (the positive path), and if it’s correct, we won’t do anything, for example (the negative direction).

For example:

not(equals(item()?['Author_Name'], 'Manuel Gomes'))

Imagine that I’m using Power Automate to upload the article to my server, and I want to ensure that the author is correct by using the equals function.

One can argue that you can check if it’s equals and if not then fit it. It’s also a good approach, but I always recommend having the condition action match the way you think. In your mind, if the sentence goes “if it’s wrong, then I need to fix it,” then you think that the “true” path is the one to go, so you should use the “not” function. If you think, “if it’s correct, then I don’t need to do anything,” then you can use the “false” path to process the results.

Since I’m the only author for this site (for now 😉), I would like to check if the name is the same as the one already defined. If not, I need to fix it. I can use the above expression to check the name and correct it before uploading.

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.

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 Compose actions 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.

Don’t nest

There’s no real reason to do it, but if you find yourself in a situation where you have nested not functions in a formula, you should review it and make everything more straightforward. You end up in the same place if you do it since a double negation will cancel it out. If this happens, either remove all the not functions or check if one should not be there.

Sources:

Microsoft’s not Function Reference

Back to the Power Automate Function Reference.

Photo by Clark Van Der Beken on Unsplash

Manuel Gomes

I'm a previous Project Manager, and Developer now focused on delivering quality articles and projects here on the site. I've worked in the past for companies like Bayer, Sybase (now SAP), and Pestana Hotel Group and using that knowledge to help you automate your daily tasks

View all posts by Manuel Gomes →

Leave a Reply

Your email address will not be published.