When exactly the calculate fields are being calculated? The calculation may reference existing field which may be displayed before or after the calculate field. Does it matter?
Let’s say I have a form with the following fields:
field_a (text) - not required
field_b (calculate) - not displayed on the form
field_c (text) - not required
field_d (integer) - not required
Questions:
Can I use the following calculation expression for field_b: ${field_c}?
What will happen if a user edits a saved form and only change field_c? Will the field_b get recalculated?
Before sending the form, I’d like to recalculate some fields. E.g. if a user did not enter any value for field_d (integer), I’d like to give it a value of 0. How can this be achieved? I don’t want to use a default value, so the user does see “0” when responding to this question. Can I use calculation here? I’d like this calculation to happen only if a user wants to submit a form with no value for the field_d (i.e. instead of null value I need “0” - zero).
Yes. calculate question type does not follow the ordering on where it is situated/located. It gets only activated when a condition is satisfied i.e. the calculate question in your case is only activated when you fill in for field_c.
Regarding your second query:
As soon as you make edits on field_c, the value from a calculate question too gets updated.
Regarding your third query:
In my opinion, using the default would be the easiest way to solve your problem here.
Thanks @stephanealoo! Yeah, I’ve seen that. Do you know if it is possible to add more than one trigger (field that would activate the calculation when its value changes?). I’ve asked this question here: Is it possible to have multiple triggers?, but it is kind of related to this thread…
Also, one thing that is still not clear to me is how groups impact the calculations. What about questions that are outside groups? If two questions are outside a group (i.e. no groups at all), are they considered to be in the same group, or each question would be considered as a group itself…? not sure if that is a clear question…
Hi @janekb
On multiple triggers for the same questions; I have not tried this out but I would advise you try out using the or command and see if this works
The overall question is the execution of the calculate questions:
When questions are within the group the calculated questions are computed differently as follows
On Enketo- The calculation will happen for both regardless of the appearance setting
On Collect- The calculation will happen after you have swiped from the preceding question (when the appearance setting is not field-list) however the calculation will not occur until you swipe off the group and come back to the group (when the appearance setting has been set to field-list)
Questions outside the group are considered just as individual questions.
Enketo tends to compute them immediately the page on which the questions appear comes up on the screen.
Collect will only execute them once you have swiped off the preceding question
On a more related note, you can also enforce when the calculate question is executed by setting a relevant field which only makes the question be computed when the relevance setting has been met. Generally too, avoid using field-list appearance setting for a group when calculate questions reference a question within the same group