The art of hiding

05-28-2016 02:27 AM
Esri Notable Contributor
12 29 28.2K

This blog post is all about using the relevant column in your XLSForm to help you control the visibility of questionsSmaller.gif in your survey.  Mastering this is an important smart form design skill. A good smart form will never show end users a question unless it is necessary. The days where an endless collection of questions and data entry boxes are presented to the user should be over. The relevant column is present in the XLSForm survey worksheet and can optionally hold an expression. If the expression evaluates to true, the corresponding question will be shown. If the expression evaluates to false, it will be hidden. You can use data anywhere from your form as part of your expression, making your forms dynamically change based on the user's responses.

Lets go first through some examples:

Say you only want to ask a question if the response to a previous question is 'yes':

select_one yes_noowner_presentIs the house owner present?

Other relevant expressions may include:

  • ${PreviousQuestion} < 18
  • int(${QuestionA})+ int(${QuestionB}) > 100
  • today() - ${LastInspectionDate} > 1000*60*60*24*15 (the last inspection happened more than 15 days ago)

The expression syntax uses the XLSForm specification. To learn more about how to write your own expressions check the Formulas—Survey123 for ArcGIS | Documentation help topic.

When using mathematical operators, one needs to be careful with empty responses. If a question has not been answered, it evaluates to 0. This would make for example ${PreviousQuestion} < 18 always return true, if ${PreviousQuestion} has not been answered.  To resolve this issue, you can add your expression to an if block as follows:

  • if (condition, a, b)  If the condition is met, returns a, otherwise returns b.
  • if(string-length(${PreviousQuestion}) = 0, false, ${PreviousQuestion} < 18)
  • if(string-length(${LastInspectionDate})=0, false, today() - ${LastInspectionDate} > 1000*60*60*24*15)

The safest way to check if a question has  been answered is using string-length(${QuestionName}).  If string-length returns 0, then you known the answer is empty. This function works well with question types of all sorts: with numbers, text, images, dates etc

In some cases, you may want to use regular expressions (regex) For example, the following expression can be used with a note type of question. If the expression evaluates to  true, the note is shown, otherwise it is hidden.

textemailYour e-mail
noteType a correct e-mailregex(${Q2}, '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}')

A special case when using relevant statements are select_multiple questions. For them, the count-selected and selected statements are very handy. In the example below, the 'Select other topping:' question only appears if the choice 'Other' has been selected from the list of toppings.  The count-selected() function is used to display a 'Too many toppings' warning if more than 2 toppings have been selected.

select_multiple toppingstoppingsPizza toppings
texttop_otherOther topping:selected(${toppings},'Other')
noteToo many toppings!count-selected(${toppings})>2

To learn more about select_multiple questions, have a look 

Relevant expressions can be applied to many different types of questions: select_one, integer, text, image, barcode, repeat etc. You can also apply them to notes as well as to Groups and RepeatsGroups are of particular importance because a relevant statement applied to a Group will have effect over all questions within that Group.  If you have three or four questions to which you want to apply exactly the same relevant expression, you may want to consider creating a Group and apply the expression only to the Group. This will make your Survey load faster because you will be reducing the number of times the expression needs to be evaluated.

Another aspect to keep in mind is the effect relevant statements have on submit. If a question is not relevant (it is hidden), then data in that question will not be submitted to ArcGIS.

To learn more, check out the video tutorial below!