Conditional (if / then / else) logic in Survey123 using numeric answers

21462
14
Jump to solution
03-19-2021 12:40 PM
SilverwoodConsulting
New Contributor II

Hi there,

I'm trying to use conditional if / then / else logic in Survey123 to take a range of numeric values calculated in (question_one) and assign it a value accordingly.

I've tried modifying this script (from: https://doc.arcgis.com/en/survey123/desktop/create-surveys/xlsformformulas.htm#ESRI_SECTION2_BED1A39...😞 

if(condition, a, b)

If the condition evaluates to true, returns a; otherwise, returns b.

if(selected(${question_one}, 'yes') and selected(${question_two}, 'yes'), 'yes', 'no')

 

The conditional logic would be something like:

            if(${question_one} > 1.0 and < 1.9) then assigned_value = '500mm'

            if(${question_one} > 2.0 and < 2.9) then assigned_value = '600mm'

            else(${question_one} > 2.0 and < 2.9) then assigned_value = '700mm'

I've tried this format (and similar variations): 

            if(${Q100} > 0.5), '500'

Note: question_one is a numeric field (decimal) that calculates a total based on other fields in the form (i.e. an average). The assigned_value field would then use that auto-calculated numeric value field and categorize the numeric values based on ranges (i.e. 1.0 to 2.9 or >= 1 and <= 2.9).

Is this possible within Survey123. Does anyone have experience using a similar loop conditional statement?

Thanks in advance for your help!

SJ

14 Replies
RobertAnderson3
MVP Regular Contributor

What is happening when you try to save/run the calculation? The first thing that comes to mind is use the domain values, not the display names as I was encountering this earlier with a colleague.

0 Kudos
BryceHancock
Occasional Contributor

I am trying to evaluate 2 if statements in the calculate field of an xls form and the following works for the 1st statement but not the 2nd (the 2nd statement is in bold):

if(selected(${RMZ_CLASS}, 'CAT_1') or selected(${RMZ_CLASS}, 'CAT_2') or selected(${RMZ_CLASS}, 'CAT_3'), ('100 Feet'), '0 Feet' or if(selected(${RMZ_CLASS},'CAT_4'),('50 Feet'),'0 Feet'))

When I choose CAT_1, 2, or 3 in the list '100 Feet' is returned in the field, but if I choose CAT_4, '0 Feet' is returned, when it should return '50 Feet'.

Anyone have ideas where I am going wrong?

0 Kudos
DougBrowning
MVP Esteemed Contributor

You have 2 else statements and too many parenes try this

if(selected(${RMZ_CLASS}, 'CAT_1') or selected(${RMZ_CLASS}, 'CAT_2') or selected(${RMZ_CLASS}, 'CAT_3'), ‘100 Feet', if(selected(${RMZ_CLASS},'CAT_4'),'50 Feet','0 Feet'))

0 Kudos
TKSHEP
by
Occasional Contributor

@DougBrowning I am trying to also do something similar, however with months in a year. I am trying to use a conditional calculation to autofill the fiscal year.  I have this, 

format-date(${inspection_date}, '%n'), if(${inspection_date } =< 1 and ${ inspection_date } =>3, 'Q2', and if(${inspection_date}=1 or 2 or 3, 'Q2') and if(${inspection_date}=4 or 5 or 6, 'Q3', 'Q4'))

  however I am getting an error and was wondering if you could help me with it.

Thank you so much for your help.

DougBrowning
MVP Esteemed Contributor

Do you have this all in one field or something?  If so that is not the correct way, this statement above just has a function at the front.  Also all of your syntax is incorrect. 

You need to split this to 2 fields.

Field_1 is format-date(${inspection_date}, '%n')  note set to integer type and appearance as hidden.  Set bind::type to null if you do not want this field in the final schema.

Then Field_2 uses the value from Field_1

Easier to read this way and proper syntax

if(${Field_1} <= 3, 'Q1', if(${Field_1} >= 4 and ${Field_1} <= 6, 'Q2', if(${Field_1} >= 7 and ${Field_1} <= 9 'Q3', 'Q4')))

Would be a good idea to read up on how if statements work in general.  if(condition, true, false)

0 Kudos