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
you
could use age >= 18
to determine if they were 18 or over.
Supported operators¶
-
Simple mathematical operators:
+
-
*
(multiply) and/
(divide). e.g.field1 + field2
-
Brackets. e.g.
1+2*3
evaluates to 7 but(1+2)*3
evaluates to 9. You can nest brackets. -
Boolean logic operators:
-
&&
(and) and||
(or). e.g.field1 && field2
would mean we require a value from both fields. -
!
(not) e.g.!field1
would yield 1 if field1 was not given/set empty or zero; and 0 if field1 had data. -
Comparison operators:
-
<=
(Less than or equals),<
(less than) >=
(Greater than or equals),>
(greater than)==
Equal to-
!=
Not equal to -
null coalesce operator
??
: e.g.field1 ?? 123
would yield the value offield1
unless it wasnull
, in which case it would yield 123 -
ternary/if operator:
expr1 ? expr2 : expr3
would evaluateexpr1
and if it is truthy (not empty, not zero) returnexpr2
, otherwiseexpr3
-
cast to number:
#expr
would takeexpr
and 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 null
:
5 + null
null ? 1 : 2
null > 0
null <= 4
null == null
Even this is considered un-evaluatable and therefore null123 / 0
Division by zero evaluates to null (infinity is not handled)
Options fields¶
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
options like red
, green
, then you could have an expression like
colour.red
which would evaluate to 1 if they selected red, and 0 if they
hadn’t.
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._value_
where 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
items.
Relevance¶
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.