cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Super User
Super User

IF LOGIC: Multiple conditions

Hello,

I'm unclear on something regarding IF statements that I'm hoping you can help with. 

Scenario:

  • IF with multiple conditions

Example:

  • If(
        And( var1, var2), // Both conditions true
            Patch( sharepointList, ID = varID, {test: "yes"}),
        And( !var1, var2), // First condition false
            Patch( sharepointList, ID = varID, {test: "no"}),
        And( var1, !var2), // Second condition false
            Patch( sharepointList, ID = varID, {test: "maybe"}),
        And( !var1, !var2), // Both conditions false
            Patch( sharepointList, ID = varID, {test: "wow"})
    )
        

Questions:

  1. If I have two conditions, do I ALWAYS need to include the full set of conditions to make the logic work?
    1. Example: both true, first true-secondfalse, first false-second true, both false 
  2. Can I include a single condition in the same statement with the two conditions?
    1. Example:
      1. If(
            And( var1, var2), // Both conditions true
                Patch( sharepointList, ID = varID, {test: "yes"}),
            !var1, // First condition false, second condition not used
                Patch( sharepointList, ID = varID, {test: "no"})
        )
  3. If I add a third condition (or fourth, fifth), do I ALWAYS need to include the full set of conditions to make the logic work?
    1. Example: all-three-true, first false-second true-third-true, first true-second false-third true, first true-second true-third false, etc...

Thanks for the clarity

2 ACCEPTED SOLUTIONS

Accepted Solutions
jhall
Level 8

Re: IF LOGIC: Multiple conditions

If statements are definitely "simpler" (the negative kind of connotation) in PowerApps.  You can do compound statements for If using And/Or, but you cannot do multiple steps after you recognize the statement is True. Switch statements can generally help you solve some of this.

You can nest If statements:

If (
    Boolean1,
    If(
       Boolean2,
       X,
       Y),
    Z
)

And in that scenario, no, you don't have to include the original Boolean Test within it.  On the first Leg above, Boolean1 will run the second nested If() or resolve to Z.  On the nested If, Boolean2 is all that is needed because Boolean 1 must be true.

To do a really nested If w/ lots of steps after a True result, you would need to keep including the test over and over (which sometimes is easier to just set a boolean in the first step and only test against that), or do something "crazy" (e.g. like starting a Timer and having that run all of your steps and then end).

View solution in original post

Dual Super User
Dual Super User

Re: IF LOGIC: Multiple conditions

Hi @ericonline 

 

1) If I have two conditions, do I ALWAYS need to include the full set of conditions to make the logic work?
That totally depends on the data you want to assign based on various criterias. You can use AND/ OR to group various conditions. Also, you can go on with the second point but only if the conditions are specified in correct order.
 
2) Can I include a single condition in the same statement with the two conditions?
Your Current formula will fail in the condition where both are false, because as per if loop execution, if a condition is satisfied then it will skip the further actions. Instead of returning "wow", it will return "no". The order given to the different criteria is majorly important. You can refer to below expression based on the order:
If(
    And( var1, var2), // Both conditions true
        Patch( sharepointList, ID = varID, {test: "yes"}),
    And( !var1, !var2), // Both conditions false
        Patch( sharepointList, ID = varID, {test: "wow"}),
    !var1, // First condition false, second condition not used
        Patch( sharepointList, ID = varID, {test: "no"}),
        Patch( sharepointList, ID = varID, {test: "maybe"}) //this will be treated as Else condition
)

3) If I add a third condition (or fourth, fifth), do I ALWAYS need to include the full set of conditions to make the logic work?
You can optimize the query using AND/OR but ultimately it will also require all the conditions specified. Something like:
If(
    And( var1, var2, var3), // All three conditions true
        Patch( sharepointList, ID = varID, {test: "yes"}),
    And( !var1, !var2, !var3), // All three conditions false
        Patch( sharepointList, ID = varID, {test: "wow"}),
    OR(!var1 && !var2, !var1 && !var3,!var3 && !var2  ), // Only two conditions out of all three are true
   Patch( sharepointList, ID = varID, {test: "no"}),
   Patch( sharepointList, ID = varID, {test: "maybe"}) //else to check if only one condition is satisfied
)
 
Hope this Helps!
 
If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up. THANKS!

View solution in original post

5 REPLIES 5
jhall
Level 8

Re: IF LOGIC: Multiple conditions

If statements are definitely "simpler" (the negative kind of connotation) in PowerApps.  You can do compound statements for If using And/Or, but you cannot do multiple steps after you recognize the statement is True. Switch statements can generally help you solve some of this.

You can nest If statements:

If (
    Boolean1,
    If(
       Boolean2,
       X,
       Y),
    Z
)

