## Advanced Use of Calculate Questions

Some advanced forms may require an internal calculation to take place as part of the form (rather than afterwards during the analysis). This can be done by adding a **Calculation,** and writing the mathematical expression into the question label field.

This article provides step-by-step instructions on how to add calculations while using the form builder or by downloading and adding it directly to the XLSform.

To view a complete and detailed list of all functions please visit ODK's excellent XPath Functions documentation.

## Approaches towards adding calculate questions:

#### Using the form builder

*Step 1: Add a calculate question*

*Step 2: Type your calculation formulae where you would typically write your question.*

Note:

- Your calculate question
**will not**be displayed when conducting data entry/collection either on Kobo Collect or Enketo. It will, however, be displayed when looking at the data in the table view or in the downloaded version. - You must follow similar rules as for XLS forms (see our section rules below)

#### Using the XLS forms

We recommend this approach when working with more advanced calculate functions.

XLS forms allow for use of calculate function on different types of question types.

- You can mimic the approach used in the form builder where the question will not be displayed on data collection by simply defining the question type as 'calculate' and then typing your calculation within the calculate column
- You can use 'calculate' for different question types, and in this case the question will be displayed during data collection. You can choose to make that question a read-only so that no one can change the entry. The question types we have tested with this approach include

*a. integer (will only take numeric calculate functions)b. text (will only take string calculate functions)c. note (will take only question referencing and not calculate functions)d. date (will only take date calculate functions)e. time (will only take time calculate functions)*

## Rules when working with calculate questions:

#### These rules apply to both the form builder and XLSform

- You cannot use both numeric and string calculations within the same question
- Your numeric calculations will follow the BODMAS rule in applying calculations i.e. the order of executing the calculations will be Brackets, Divisions, Multiplications, Additions then Subtractions (Always remember this when ordering a question)
- Calculate questions that reference other questions should not be placed on the same group as the reference questions since the calculate will not appear unless you move from the group
- When referencing a question in a calculation you need to indicate it as ${Question} where question is the name of the question
- You can "force" a calculation to evaluate by setting 'required' to TRUE

## List of tested calculate functions

** ***(Please feel free to recommend additional ones and we will update)*

#### Working with IF command on calculations

## Work-arounds for calculations

*Recommended for advanced users*

#### Workaround 1: Creating Dummy Questions for Calculation of Final Score in a Series of Questions

Let's say you have a question like QN1 Do you have a question? Answers Yes=1 No=2 Don’t Know=999

In this case you may want to create a dummy QN1 right after QN1 to account for the differences in coding and define the mathematical equivalent. So you will create a calculate question QN1d (*note this is my own naming convention d stands for dummy)* and define the default value as 0 but write the formula as **If (${QN1}=1,1,0) **

Notice in my test form I managed to create the situation and the data appears as coded for Q1N and as calculated for mathematical meaning for Q1Nd. This should be meaning that is meant by your score. *You can do this for calculations such as wealth where Yes and No could mean a different value in different countries*

Once you do this for all your questions you can add a calculate question that sums up all the dummies as

${QN1d}+${QN2d}+ *etc*