# Calculate field based on previous dropdown question in survey

172
1
07-14-2022 12:32 PM
New Contributor

Hello! I'm trying to populate data based on dropdown answer higher up in the survey. Essentially a hospital has to select their name, then I'd like a hidden field that assigns the region they are in. I have 84 hospitals in 6 regions. I tried:

if(\${hospital}, 'Facility1' or 'Facility2'), 'Region 1', if(\$[hospital}, 'Facility3' or 'Facility4' or 'Facility5', Region 2', '')

I don't know it the syntax error is because of the "or", having a second if statement embedded in the first one, or what. It seems to start showing up when I add the first "{"

My destination field is a Singleline text, and I'm trying to do this in the calculation. Not opposed to switching to Connect and doing it on the XLS.

Anyone have ideas

Tags (4)
by
MVP Esteemed Contributor

In Survey123, the if function follows the syntax

`if(condition, return if true, return if false)`

So, for the example expression you give, you'd need to write this out as follows (I'll use line breaks to make this clearer):

``````if(
\${hospital} == 'Facility1' or \${hospital} == 'Facility2',
'Region 1',
if(
\$[hospital} == 'Facility3' or \${hospital} == 'Facility4' or \${hospital} == 'Facility5',
Region 2',
''
)
)
``````

You'll need that all in one line for it to work properly, but note how multiple conditions have to be specified clearly. It feels linguistically sensible to say "\${hospital} = 'A' or 'B', but programmatically, your survey is going to treat 'B' as its own condition.

For a large number of potential hospitals and regions, this can make for a large expression, but at least you'll only need to write it once.

- Josh Carlson
Kendall County GIS