If() or Switch() in Power Apps

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… READ MORE [https://lewisdoes.dev/blog/if-or-switch]
white wall
Photo by Steve Johnson on Pexels.com
In: Low Code Lewis Content 🚀

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!

Written by
Lewis Baybutt
Microsoft Business Applications MVP • Power Platform Consultant • Blogger • Community Contributor • #CommunityRocks • #SharingIsCaring
Comments
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to LewisDoesDev.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.