How to implement multiple/nested and/or statements

912
3
Jump to solution
11-28-2018 07:47 AM
ByronTsang1
New Contributor II

I'm having trouble finding the correct syntax for series of and/or conditions within an IF() calculation. how do AND and OR operators group naturally, and do parenthetical groupings parse correctly when Survey123 does calculations?

In other words: would the following result in valid syntax for something like the following:

IF(  (A and B) or (C and D), [return x], [else return Y] )

obviously this is simplified, but I'm trying to build a calculate question based on several combined inputs and other calculations.

0 Kudos
1 Solution

Accepted Solutions
StephenM
Occasional Contributor II

I think the issue may be the use of sum(). Based on the documentation, sum() expects a repeat, rather than a list of question names.

FunctionDescriptionExample
sum(repeat)Returns the sum of all responses to a given question across repeats.sum(${question})

This worked for me:

if((${tree_3_dom}= 'yes' and ${tree_4_dom} = ${tree_3_dom}) or ${tree_4_dom} > ${tree_cover20} or (${tree_1_cover_calc} + ${tree_2_cover_calc} + ${tree_3_cover_calc} + ${tree_4_cover_calc}) < ${tree_cover50}, 'yes', 'no')

View solution in original post

0 Kudos
3 Replies
ByronTsang1
New Contributor II

as an additional note: I've attempted the IF() statement using typical logical order precedence and WITHOUT parentheses, but Survey123 Connect threw the following error:

ODK Validate Errors:

>> Something broke the parser. See above for a hint.

Dependency cycles amongst the xpath expressions in relevant/calculate

Result: Invalid

My formula was built as follows:

if( (${tree_3_dom}= 'yes' and ${tree_4_dom} = ${tree_3_dom}) or ${tree_4_dom} > ${tree_cover20} or sum(${tree_1_cover_calc}, ${tree_2_cover_calc}, ${tree_3_cover_calc}, ${tree_4_cover_calc}) < ${tree_cover50}, 'yes', 'no')

0 Kudos
StephenM
Occasional Contributor II

I think the issue may be the use of sum(). Based on the documentation, sum() expects a repeat, rather than a list of question names.

FunctionDescriptionExample
sum(repeat)Returns the sum of all responses to a given question across repeats.sum(${question})

This worked for me:

if((${tree_3_dom}= 'yes' and ${tree_4_dom} = ${tree_3_dom}) or ${tree_4_dom} > ${tree_cover20} or (${tree_1_cover_calc} + ${tree_2_cover_calc} + ${tree_3_cover_calc} + ${tree_4_cover_calc}) < ${tree_cover50}, 'yes', 'no')

0 Kudos
ByronTsang1
New Contributor II

ah whoops! good catch, thanks!

0 Kudos