Formulas supported in Survey123 include the following operators:
Symbol | Function | Example |
---|
+ | Addition | 2 + 2 |
- | Subtraction | 2 - 2 |
* | Multiplication | 2 * 2 |
div | Division | 2 div 2 |
= | Equal | .=10 |
!= | Not equal | .!=10 |
< | Less than | .<10 |
<= | Less than or equal to | .<=10 |
> | Greater than | .>10 |
>= | Greater than or equal to | .>=10 |
or | or | .=10 or .=20 |
and | and | .>10 and .<20 |
Formulas can be used in Survey123 when building your forms in the following columns:
Constraint
Adding a constraint to a survey question will restrict the accepted inputs for a response. This could include a specific range of numbers, combinations of letters and numbers or general pattern matching. In your spreadsheet, the constraint expression is entered into the constraint field and helpful text is entered into the constraint_message field. In the constraint expressions, the input for the question is always represented by a full stop.
For example, the following formula can be used to restrict the input of an integer field to positive numbers only:
.>=0
Regular expressions can also be used for pattern matching to constrain valid responses. This regular expression can constrain the input of a string field to a well-known email address format:
regex(., '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}')
See here for more information on regular expressions...
See here for more information on using constraints...
Relevant
A question, or a sets of questions, can be hidden and revealed based on previous answers using relevant expressions. These expressions are entered into the relevant column and the answers to previous questions are always referred to as follows: ${field_name}. You can apply a relevant expression to a single question, or group questions together and set the relevant expression for the entire group. For example, this expression will reveal a question if the answer to the previous question is true:
${previous_question} = 'true'
This example will hide questions if the answer to the previous question was greater than 100:
${previous_question} <= 100
This example combines multiple operators and questions:
${previous_question} + ${other_previous_question} <= 100
When using mathematical operators, be aware that sometimes you may need to cast values into numbers. In the previous examples we assumed that previous_question and other_previous_question where integers or decimals... but what if the question types were strings? Then you cast them as follows:
int(${previous_question}) + int(${other_previous_question}) <= 100
See here for more information about using relevant clauses...
Calculations
Calculations are performed in the calculation field of a question. Calculations are often associated with calculate type of questions, but can also be applied to integer, text etc type of questions. The outcome of the calculation can be used to populate relevant or constraint expressions by referring to the field name of the calculate question. They can be used to hold values that do not need to be displayed on the form, but are included in the feature service.
For example, you can create a question of type calculate and name it calc, then insert the following expression int its calculation column:
${question_1} + ${question_2} + ${question_3}
And then use the result to set the relevance for the next question:
${calc} <= 100
See here for more information on calculations...