And in that scenario, no, you don't have to include the original Boolean Test within it.  On the first Leg above, Boolean1 will run the second nested If() or resolve to Z.  On the nested If, Boolean2 is all that is needed because Boolean 1 must be true.

To do a really nested If w/ lots of steps after a True result, you would need to keep including the test over and over (which sometimes is easier to just set a boolean in the first step and only test against that), or do something "crazy" (e.g. like starting a Timer and having that run all of your steps and then end).

View solution in original post

Dual Super User
Dual Super User

Re: IF LOGIC: Multiple conditions

Hi @ericonline 

 

1) If I have two conditions, do I ALWAYS need to include the full set of conditions to make the logic work?
That totally depends on the data you want to assign based on various criterias. You can use AND/ OR to group various conditions. Also, you can go on with the second point but only if the conditions are specified in correct order.
 
2) Can I include a single condition in the same statement with the two conditions?
Your Current formula will fail in the condition where both are false, because as per if loop execution, if a condition is satisfied then it will skip the further actions. Instead of returning "wow", it will return "no". The order given to the different criteria is majorly important. You can refer to below expression based on the order:
If(
    And( var1, var2), // Both conditions true
        Patch( sharepointList, ID = varID, {test: "yes"}),
    And( !var1, !var2), // Both conditions false
        Patch( sharepointList, ID = varID, {test: "wow"}),
    !var1, // First condition false, second condition not used
        Patch( sharepointList, ID = varID, {test: "no"}),
        Patch( sharepointList, ID = varID, {test: "maybe"}) //this will be treated as Else condition
)

3) If I add a third condition (or fourth, fifth), do I ALWAYS need to include the full set of conditions to make the logic work?
You can optimize the query using AND/OR but ultimately it will also require all the conditions specified. Something like:
If(
    And( var1, var2, var3), // All three conditions true
        Patch( sharepointList, ID = varID, {test: "yes"}),
    And( !var1, !var2, !var3), // All three conditions false
        Patch( sharepointList, ID = varID, {test: "wow"}),
    OR(!var1 && !var2, !var1 && !var3,!var3 && !var2  ), // Only two conditions out of all three are true
   Patch( sharepointList, ID = varID, {test: "no"}),
   Patch( sharepointList, ID = varID, {test: "maybe"}) //else to check if only one condition is satisfied
)
 
Hope this Helps!
 
If this reply has answered your question or solved your issue, please mark this question as answered. Answered questions helps users in the future who may have the same issue or question quickly find a resolution via search. If you liked my response, please consider giving it a thumbs up. THANKS!

View solution in original post

Super User
Super User

Re: IF LOGIC: Multiple conditions

Excellent responses @jhall and @yashag2255 , thank you. 

I'm still a little cloudy on the concept (kind of hard to explain and answer a forum). 

My Scenario:

  • Testing 3 conditions to determine where data goes when user clicks Submit button:
    • varConnected (Connection.Connected)
      • If varConnected, perform a Patch
      • Else SaveData
    • FormMode (either .New or .Edit)
      • If .New, Patch Defaults to DataSource
      • If .Edit, LookUp correct record then Patch
    • varEnvironment ("DEV" or "PROD"
      • If "DEV" Patch to testing table
      • Else Patch to production table
  • If(
        And(
            varConnected,
            frmMain.Mode = FormMode.New,
            varEnv = "DEV"
        ),
  • I just added varEnv otherwise the code is working fine with only two conditions
  • I'm apprehensive about adding so much code to accommodate the third condition. 
    • With two conditions, there are 8 paths / table (3 tables total)
    • With three conditions, there are 12(?) paths / table

I'll study the optimizations you both mention to see if I can wrap my head around an alternate method. 

Thanks again for the input!

Super User
Super User

Re: IF LOGIC: Multiple conditions

Hi @jhall and @yashag2255 . I'm working through some of your recommendations. Led me to another issue posted over here.

Take a peek when you can. 
Thanks!

jhall
Level 8

Re: IF LOGIC: Multiple conditions

I can say that when I ran into a very complex series of Patch() scenarios on a project a year ago, I ultimately just did the complex calculations in one section of codet where I assigned a PatchCode variable a value.  Then I had a Switch() that did all the Patching.  This was simply to make it so that all data writes were in the same place and easy to jump to for future management.

It didn't really simplify the code from a number of lines perspective, it was purely for simplifying future edits.

When I did mention the "crazy" methods, it is a similar solution.  You're just chopping up code into "modules" in a way that a programmer might consider them.  Assign a series of steps to a hidden Button or Timer and then fire the event to start them.

It's not really how you're supposed to "do PowerApps", but sometimes it just makes sense to my programmer brain to want to carve out code into a specific location so I know where it is in all of my applications (e.g. un-displayed page, hidden controls, etc.)

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors
Users Online
Currently online: 136 members 5,323 guests
Please welcome our newest community members: