# Nested IF statements in Survey123

6481
7
10-24-2019 09:52 AM
MVP Regular Contributor

Hello,

I am trying to write a nested IF statement in Survey123 Connect, and I am having a bit of trouble with the syntax. Here is the gist of what I want to do, in Python syntax:

``````if val1 == 'a' OR val1 == 'b':
score = 'Very Low'
elif total_score < 10:
score = 'Very Low'
elif total_score < 20:
score = 'Low'
elif total_score < 30:
score = 'Moderate'
elif total_score < 40:
score = 'High'
elif total_score < 45:
score = 'Very High'
elif total_score > 45:
score = 'Extreme'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``````

So far, I've gotten this much to work in Survey 123:

if(selected(\${material}, 'a'), 'Very Low',if(selected(\${material}, 'b'), 'Very Low', '0'))

I seem to be able to get as far as the first IF statement and then an ELSE statement, but nesting any further is returning error messages like "cannot handle 'if' requires 3 arguments. Only 4 provided."

Any help would be appreciated!! Thanks!

Best,
Katherine
1 Solution

Accepted Solutions
Esri Esteemed Contributor

Hi Katherine,

First, it looks like you could combine your first 3 conditions (a, b, <10) into one evaluation as they provide the same result.

if(selected(\${material}, 'a') or selected(\${material}, 'a') or \${score}, 'Very Low', ...)

The next part is nesting, which you have done 1 level correctly for above:

if(selected(\${material}, 'a') or selected(\${material}, 'a') or \${score}, 'Very Low', if(\${score} < 20, 'Low', ...))

Further nesting involves placing full if() statements inside the '...' area.  Given the error message you are reporting, it's likely you may be closing off an if() statement early, causing it to see additional values in the next higher up if()

if(selected(\${material}, 'a') or selected(\${material}, 'b') or \${score}, 'Very Low', if(\${score} < 20, 'Low', if(\${score} < 30, 'Moderate', if(\${score} < 40, 'High', if(\${score} < 45, 'Very High', 'Extreme')))))

I find it easiest to do this properly by typing if() when beginning a function and then going inside the parenthesis to fill the values out - that way, you always have the parenthesis in the right place.  Note that you don't need an if() for the final value - it's an else (just like you don't need an elif in the python sequence; an else would have produced identical results)

7 Replies
Esri Esteemed Contributor

Hi Katherine,

First, it looks like you could combine your first 3 conditions (a, b, <10) into one evaluation as they provide the same result.

if(selected(\${material}, 'a') or selected(\${material}, 'a') or \${score}, 'Very Low', ...)

The next part is nesting, which you have done 1 level correctly for above:

if(selected(\${material}, 'a') or selected(\${material}, 'a') or \${score}, 'Very Low', if(\${score} < 20, 'Low', ...))

Further nesting involves placing full if() statements inside the '...' area.  Given the error message you are reporting, it's likely you may be closing off an if() statement early, causing it to see additional values in the next higher up if()

if(selected(\${material}, 'a') or selected(\${material}, 'b') or \${score}, 'Very Low', if(\${score} < 20, 'Low', if(\${score} < 30, 'Moderate', if(\${score} < 40, 'High', if(\${score} < 45, 'Very High', 'Extreme')))))

I find it easiest to do this properly by typing if() when beginning a function and then going inside the parenthesis to fill the values out - that way, you always have the parenthesis in the right place.  Note that you don't need an if() for the final value - it's an else (just like you don't need an elif in the python sequence; an else would have produced identical results)

MVP Regular Contributor

James,

Best,

Katherine

Best,
Katherine
by
New Contributor III

I'm looking to do this exact "if selected OR if selected" type statement, but in a survey report. I know I can use:

\${if field_name | selected:"choice1"}

to see if a single choice is selected, but how how do I make that if statement work if I want it to evaluate to "True" if choice1 or choice3 or choice5 are selected (as in a true "OR" statement - any or all of them can be selected)?

GIS Specialist - MO Office of Geospatial Information
MVP Regular Contributor

Hi @ThomasM !  You might find it helpful to look at the "Conditional Report Elements" section of this documentation

The syntax is slightly different for the reports compared to within the XLS form.

If you have a more specific question after reading that, let me know and I'll do my best to assist. 🙂

Best,
Katherine
by
New Contributor III

So that documentation only covers the example I gave in my question: how to do an "if" for a single selected choice. I have been unable to find any documentation on having an "if" statement that has multiple possible selections be valid. I would expect something along these lines, but I haven't found a way that works yet:

\${if field_name | selected:"choice1 OR choice3 OR choice5"}

GIS Specialist - MO Office of Geospatial Information
MVP Regular Contributor

Ah, ok I see now. I would have to do some testing and experimenting to see if a syntax directly in the report would work for that, but one idea that came to mind would be a schema change in the survey. Have a hidden question that returns "True" if choice1 OR choice3 OR choice5 is selected. Then in the report, populate the checkbox based on that hidden question.

Best,
Katherine
New Contributor

Hi Thomas,

Did you ever figure this out? I'm working on troubleshooting the same issue right now and have not been able to find any documentation with examples. I'm hesitant to use Katherine's option involving a schema change because my survey's XLSForm is being built off of an existing hosted feature layer, so adding new fields is more labor-intensive.

Jordan - GIS Analyst & Watershed/Stormwater Research Specialist