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.
Solved! Go to Solution.
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.
Function | Description | Example |
---|---|---|
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')
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')
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.
Function | Description | Example |
---|---|---|
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')
ah whoops! good catch, thanks!