“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 have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

3 thoughts on “Power Apps: If Function

  1. Howdy, I’m quite new to Power Apps and have a history coding in the past where assigning a value to another field via a button is quite easy. For instance, you click a button and it will change the value of the field “Status2” will be changed to “Open”, this can easily be done using this code…Status2=”Open”

    With PowerApps, it is not that easy. I’ve read and read and searched and search

    Another example is

    A field named “StatusCode”

    If StatusCode =”O” then
    StatusVal = “Open”

    end if

    Something basic like these 2 things I can’t seem to figure out. Both are text fields in SharePoint, from the same table. I’ve also followed this line of reasoning.

    Field SatausVal, as the Default

    If(StatusCode=”O”,”Open”,”Closed”)
    No more errors, but the result will always “Closed”

    Any thoughts?

    1. Hi Jason,
      I love the question, and I can sympathize with it. I was a developer in the past also, and I see where you’re coming from.
      Actually, I have quite a few thoughts. If you don’t mind, I’ll use your question as to the basis of a new article. It’s a rich topic to explore, and I think I would do a better job than explaining it in the comments.
      I’ll keep you posted.
      Cheers
      Manuel

Leave a Reply

Your email address will not be published. Required fields are marked *