This example will explain how to calculate a participant's age based on the DOB that was entered for him/her.
1. Add a Date/Time field and change the field name to e.g. 'DOB'
- This is the field where the participant's date of birth will be captured.
2. Add a Calculated Value field and change the field name to e.g. calculate_age
- This is the field where the participant's age will be calculated and stored, but not displayed to the fieldworker.
- Add the formula "=((today() - $DOB) / 365.25)"
- This subtracts the date of birth entered in the 'DOB' field above from the current date (i.e. the date the form is captured). This answer then gets divided by the number of days in a year.
- Ensure that the 'Decimal' data type is selected from the drop-down.
3. If you want to display the calculated age to the fieldworker, add another field (e.g. an Information field) and reference the Calculated Value field's name (in this case it is 'calculated_age') in the display text.
- Use the function "=int($calculate_age)" to drop the fractional part of the age (e.g. if you are 36.83 years old, this would give a result of 36).
- Alternatively, use the function "=round($calculate_age, 0)" to round up to the nearest full year (e.g. if you are 36.83 years old, this would give a result of 37).
Select the Preview button from the toolbar to test your calculation and see how it will be displayed when using the mobile app.
The calculated value can also be used as a source field to base skip logic on. For example, certain fields should only be asked if the participant's age is between 5 and 15 years old.
- The field below will be shown only if the value for age as calculated above in 'calculate_age' is less than or equal to 15 and greater than or equal to 5.