top of page

Conditions in PowerApps

Sean Foote

PowerApps takes the simple Excel if() function and powers it up to control everything.


For example, if you wanted to change the color of some text, change the text or hide the text, the syntax is the same and simple.



There are two primary conditions functions, If() and Switch(). For now, let's just focus on If(). The syntax is the same in Excel and in PowerApps:


Single Conditions


If(<Condition>, <Result if true>, <Result if false>)


Generally I think it makes sense to space out If Statements like this in PowerApps using Shift+Enter to add carriage returns to the code:



If(2 = 2, // This is the condition. Right now, it's checking to see if 2 = 2

"value if true", // This is the value if the condition evaluates to true. Since 2 does in fact equal 2, this is the result

"value if false" // This is the value if the condition evaluates to false. This would be the value if 2 didn't equal 2 ) //End If(2 =2 In big complicated formulas, it's nice to have comments like this that make it easy to read.


This can be used in any context to return Colors, Strings, Tables, Form Modes etc.


Beyond Single Conditions


What if you had a situation where you wanted multiple inputs to be evaluated to determine a condition?


This means you'd like to add an operator to your condition. Here's the Microsoft Documentation on operators in PowerApps.


PowerApps is flexible here and allows you to write this in a variety of ways.


AND

For Example, "And" can be written like this:

2=2 And 2=3

2=2 && 2=3

And(2=2, 2=3)




If(2 = 2 And 2 = 3 , // This is the condition. Right now, it's checking to see if 2 = 2 and 2 = 3

"value if true", // This is the value if the condition evaluates to true. This would be the value if both statements were true

"value if false" // This is the value if the condition evaluates to false. This is the result since 2 does not equal 3 ) //End If(2 = 2 And 2 = 3


OR

For Example, "Or" can be written like this:

2=2 Or 2=3

2=2 || 2=3

Or(2=2, 2=3)





If(2 = 2 Or 2 = 3 , // This is the condition. Right now, it's checking to see if 2 = 2 or2 = 3

"value if true", // This is the value if the condition evaluates to true. This is the result

"value if false" // This is the value if the condition evaluates to false. This would be the value if both statements were false ) //End If(2 = 2 Or 2 = 3



ElseIf


What about when you want multiple conditions to be evaluated? For example, if this is true, then is that true or false. To evaluate this scenario, you'll need nested if statements.





If(2 = 3, // This is the condition. Right now, it's checking to see if 2 = 3

"value if true", // This is the value if the condition evaluates to true. Since 2 does not equal 3, this value will not show

If(2 = 2, // This is the value if the condition evaluates to false. And leads to a nested If Statement

"value elseif true", // This is the value if the condition evaluates to true. This is the result since 2 equals 2

"value elseif false" // This is the value if the condition evaluates to false. ) //End If(2 = 3

) //End If(2 = 2


Switch

Elseif's can get very complicated in a hurry. Every time another layer of condition is added on, more formula gets added. Instead, it may be better to use a Switch() function.


Switch(<Original Value>, <Match Value 1>, <Result Value 1>, <Match Value 2>, <Result Value 2>...<Default Result Value>)



Switch(2,

1, //Match Value 1

"Return if Original Value is 1",//Result Value 1

2, //Match Value 2

"Return if Original Value is 2",//Result Value 2 3, //Match Value 3

"Return if Original Value is 3",//Result Value 3

4, //Match Value 4

"Return if Original Value is 4",//Result Value 4 "Return if No Match Values fit with the Original Value" //Default Result )//End Switch


This can be incredibly helpful for so many situations that makes it very easy and simple to reuse controls, screens, labels as versus creating copies of the same item with different visibility states based on different criteria. I really like this solution for having dynamic links from a Gallery which will take you to different screens based on values.


For Example, depending on a status of an item, navigate to a different screen:

Gallery.OnSelect =

Switch(ThisItem.Status,

"Ready",

Navigate('Ready Screen'),

"Needs Attention",

Navigate('Attention Screen'),

"Complete",

Navigate('Complete Screen')

)




There are times where Switch functions won't work and you'll be stuck with nested If's, but any time you can, I recommend using a Switch.


Extra Credit

Here are a few quick tips that are extra options if you're having trouble understanding someone else's formulas.


Other mode for If()

There is an additional mode for the If() function:

If(<Condition>,<Result if true>,<Condition>,<Result if true>,...<Default Result if no conditions return true>)


This may seem very similar to a Switch(), but a Switch cannot evaluate a condition. This can evaluate multiple conditions and return values based on each of them. It is still limited in terms of stacking conditions and you still may need nested If statements, but please be aware this exists.


Places where If() is implied


PowerApps is intelligent and can determine context. For example in the visibility property of controls, you don't need to write an If statement.


Label.Visible = If(2 = 2, true, false)



While that will work and may be easier for beginners to read, be aware that PowerApps will evaluate a condition to true or false without the rest of the formula.




2,305 views0 comments

Recent Posts

See All

コメント


Contact Me

  • LinkedIn Social Icon
  • Twitter Social Icon

© 2019 Sean Foote . Proudly created with Wix.com

Icons made by Freepik from www.flaticon.com

Have an idea for a new topic or a question?

Success! Message received.

bottom of page