Using a choice filter and calculation for the same question

571
11
01-19-2024 11:41 AM
EricaNova
Occasional Contributor

I am authoring a survey with Survey123 XLSForms and I cannot quite get where I want with choice filters and the relevant field.

The form has a question, "date_drit_test" that the user can leave blank (if no test occurred) or fill out (if a test occurred):

EricaNova_0-1705692694415.png

If the user selects a date, then another question, 'drit_test_result' should appear to indicate test results (which was hidden if no date was selected):

EricaNova_1-1705692876098.png

However, I do not want any null values in the 'drit_test_result' column. If a user does not select a date for the DRIT test, I want the value of 'drit_test_result' to be filled with "Not tested"/"NT".

I've got everything working save the last bit. I also suspect I could make my choice filter without the "helper" column 'date_drit_result' but couldn't figure out how to do it - any thoughts there?

Relevant worksheets & fields:

survey worksheet     
typenamelabelcalculationchoice_filterrelevant
datedate_drit_testDate of DRIT test   
hiddendate_drit_resultWas there a DRIT test date?if(string-length(${date_drit_test})>0, "test_date_present", "test_date_absent")  
select_one test_resultdrit_test_resultDRIT test result test_filter=${date_drit_result}string-length(${date_drit_test})>0
      
choices worksheet     
list_namenamelabeltest_filter  
test_resultINCInconclusivetest_date_present  
test_resultNNegativetest_date_present  
test_resultPPositivetest_date_present  
test_resultNTNot Testedtest_date_absent  
test_resultRTRetestedtest_date_present  
test_resultFFailed (PCR)test_date_present  

 

 

0 Kudos
11 Replies
DouglasKnabe
New Contributor III

Can you use a calculated field to store the final result? if(string-length(${drit_test_result})>0,${drit_test_result},"Not Tested")

to go further, you could set use a note to display this in lieu of the test_result question and use the relevant column to only display it when there's no date selected

abureaux
MVP Regular Contributor

Simple solution is to rename drit_test_result to something list drit_test_result_slct then add a calculate:

abureaux_1-1705700934614.png

EDIT: Updated calculate. I forgot to include the jr:choice-name() component to save the label rather than the name. Basically, just need to choose whether you want to save the name or label from drit_test_result_slct.

0 Kudos
EricaNova
Occasional Contributor

Thanks for your suggestion @abureaux ! I'm not sure I understand your response fully and I'm getting an error when I try to incorporate your suggestion (maybe around the first appearance of ")

EricaNova_0-1705930924215.png

 

 

0 Kudos
abureaux
MVP Regular Contributor

There are three ways you could do this.

This is what I was thinking. Basically, it automates the "Not Tested" part.

abureaux_1-1705938575244.png

Here is what you have. This works perfectly fine but just takes an extra click (just make the select_one "required"):

abureaux_2-1705938604909.png

Here is a slightly modified version of yours that removes that extra click. It's convoluted, but it does work:

abureaux_4-1705939076860.png

EDIT: I removed the 'note' from Option 3. It was just there for proof of concept since the select_one is hidden for part of this operation. It wasn't required to make this work.

0 Kudos
EricaNova
Occasional Contributor

Your answer looks cleaner - but then am I storing the full text (e.g., "Not Tested" vs "NT")? How can I add a domain to that calculate field?

0 Kudos
abureaux
MVP Regular Contributor

Each of the three options all have slightly different applications, so just select the one that works best. At first, I was just thinking Feature Reports, which Option 1 would do well.

Options 2 and 3 will handle domain values better since they deal with the actual select_one. Option 3 would automate the process more for your end users since they don't need to make a selection at all.

0 Kudos
EricaNova
Occasional Contributor

Thanks so much for staying with me @abureaux !

I tried Option 3, and it is mostly working. Users don't see options for test result when they leave the date field empty. If they leave the date field empty, "NT" / "Not Tested" is selected. If they choose a date they can choose any of the other test result options. However, the drit_test_result field is still displaying codes instead of labels in my table when I view it in Survey123 (my other fields with domains show the labels). 

Also, when I attempt to edit a record and delete the entry from date_drit_test, "NT" is not auto-populated (even though "calculateMode" is set to "always"). 

Erica

0 Kudos
abureaux
MVP Regular Contributor

Opps. There was an error on my part in Option 3. This should work as expected.

The idea is you would use "drit_test_result" as your reference field as it will have either the user's selection (if there is a date) or "Not Tested" (if there is no date). This should minimize user input while preserving your domain values. "drit_test_result_slct" can be ignored in the table as it is just an intermediary field, but you don't want to make it null if you are allowing editing because you want that user selection (if any) to be preserved.

Survey tab:
abureaux_1-1705950937187.png

Choices tab:

abureaux_4-1705950985696.png

When you submit:

abureaux_0-1705950911332.png

When you Edit a submission:

abureaux_5-1705951028881.png

Note: That "Just a random select" at the end is just a plain select_one I used for side-by-side testing.

0 Kudos
EricaNova
Occasional Contributor

I replaced the single quotation marks with "" and it works better... but now I'm getting labels in one field and code in the other:

EricaNova_0-1705938202455.png

 

0 Kudos