How to write a nested if statement in formula tool to check the second condition only if the first condition is true or false.
Example: Condition 1: If country = USA is true then check
Condition 2: State = Oregon or New Jersey
If Condition 1 is not met then "Not Valid"
Solved! Go to Solution.
Please see below how you can do it:
Attached the workflow,
If this solve the issue please mark it as solved. It will help others.
Regards,
What should the value be if country is not USA? Try this
IF [country] = "USA"
THEN
IF [State] IN ("Oregon", "New Jersey")
THEN "valid"
ELSE "Invalid"
ENDIF
ELSE Null()
ENDIF
Here is a list of all functions: https://help.alteryx.com/current/designer/functions
Chris
Some other alternatives to those presented, depending on your use case.
If Country = USA AND State in (Oregon, New Jersey) then ....
ElseIf Country = USA then ....
Else ...
End
The important thing to remember about using nested IF/THEN/ELSE or CASE logic is that it is not recursive at all. Every row of data can only match 1 condition, and it will get a value as soon as it matches one. So, it's often helpful to put the most restrictive conditions first and expand from there.
You could also use a filter tool to separate your data into 2 streams based on Country = USA logic. Use the "True" stream to do your next validation on the states. This would also give you the opportunity to do any validation on the non-USA rows separately. You could use a Union tool to put the 2 streams back together once the two streams have been categorized accordingly.
Hope this helps!
-Jim
Thank you everyone for the solution!