Hello everyone,
I was wondering if it's possible to control the visibility of a question depending on the number of answers/values coming back from a choice_filter.
The following example:
I have a question 1 with the answer options "A", "B" and "C". Using a choice_filter and a table in the "choices" spreadsheet, I control the form in such a way that question 2 only offers the answers that match the selection from question 1. That works so far.
Now there is a third question. Here, based on the answer from question 2, answer options should also be offered via a choice_filter. However, it is the case that not every answer from question 2 leads to something being offered as an answer option in question 3. Is there a possibility that if the choice_filter has no match, the question will be hidden? Currently, the question text is still displayed, but then without any possible answers. Might irritate users.
I hope the scenario is understandable.
Best
Sven
Solved! Go to Solution.
There are two methods that will work for you:
Method #1: Lazy
selected(${gender}, 'Desire') or selected(${gender}, 'goat/roe deer') or selected(${gender}, 'adult/small animal') or selected(${gender}, 'deer') or selected(${gender}, 'goat') or selected(${gender}, 'Aries') or selected(${gender}, 'sheep')
I just removed duplicates from the age list's gender column and tossed the few items that remained into the above statment. If you replace the current Relevant statement with the bold text, it give the desired outcome.
Method #2: Dynamic
You need to turn your age list into a CSV for this one to work:
string-length(pulldata('test', 'name', 'gender', ${gender}))>0
Toss this into the relevant column. It essentially tests to see if your selection exists.
Edit: Since Method #2 is probably what most people will be looking for, I should probably elaborate on a couple things in case others find this post.
You can put your logic into the Relevant or the body::esri:visible column to conditionally show/hide your third question. The logic is the same for both columns, but they function slightly differently (Relevant: hides and makes values null; esri:visible hides and preserves values)
What that logic is will depend on your form.
Hi @abureaux,
thank you for your reply. I know both columns. I wonder if there is anybody who could give me a hint how such a query could look like. Is it possible to proof, if there are, in dependency of a previous question, values returned by the choice_filter or not. It not, the next answer should not be shown.
Best
Sven
As Ismael stated, it may not be possible. string-length() is one option (probably the best), and hard-coding with selected() is another.
Is it possible to share the Excel?
Hi @abureaux , hi @IsmaelChivite,
I have attached the excel file. Here is a description with pictures:
The use case is the following: The customer records different wild animal species that have been hunted in the field. To do this, he first selects the wild animal species (question 1). Then the subspecies or sex is selected via a choice_filter (question 2). There are also age categories for a gender (question 3). A choice_filter is also used here. The challenge: There is not an age description for every sex/subspecies. In those cases, no answers are offered for question 3, but the title of the question remains there.
There are two methods that will work for you:
Method #1: Lazy
selected(${gender}, 'Desire') or selected(${gender}, 'goat/roe deer') or selected(${gender}, 'adult/small animal') or selected(${gender}, 'deer') or selected(${gender}, 'goat') or selected(${gender}, 'Aries') or selected(${gender}, 'sheep')
I just removed duplicates from the age list's gender column and tossed the few items that remained into the above statment. If you replace the current Relevant statement with the bold text, it give the desired outcome.
Method #2: Dynamic
You need to turn your age list into a CSV for this one to work:
string-length(pulldata('test', 'name', 'gender', ${gender}))>0
Toss this into the relevant column. It essentially tests to see if your selection exists.
Edit: Since Method #2 is probably what most people will be looking for, I should probably elaborate on a couple things in case others find this post.
Hi @abureaux,
this solution worked for us. Thank you!
Best
Sven
I do not think it is possible. We can check if the answer is left empty (with string-length()) but I do not think you can count the number of choices available in a list. Just curious, what is the use case?
This is an interesting problem. I had a similar situation where I show the question with a single choice to fit the scenario. This page looks promising: https://forum.getodk.org/t/choice-filter-returns-no-values-for-select-one-question/39417/2 but at version 3.18 Survey123 connect does not appear to support the instance() function in a formula.
So, that leaves us with 2 options that I can think of.
1. Add a csv to your media folder with two columns, one for the choice from question 1, and one for the count of filtered results. Then use a pulldata to get that second column value. Something like: pulldata('name_of_csv', 'return_column_name', 'key_field_name', ${question_1})
Retrieve a value from CSV
2. A more dynamic solution: Put your full choice list for question 2 into a csv and then use JavaScript to count the number of matching values. The main issue with this option is that your survey can only be run by folks who are in the same organization as the survey is loaded from.
I'm interested to hear how it goes.
Hi @fklotz,
I will have a look at those options! Thanks.