If then syntax issues

897
5
06-25-2018 07:49 AM
by Anonymous User
Not applicable

Hi,

I am trying to get this calculation to work in survey123 xls, and I think my syntax is incorrect. Can someone help?

if(${CrossingSpanId},'Spans Full Channel & Banks',1,if(${CrossingSpanId},'Spans Only Bankfull/Active Channel',0.9,if(${CrossingSpanId},'moderate',0.5,if(${CrossingSpanId},'severe',0,if(${CrossingSpanId},'Unknown',1,'NULL')))))

Error:  Xpath Evaluation cannot handle funciton 'if' requires 3 arguments, only 4 povided.

TIA!

0 Kudos
5 Replies
NickDierks1
Occasional Contributor II

Is ${CrossingSpanID} a select_one question? If so, then try using selected() in your if() statements:

if(selected(${CrossingSpanId}, 'Spans Full Channel & Banks'), 1, if(selected(${CrossingSpanId}, 'Spans Only Bankfull/Active Channel'), 0.9, if(selected(${CrossingSpanId}, 'moderate'), 0.5, if(selected(${CrossingSpanId}, 'severe'), 0, if(selected(${CrossingSpanId},'Unknown'), 1, 'NULL')))))

by Anonymous User
Not applicable

Thank you! This worked! Is it different for a select_multiple?

0 Kudos
NickDierks1
Occasional Contributor II

It depends on how you want it to work. This nested if/then statement will simply go in order, and stop when it reaches a true argument or the final else clause ('NULL'), so it will only ever return a single result. For example, if you select both 'Spans Full Channel & Banks' and 'moderate', then this statement will only return the value 1, because that's what comes first, and no else clause is checked for after that.

If that's the behavior you want, then this will work just fine. Otherwise, you'll need one or more complex calculations depending on what you want it do.

by Anonymous User
Not applicable

Thank you. Do you know if it is possible to make calculations from fields in the related tables/repeats and write back to the original survey/fc? ie i have 5 identical repeats that each have a  final score calculated within them, and I want to write back to the fc/survey the max of those scores from the repeats.

0 Kudos
NickDierks1
Occasional Contributor II

There are a few calculations that will work with repeats. It sounds like you'll want to use a max() calculation for each repeat's final score, which should be simple enough.

Check out these resources for details and information on max() and similar functions:

Repeats: Aggregate Functions

Release notes for version 2.1 (scroll to Repeated Aggregate Functions for a better description and a simple example table)

Formulas (according to the examples here, min() and max() can be used on either a single field from a repeat, or for multiple different fields, in case that sounds like something you may need)