Is it possible to set a default value for a decimal question using the calculate column?

I have this question where I want to generate a default value for it, depending on the previous question's answer.

| type    | name | calculate                                                                  |
| decimal | test | if((${other_question} < 5), 10.5, if((${other_question} < 7), 21.3, 30.6)) |

If the answer to the other_question was 4 then test will have a default value of 10.5.

Again, Is it possible to set a default value for a decimal question using the calculate column?

1 Like

EDIT: This is wrong. See detailed post by @LN below.

I thought you might have to have a type calculate with your formula in the calculation column. And then use that value in the default column for your decimal type question. That didn't work. But then I tested your initial post and it works, you just need a different column header. It should be calculation and not calculate for the column header.

|  type   |      name      |  label   |                                calculation                                 |
| integer | other_question | my other |                                                                            |
| decimal | test           | no label | if((${other_question} < 5), 10.5, if((${other_question} < 7), 21.3, 30.6)) |
1 Like

Very good question, @Abel_Melquiades_Call. I believe that when you say default you mean you would like to set the value but allow the user to modify it, is that right?

In general, the mental model to have is that all calculations, constraints and relevance statements are evaluated on question load and on form save/load. That means that with the strategy you've tried and @danbjoseph's fix, you can set a value but that value will be the one sent no matter whether the user changes it or not. Again, that's because the calculation will be evaluated again after the user changes the value and the user's value will be lost.

You might come up with the idea of using once() to only evaluate the calculation if test is blank. With just adding once() to what you have now, the calculation would be evaluated when the form is first loaded, get set to 30.6 because other_question is blank and then never get set again.

What should do what you want is once(if(${other_question}='', '', if(${other_question} < 5, 10.5, if(${other_question} < 7, 21.3, 30.6)))) Your original condition will be evaluated the first time ${other_question} gets a value. There are a couple limitations I can think of:

  • if the user jumps to other_question and changes its value, test will not be set again (that's probably the best behavior anyway?)
  • it's not possible for ${test} to be blank if ${other_question} is not blank

This technique should be generalizable for any dynamic default. For example, someone might want to pull a default birthday from a dataset with an XPath expression (or pulldata) based on a name field's value and let the user edit it.