Hello,
I'm unclear on something regarding IF statements that I'm hoping you can help with.
Scenario:
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:
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"}) )
Thanks for the clarity
Solved! Go to Solution.
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).
Hi @ericonline
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 )
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 )
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).
Hi @ericonline
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 )
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 )
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:
If( And( varConnected, frmMain.Mode = FormMode.New, varEnv = "DEV" ),
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!
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!
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.)
While this thread is old, if others come across it, please note that you apparently now CAN do multiple statements after an IF by separating them by a semicolon. I'd tested this late in 2019 and have been holding my breath to see if one of the updates breaks it, but it does work. So formatting a statement like:
If(myBoolean,
a=1;
b=2;
c=3,
d=4
)
Means that if myBoolean is TRUE, then a, b, and c get assigned values. If FALSE, then d gets assigned a value.
User | Count |
---|---|
174 | |
116 | |
85 | |
44 | |
41 |
User | Count |
---|---|
238 | |
150 | |
131 | |
77 | |
72 |