A calculation is a question the respondent never sees. In the Survey Builder, add a question of the type Calculation after the questions it reads.

Write the calculation

Name it for the data, for example Household size, and write the calculation, for example ${adults} + ${children}. Read an answer with its question's id inside ${ }. The language is the one KoboToolbox and ODK forms are written in, so a calculation copied from an XLSForm works as it was written.

It can use:

  • arithmetic with +, -, *, div and mod, and comparisons joined with and and or;
  • logic: if(), not(), true(), false(), coalesce() and once();
  • text: concat(), string-length(), substr(), contains(), starts-with(), ends-with(), normalize-space(), translate(), trim() and regex();
  • numbers: round(), int(), floor(), ceiling(), abs(), pow(), sqrt(), exp(), log(), log10(), min(), max() and the trigonometric functions;
  • choices: selected(), selected-at(), count-selected() and jr:choice-name(), which gives the label of the answer chosen;
  • dates: today(), now(), date(), date-time(), decimal-date-time() and format-date();
  • repeat groups: count(), sum(), count-non-empty(), join(), indexed-repeat() and position(..), with min() and max() reading every entry too.

Dates count in days. So int((today() - ${dob}) div 365.25) is an age in whole years, and date(${visit} + 30) is the date a month after a visit.

The builder says at once if a calculation cannot be read, and why. Choose whether the result is analysed as a number or as text.

Whose date today() is

today() and now() read the day and time the interview began, on Kenyan time. They never read the clock of the phone or server that works the value out again. An interview collected with no signal on Monday and sent on Wednesday gives Monday's date. For a paper questionnaire keyed in, the interview begins when the keying begins.

How it behaves

The value is worked out the moment the interview reaches it, on every channel, including the interviewer app and the web link without a connection. A skip rule can read it like any answer, so a question can be asked only when Household size is above five. When the interview arrives, the server works every calculation out again from the answers, on the interview's own date, and stores its own result.

A calculation reads the answers given before it. One that names a question asked after it reads that question as not yet answered, everywhere, and the builder says so.

If a calculation cannot be worked out, because an answer it reads was skipped or is not a number, it is stored as no answer. Nothing is put in its place.

From an XLSForm

calculate rows import with their calculation as written, pulldata() and the functions across a repeat group included, inside a repeat too. Two functions are listed with the row number and the reason instead: random() and uuid(), whose value would change each time it was worked out again.

Inside a repeat group

A calculation inside a repeat group is worked out for each entry, in order, reading that entry's answers and the calculations before it. A question after the repeat group can add them up with sum(). See Ask the same questions for each member.