Learning Power Apps with Clarissa #4 – What If? Logic Functions Explained #1

Image: PowerApps webinar clarissa header#4
By Power Platform Team on

Power Apps – This blog series

Power Apps is a fantastic tool that can allow you to quickly create incredible, powerful, and visually engaging applications to replace your manual business processes. It’s an extremely fun platform to work with that empowers everyone to be able to create professional applications, regardless of background, with an ever-growing collection of incredible features.
The one down side? With the breakneck pace that Microsoft are introducing new functionality, and the myriad of features available, it can be quite daunting trying to find how to start learning to use Power Apps. But never fear! This blog series is designed to take you through my experiences learning Power Apps and provide you with the right steps to take to start your very own PowerApps Journey!

Learning Power Apps with Clarissa #4 – What if? Logic Functions Explained #1

Last time we had a broad look at Functions, how we can think of them as behaviours, or instructions, and how they can take arguments and have return values. We looked in detail at the ‘RGBA’ and ‘Navigate’ functions, but there are a whole host of functions yet to be discovered!
Today we’ll look at some of the most useful functions available that will quickly become your bread and butter: Logic Functions. As always, if you have any questions, suggestions or feel any explanations are unclear, feel free to contact me on Twitter @ClarissaGillin2!

Logic Functions

What are Logic Functions and why would I need them?

While any Function will enable an application to act in a defined way, Logic Functions enable an application to react to its environment and perform different behaviours based on given conditions.
As always, it’s easier to grasp these concepts when we consider a real-life example, so let’s dive straight in!

If

Imagine you are getting your laundry ready to go in the washing machine. In order to make sure you don’t end up having to go to work in a previously-white-but-now-multicoloured shirt, it’s best to do a dark wash and a light wash*. So, when we put our clothes in for wash, or before we put them in the machine, we need to separate them into two loads: clothes that are dark-coloured and clothes that are light in colour.
If we were to order a robot to sort our laundry for us, we might say to them, “Put all the dark clothes in this pile, and all the light clothes in that pile.” In order to execute this instruction, the robot would need to perform some logic. For each item it picks up it will have to determine whether it is light or dark, and then put the item onto the correct pile.

How would we structure this instruction?

The magic word is ‘If’:

  1. Pick up a piece of clothing.
  2. If it is light-coloured, then put it on the light-coloured pile.
  3. If it is dark-coloured, then put it on the dark-coloured pile.”

Notice now our instruction is structured in an “If this, then do that” format. This allows the computer to evaluate a condition (the ‘this’) and choose the appropriate behaviour (the ‘that’) to execute. This comparison is what gives the instruction its conditional nature.
We can extend this further. The computer will start by evaluating the condition: “If it is light-coloured”. This will result in either a true (it is light-coloured), or a false (it is not light-coloured). In our scenario we know that if it is not light-coloured it is, by necessity, dark-coloured, so we don’t need to check again to see whether it is dark-coloured in Step 3. Instead we can say the following:

  1. Pick up a piece of clothing.
  2. If it is light-coloured, then put it on the light-coloured pile.
  3. Else, put it on the dark-coloured pile.

Now we have structured this in the form “If this, then do that, else do something else”.

This format is perfect for using in our Power Apps. If we check the Power Apps formula documentation, we see that the given Syntax is:

  • If( Condition, ThenResult [, DefaultResult ] )

This can look intimidating, but if we compare it to our statement above, we see that it is actually saying the exact same thing. We start off with a Condition (If this), followed by a ThenResult (which is our then do that), and then have an optional DefaultResult (which is our else do something else).
So:
If it is light-coloured, then put it on the light-coloured pile. Else put it on the dark-coloured pile.”
Becomes:
If( Clothing is light-coloured, Put on light-coloured pile, Put on dark-coloured pile )
This means that, so long as we can word our instruction in the sentence “If this, then do that, else do something else”, we can write it in code using an If Function!

*Disclaimer: I myself am far too lazy to do a dark wash and a light wash; thank goodness for colour catchers!

In Practice

Laundry is no fun, so let’s have a look at this in an app.
Imagine we want to ‘simulate’ a set of traffic lights. For the red light, we would want it to have two statuses:
If the light is on, then the light should be a bright red, else the light should be a dark red.
We can do this by altering the Fill property of a Button:

In this clip we have given the button an OnSelect property which turns a variable “ButtonIsPressed” to true. In the Fill property we have our If statement: If the button is pressed, then the fill is bright red, else the fill is dark red, which in code is:
If(
ButtonIsPressed,
Red,
DarkRed
)

If we change this OnSelect property to instead toggle whether “ButtonIsPressed” is true or false, we can see that clicking the button flicks the light between off and on:

Let’s now add a green light to our traffic light. We’ll rename the variable “ButtonIsPressed” to “RedLightIsOn” and introduce a “GreenLightIsOn” variable to control the green light:

 

Now If the Red Light is On, then the fill for the Red Light button is Bright Red and the fill for the Green Light button is Dark Green, else the fill for the Red Light button is Dark Red and the fill for the Green Light button is Bright Green; and we have the beginnings of our traffic light!

We can expand this entire situation to simulate a car approaching the traffic light and adjusting the behaviour of the car (i.e. whether or not it stops) based on whether the light is Red or Green.

 

It might surprise you to know that the above implementation uses If statements only  to control the behaviour of the car and the traffic light! Download the app here to have a play for yourself and see how it works!

What’s next?

Now we’ve started to scratch the surface of Logic Functions. Next, we’ll start to look into what to do when you have multiple conditions, exclusive conditions, or when you have more than two options for your behaviours (e.g. what if you introduced the amber light into the traffic light scenario, so it’s no longer a simple if red, then stop, else go?). In the meantime, you’re ready to start trying out some If statements yourself! Have fun!

I hope you’ve enjoyed this next step on the way to becoming an App Maker! If you have any questions, want to strike up a conversation, or want regular updates on all things PowerApps, Microsoft Flow and Power BI, make sure to follow me (@ClarissaGillin2), Ben Haynes (@disappointmentb) and @SilversandsLtd on Twitter.

If you have a potential Power Platform requirement and need help please contact us using the form below:

Contact us

  • This field is for validation purposes and should be left unchanged.

We have the expertise and the experience to provide specialist solutions and drive your business forward

Get in touch

How can we help you?

Get in touch

What updates would you like?

Subscribe