“If” is one of the most used functions in any programming language and PowerApps is not the exception. The If  function tests if the first condition is met and returns the result. You can define an optional value when all conditions are false. So if you have something like If(IsToday(birthday),"Happy Birthday") you’ll get a “Happy Birthday” message when the birthday variable contains the date of your birthday. Notice that nothing happens if it’s not your birthday so to add a default value just If(isCorrect,"Value is Correct", "Error"), and the field will display Value is Correct if the value passes the validations and Errorif there’s an issue.

The If function in PowerApps can be different than in other programming languages that only support an If(<condition> then <value if true> else <value if false>). It’s quite powerful, allowing you to write something like If(<first validation>,"Failed the first validation",<second validation>,"Failed the second validation", "All Good") if you have multiple validations. All Good is the default value in case all the other conditions are false. You can use this strategy, for example, to validate user entry fields, where you have a lot of conditions that check the user inserted something invalid and, if all checks fail, it means the value is correct.

Please don’t confuse an If formula with multiple conditions with a Switch function. Both serve different purposes and are inherently different. If starts from the beginning of the conditions and stops as soon as it finds the first true one. A Switch would evaluate a single formula and checks if any of the values provided fit in the criteria. These values can be true or false or any other type. Check the Switch reference for more details.

Limitations

  1. You can only nest 50 If statements. Once you reach this limit, think if you’re overcomplicating the overall formula. Re-evaluate the conditions and combine validations into smaller chunks.

Recommendations:

  1. Keep the If formulas as simple as possible. It’s quite easy to get lost in complex formulas, so simplicity is your friend.
  2. Use the If(<condition 1>, <result if true>, .... , <default value>) when possible instead of creating multiple If statements. It makes for better and simple formulas.
  3. To check if all conditions are true, prefer using the && instead of nested If statements. For example prefer using If(IsToday(birthday) && IsToday(birthdayparty), "Happy Birthday & See you tonight" , DateDiff(Today(),birthday,Days) & " day(s) to birthday") instead of If(IsToday(birthday), If(IsToday(birthdayparty), "Happy Birthday & See you tonight") , DateDiff(Today(),birthday,Days) & " day(s) to birthday") . They both return the same value but the first one is largely more readable.

Localization

  1. Please note that formulas may have localization-based differences. For example, you should write If(Today(),Now()) with “,” separating each of the arguments, but if your localization is Portugal, you should use “;” instead.

Sources:

If and Switch functions in PowerApps

Back to the PowerApps Function Reference

Featured Image by Caleb Jones on Unsplash

Manuel Gomes

I'm a Project Manager with experience in large projects and companies. I've worked in the past for companies like Bayer, Sybase (now SAP) and I'm currently working for Pestana Hotel Group.

View all posts by Manuel Gomes →

Leave a Reply

%d bloggers like this: