Conditional default in repeat questions

1362
12
05-20-2019 04:43 PM
deleted-user-a_zHTK67PWYA
New Contributor II

I have a repeat set of questions that starts with a select_one question with 4 options. The point of this question is to keep the observer on an unbiased path when taking measurements. The question is "What is your location from the center of the stream channel?". The four options are "Left 1 meter" (L1), R1, L2, R2. I would like the answer to cycle default in that order. There is no constraint on the number of repeats. The idea is that the location is recorded in the repeat table but the observer isn't changing the location from the default. Instead they are using it to guide their sampling. If there is something obstructing their ability to take a measurement, they can change the location and the default cycle resets from that question onward. Is this possible? 

0 Kudos
12 Replies
DougBrowning
MVP Esteemed Contributor

So basically you need to adjust based on the value of the last repeat right?

My only idea if a text field with join() outside the repeat.  You could then pluck the last 2 chars off the end of the join. So something like substr(joinfield, string-length(joinfield}) - 2, string-length(joinfield}))  Since you cannot index the repeat this is all I can think of and have used in the past.

Then your default would need a big If statement using the value created above.  If lastval = L1, R1, if lastval = R1, etc.

0 Kudos
deleted-user-a_zHTK67PWYA
New Contributor II

Correct.

I think I understand what your are suggesting but I'm not exactly sure what/where I need to be entering these calculations. Would you mind being more explicit? My repeat question is named ${loc} and the four available choices are R1, L1, R2, L2. If I understand correctly, the idea is to create a text field (${caploc}) outside the repeat that is capturing the answer of the repeat and the repeat question relevant column is referencing ${caploc}'s value? So the answer for ${loc} in the first repeat will need to be selected by the observer and then the default if statement would start the cycle of conditional defaults?

0 Kudos
DougBrowning
MVP Esteemed Contributor

I am not sure where relevant is coming in?  I would do it as a default or even a calculation.

Create a field - ${caploc} - outside the repeat that does a join(${loc},"") (no spaces needed since your codes are always 2 chars).  This will then be a list of ALL your repeat values.  But you know that the last repeat value will be at the end.

For ease of understanding I would create a second field - called maybe ${lastChoice} that strips just the last 2 chars off of the join field. 

So  substr(${caploc}, string-length(${caploc}) - 2, string-length(${caploc}))  Not sure syntax is perfect - you are just trying to get the last 2 chars.

Then in the default (which sometimes does not work) or a calculation column (which should always work) for ${loc} put your big If statement based on ${caploc}.  

So If(${lastChoice} = "L1", "R1", etc.

Does that make sense?

0 Kudos
deleted-user-a_zHTK67PWYA
New Contributor II

Yes this makes sense. And yes,a mistype. I meant to say "calculation column" not "relevant". I'll give it a try and report back. Thanks for the assistance!

0 Kudos
deleted-user-a_zHTK67PWYA
New Contributor II

Survey123 appears to be struggling with the if statement in the calculation field of the repeat. I get this error: "ODK Validate Errors:>>Something broke the parser. See above for a hint (above reads: Error converting XLSForm) Dependency cycles amongst the xpath expressions in relevant/calculate. Result: Invaild"

It doesn't work when the if statement is entered in the default field.

0 Kudos
DougBrowning
MVP Esteemed Contributor

Seems like your if is wrong.  Try posting it here.  

0 Kudos
deleted-user-a_zHTK67PWYA
New Contributor II

I haven't written out the full if statement. I wanted to see if something circular like this worked. My current if statement is if(${lastChoice}= 'R1','L1',R2'). In the past I have only used if statements for select_one or select_multiple questions so I'm certain something is missing. Here are screen shots of the most recent error and excel doc:

0 Kudos
DougBrowning
MVP Esteemed Contributor

You are missing a ' in R2 value on the loc field.  Should be 'R2' but you have R2'

Also note that sometimes it will add the value you just added to the list then say it is there.  If that happens wrap it in a once().

0 Kudos
deleted-user-a_zHTK67PWYA
New Contributor II

Oops. I fixed the error in my if statement, but now Survey123 has another issue with calculations.

0 Kudos