Relevance / Calculation Expressions¶
Hark survey parts can use expressions based on data from other parts. This can be used for relevance (whether a question should be shown) and for calculated parts (where a part's value is determined by a calculation).
You reference data by the code name you gave that part. e.g. if you
asked for someone's age with a number field, and codenamed that
age >= 18 to determine if they were 18 or over.
Simple mathematical operators:
field1 + field2
1+2*3evaluates to 7 but
(1+2)*3evaluates to 9. You can nest brackets.
Boolean logic operators:
field1 && field2would mean we require a value from both fields.
!field1would yield 1 if field1 was not given/set empty or zero; and 0 if field1 had data.
<=(Less than or equals),
>=(Greater than or equals),
!=Not equal to
null coalesce operator
field1 ?? 123would yield the value of
field1unless it was
null, in which case it would yield 123
expr1 ? expr2 : expr3would evaluate
expr1and if it is truthy (not empty, not zero) return
cast to number:
exprand force it to being a number: if it was empty or text or null it would become 0.
Let’s talk about null¶
Sometimes you encounter
null data. This can be the case if a survey
question has not yet been answered so there is no data yet, or you've
referenced a field that does not exist. Generally speaking: any expression
that uses a null will be evaluated to null as well. It is considered that
if we find null then we can’t evaluate the expression. So all the
following result in
5 + null
null ? 1 : 2
null > 0
null <= 4
null == nullEven this is considered un-evaluatable and therefore null
123 / 0Division by zero evaluates to null (infinity is not handled)
For option values we need to reference the stored value as well, e.g. If
you had a Favourite colours question code named
colour and some
green, then you could have an expression like
colour.red which would evaluate to 1 if they selected red, and 0 if they
This works with radios and checkboxes alike. If a checkbox field (allows
multiple options) you might have
colour.red && colour.green to identify
those who selected both red and green. Because it uses 0 and 1 for
not-selected and selected (respectively) you can use any of the above
operators, e.g. you could see whether they’d ticked 3 or more options by
adding together all the possible options, and use this to open up a new
set of questions for rainbow people.
Sometimes you might want to get the stored value (not the label) of an
options field, particularly for radios when you might just want the selected
value. You can access this using
colour is the code
name of your field.
_value_ is a special code that means get the value of the selected
item. If you use this with checkboxes where there’s possibly 0 or more
items selected, this will return null if nothing is selected, or the first
selected option's value if one or more are selected.
_count_ is a special code that means return the number of selected
Survey parts have a 'relevance' field (in the Meta tab) which takes an expression. If that expression evaluates truthy then the question/part is considered relevant, otherwise it's hidden.
You can apply these to a single part, or a part that contains other parts, e.g. a group, a page or a section.