Dynamic defaults within repeat group


I’m trying to set a dynamic default where answers given outside a repeat group appear as default within it.

It is a household survey and at the beginning of the form I ask personal details of the respondent (name, age, sex, marital status) and of the head of the household if different from the respondent. I then ask the number of people in the household and use this value as the number of repeats for a household roster in a repeat group, where I ask the same questions for every member in the household (including the respondent and the head of the household).

To make the life of enumerators easier, I want the details filled at the beginning of the form to appear as defaults in the first iteration of the repeat group. If the respondent is not the head of the household, then the head of hh details should appear as default in the second iteration of the repeat group.

I tried adapting this code without success.

I also tried using the following formula in the calculation column within the repeat:

if(position(…)=1, ${name},if((position(…)=2 and ${head}=‘no’), ${head_name},’’))

${name} is the name of the respondent
${head} is the question that asks if the respondent is the head of household
${head_name} is the name of the head of the household if different from ${name}

With the formula above the values appear as default but are deleted in ODK for the iterations where none of the conditions are met. Since ODK evaluates the formulas when saving the form, the single quotes at the end of the formula clear the values entered by the enumerator.

I also used triggers to avoid ODK Collect from evaluating the calculation when saving the form (triggers outside and within the repeat group) but values are emptied by the ‘’ at the end of the formula in every case.

Here’s my xls form. The tests mentioned above are in different tabs.

dynamic_default_test.xlsx (32.1 KB)

Your support is very much appreciated!

Maybe using calculation with once(…) instead of default can offer a workaround. But will only fire when field is empty.
See previous posts


Thank you!

I wrapped my initial formula in the once() operator and that did the trick.

The way I understand it, the once() checks whether the value is empty and if it is, passes on the expression within the brackets. Once the user enters a value, ODK passes on without doing the calculation. As a result I can save without the ‘’ in the formula deleting the data that was entered.

Here’s a copy of the xls if it can help anyone out there!

dynamic_default_test.xlsx (17.6 KB)

1 Like

There is to take care of: If the user goes back and changes the value/variable referenced in the calculation, the once(…) will NOT update (unless the field was edited before to empty again).


Buenas tardes,
Estoy revisando esta solución y aplica perfecto para mi duda, quisiera saber a que hace referencia cuando indica “position(…)=1” en la función.
Agradezco su ayuda Saludos.!!

The position(…) function returns the iteration number within the repeat you are currently in. More information here.

The formula in the calculation column you might be referring to in the form does the following:

once(if(position(…) = 1, ${variable}, “”))

If this this the first iteration of the repeat loop, then use the value of ${variable} as a default, else there should be no default value. The once() function wrapping the formula prevents ODK from evaluating the formula (and deleting the values you might have entered manually) when finalizing the form.

1 Like