Boolean logic in Power Apps

This post is all about Boolean logic in Power Apps. So that is all the true/false stuff within your app.

true and false

In short we have two values that are important in Boolean logic. And we actually have 3 values that are relevant:

  • true
  • false
  • null/empty/nothing/undefined ( or whatever you would like to call this)

In Power Apps we can now use this true and false to check if something is true or if it is false (yes this sounds indeed very logical!)

If (true, 
  Set(varResult, "The value is true"),  
  Set(varResult, "The value is false")
)

Now we could take this a bit further

Set(varVariable, true);
If (varVariable, 
  Set(varResult, "The value is true"),  
  Set(varResult, "The value is false")
)

Or we can use operations like =, <>, >=, <=, <, > to check values of variables.

Set(varVariable, Len("My Text") = 7);
If (varVariable, 
  Set(varResult, "The value is true"),  
  Set(varResult, "The value is false")
)

So far it’s all easy and not very exciting.

Not

Then to reverse trues into false and false into true you could also use

Not(true)

or if you prefer the ! can also be used instead of Not.

!true

And and Or

Now that we have true and false sorted out, we might want to combine multiple true and false values using the And and/or Or .

So for exmaple if we want to check if a text variable has 7 characters and it starts with a letter “t” then we could use the following logic:

Len(varMyText) = 7 And StartsWith(varMyText, "t")

In a similar way we can use Or.

Similar to the Not shorthand of ! for And and Or we can also use && and ||

Brackets

As you build up the logical complexity of your Boolean logi you might need to use brackets, ( ), to make sure that the right elements are run first.

Compare the following code:

Len(varMyText) = 7 And StartsWith(varMyText, "t") Or StartsWith(varMyText, "b")

And the following code will be evaluated very differently.

Len(varMyText) = 7 And (StartsWith(varMyText, “t”) Or StartsWith(varMyText, “b”))

Optimizing code

Before I get to the order of execution of the logic I wanted to quickly look at some code optimizations. Remember Less Code More Power!

If(Len(varMyText) = 7, 
   Set(varResult, true), 
   Set(varResult, false))

I’ve seen so many times people create code similar to the above. If all you’re doing is set a variable to true or false within an If function, then you could optimize your code like this:

Set(varResult,Len(varMyText) = 7)

This of course is a simple example and once you have a few And and Ors included it could make your code more complicated, however doing less is better.

Left to right ordering

Now I would like to consider errors in the middle of your code. As an example, I’m going to use the Index function. The Index Function will take the nth item in your array. So in the following code the Index function will take the 3rd item from my array as my array only contains one item we will see the following error:

Boolean logic in Power Apps Microsoft Office 365 image 22
Set(varMyArray,["Test"]);
Set(
    varDetails,
     ( CountRows(varMyArray) = 1 And Index(varMyArray, 3)
    ).Value = "test" ) 
);

One important thing to note is that the true and false logic will be evaluated from left to right and once we know the result any further test are to be ignored.

Now if we take this a bit further. I could imagine writing the following code. The CountRows is protecting the Index function from collecting the invalid item in an array.

Boolean logic in Power Apps
Set(varMyArray,["Test"]);
Set(
    varDetails,
     ( (CountRows(varMyArray) = 0) Or 
       (CountRows(varMyArray) = 1 And Index( varMyArray,1).Value = "Test") Or
       (CountRows(varMyArray) = 2 And Index( varMyArray,2).Value = "Test" )
));

Or how about if we took the same code but just reorder the lines a bit.

In the example below we will find that the above error will appear, as the first bit of the logic that gets the second item from my array will be run first as my array only has one item in it this will fail.

Set(varMyArray,["Test"]);
Set(
    varDetails,
     ( 
       (Index( varMyArray,2).Value = "Test" And CountRows(varMyArray) = 2) Or
       (Index( varMyArray,1).Value = "Test" And CountRows(varMyArray) = 1)  Or
       (CountRows(varMyArray) = 0)        
));

Optimizing the code

Now if we take the optimization steps into account. And the left to right evaluation of the logic, we can cut out quite a bit of the complexity and the following code will work:

Set(varMyArray,["Test"]);
Set(
    varDetails,
     ( 
       CountRows(varMyArray) = 0 Or
       Index( varMyArray,1).Value = "Test" Or
       Index( varMyArray,2).Value = "Test" 
));

As the code is evaluated. The CountRows function will be false. The first Index function returns true and the second Index function is ignored as false OR true OR Whatever makes the last part irrelevant. In Power Apps the Index( varMyArray,2).Value = “Test” part of the expression is never run.

So in the above example we have see that when multiple Boolean values are compares with the Or operator the first true stops the rest of the code from running.

When we do the same with an And then we will see that the first false makes the rest of the Boolean logic irrelevant.

Continue Reading Pieter Veenstra’s Article on their blog

Boolean logic in Power Apps

This post is all about Boolean logic in Power Apps. So that is all the true/false stuff within your app. In short we have two values that are important in Boolean logic.

Blog Syndicated with Pieter Veenstra’s Permission

Author: Pieter Veenstra

Share This Post On