When exactly the calculate fields are being calculated?

Hi all,

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:

  1. Can I use the following calculation expression for field_b: ${field_c}?
  2. What will happen if a user edits a saved form and only change field_c? Will the field_b get recalculated?
  3. 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).

Any suggestions?

2 Likes

Hello,
What are you using?

  • Enketo or KoBoCollect?
  • style pages or single (settings)?
  • Are the fields in your example on the same level, e.g. in the same group?

Hi, thanks for following up!

  • eventually I want to use KoBo Collect, but would be happy to learn how Enketo works in this regard too
  • I think I’m using single, but not sure what are these…
  • I’ve put my calculate fields in a separate group than fields used for calculations

Hi @janekb,

Regarding your first query:

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.

So, the calculation will be triggered each time any of the fields used in the calculation expression will be changed?

1 Like

Yes, you got that correct!

Makes sense! Going forward, we can also add other field (not used in the calculation) as a trigger, by adding it to the trigger column, right…?

Hi @janekb

Yes you can do so. To understand more about trigger, you can also see this link (if not already done so)

1 Like

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… :slight_smile:

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 :bell:

The overall question is the execution of the calculate questions:

  1. 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)
  1. 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

Stephane

3 Likes

Thanks @stephanealoo - this is a great wrap up! Very helpful!

2 Likes

Hi @stephanealoo, (@Kal_Lam)
Could you review this, please.

There is different info here:
When are expressions in a form recomputed? - #4 by LN - Support - ODK Forum.