Subscribe

If() or Switch() in Power Apps

9th January 2023

In my 50th post on Low Code Lewis, I’ll explain when to use the If() function in Power Fx, and when the Switch() function comes in more handy! 😍

If()

The If() function in Power Fx sort of does what it says on the tin πŸ™‚

If() lets you test one or more conditions until a true result is found, where its corresponding or following value is then returned. Where If() doesn’t find a true result against one or more specified conditions, it is able to return a default value provided at the end of an If() statement.

If() is best used to evaluate a single condition against a single possible match or to evaluate multiple unrelated conditions against corresponding results.

If() isn’t necessarily the right function to use if we’ve got a single condition and multiple possible matches though.

Here’s an example of using If() to evaluate a single condition and result.

If(Value, Green, Red)

My statement here says that if a boolean variable is equal to true, then make my result Green which is a colour in Power Apps, and otherwise Red.

This is the same as the below, but the above is better practice when writing Power Fx.

If(Value = true, Green, Red)

Now, we can look at using If() to evaluate multiple conditions against their results.

If(Value = "New", Red, Value = "In Progress", Yellow, Value = "Complete", Green)

My statement here says that if Value is equal to “New” then the result should be Red, then it says that if Value is equal to “In Progress”, then the result should be yellow, and finally if Value is equal to “Complete”, then the result should be Green. In this case depending on what Value is equal to, the result will be what is returned and applied in the property we’ve put this formula into.

Switch()

Switch() is a better function to use when we need to evaluate a single condition against multiple potential results. For example when switching between 10 different status colours depending on a status value. If can also be used here as we’ve done so above, but then you need to repeat the condition in your formula for every possible match that could be returned, to then get your result. Switch() is a better function to use in this case.

Let’s take the second example we looked at above and improve it using Switch(). I’d simple need to write…

Switch(Value, "New", Red, "In Progress", Yellow, "Complete", Green)

Now this only looks slightly smaller in this case, but if you’re writing long conditional statements with lots of conditions and results, this is more efficient than using an If() and needing to continually write out the condition in your formula.

You can read more on the If() and Switch() functions by reading the documentation about them at learn.microsoft.com

I hope this post helped you better understand these two simple functions in Power Fx! If you didn’t understand anything or have any questions, feel free to comment down below and I’ll get back to you asap!

Posted in Power Apps
2 Comments
  • Kat

    Can you use β€œand” for a switch statement? I want a and 1 to equal x, a and 2 to equal xx, then b and 1 to equal xy, and b and 2 to equal xz

    6:46 am 14th July 2023 Reply
    • Hi Kat,
      I’m not sure if I am completely understanding and matching your requirement here. But yes in the switch_value part of a switch statement you could include multiple things which need to match the value. Say for example 2 variables must both equal true for the corresponding value to be returned from the true ‘match_value’. That would be possible with something like this…

      Switch(gbl1 And gbl2, true, “Both variables are equal to true”, false, “Both variables are not equal to true. Either one or both are equal to false”.)

      If one of the two variables equal false, the entire switch value returns a false and then looks for this value to match, then returning the corresponding result in the end.

      I hope this helps explain πŸ™‚

      4:07 pm 15th July 2023 Reply

Leave a Reply

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

This will close in 0 seconds