Dynamic set a default value for a select_one question type based on another select_one question

Is it possible to choose a default value for a select_one question type depending on another select_one question?

Let’s say I have a select_one question called “category” with the following possible values pulled from choices list:

  • Cat A
  • Cat B
  • New Cat

Then I have another select_one question called “status” with the following values:

  • Not Changed
  • Changed
  • Removed
  • New

If a user choose “New Cat” for question “category”, I’d like to set “status” to “New”. If the “category” is not “New” (e.g. Cat A, Cat B…), then I’d like to set the default value for “status” to be “Not Changed”.

However, this should happen only once, so that user can change the default value.

If I use a calculation for “status” field, it will always change the value to either “Not Changed” or “New”. I tried applying once() function, but it didn’t work. I tried also adding the calculation expression to the “default” column, but it is not working either (looks like calculations are not supported for “default” column).

Can anyone think of a workaround? Thanks

Would you mind explaining this a bit …

Thanks @Kal_Lamm for following up!

I want to preselect the default value for the user depending on another field. However, user should be able to change this value if needed. If I use the calculation, it will be triggered each time the user change another field. I would like to select default value only once, when the field is displayed for the first time. Hope this is more clear :wink:

Maybe you could do the same as outlined in the image shared below:

In the survey tab of your xlsform:

Image 1

In the choices tab of your xlsform:

Image 2

Data entry screen as seen in Enketo: when nothing is selected

Data entry screen as seen in Enketo: when Cat A is selected

Data entry screen as seen in Enketo: when Cat B is selected

Data entry screen as seen in Enketo: when New Cat is selected

Reference xlsform:

Auto Select.xls (29.5 KB)

Thanks, this is exactly what I need and what I tried before. My problem is that while it works well in Enketo, it doesn’t work in Collect (Android), because each time a user goes back to Q1, the Q2 will be defaulted to either “Not Changed” or “New”. That’s why I was thinking of using once() in the calculation, to ensure the defaults are applied only in the first attempt…

Have you checked if this happens when nothing is filled in Q1?

When nothing is filled in Q1, Q2 does not get changed when going back and forth between Q1 and Q2.

I don’t see an issue with Collect android app as well. I feel it’s the same way how Enketo behaves. Have you checked the if-statement? It is there exactly how it should be. Correct me if i have mis-understood you.

1 Like

See https://docs.getodk.org/form-logic/#setting-default-responses
You could also find previous discussion in the forum, by search e.g. with token “dynamic default”.
Instead of default, you might also work with calculation once(…).

1 Like

I’ve deployed the form you shared and synced with KoBo Collect. Steps to reproduce:

  • start the form “Auto Select”
  • For Q1, select “Cat A” and click Next
  • In Q2 “Not Changed” will be selected (this is what I need)
  • For Q2 - change selected option to “Changed”
  • Go back to Q1
  • Do not change Q1 response (still “Cat A” is selected) and click Next to go to Q2
  • Q2 selected value is set back to “Not Changed” (this is my problem - I want this value to stay the same, i.e. “Changed”)

Apparently the calculation in Q2 is being executed each time user goes from Q1 to Q2, even if the Q1 value is not changed.

1 Like

Ah, yes! That’s a good catch! Maybe @martijnr and @Xiphware would be in a better position to break it up further.

Hi @janekb
I reworked the logic used by @Kal_Lam incorporating the use of once as suggested by @wroos
I however noticed that you need to correct for when the calculation happens and put them within calculate fields to ensure that the calculations only happen once a choice has been made (see relevant columns). Note, I introduced 4 calculations which are basically hidden (to data collector). I believe two of them could be irrelevant and can be combined :slight_smile: However the form should work fine on all platforms i.e. Collect as well as Enketo

This is the form Auto Select.xls (30.5 KB)


1 Like

Wow, that is a nice hack! :wink: However, in Collect, Q2 does not show up, regardless of what is selected for Q1.

PS. How exactly once() function works?

Would you mind to have a look at the specification

Maybe the KoBo experts could add a hint on once() function in their support article?
I didn’t find anything about the once() function there (with search).


Hello @stephanealoo ,
Is this correct, that KoBo meanwhile also allows dynamic defaults? Even expressions?
See https://xlsform.org/en/#default
Is there any difference to once() in behaviour, e.g. activation point?

So, default might also be a solution for the example? (Enketo & KoBoCollect)
Kind regards