Hi Team. I'm trying to make my form a little smarter. I have 2 fields: Type and Subtype.
Some hazard types have only 1 matching Subtype, whereas others have multiple to pick from.
I want my subtype field to automatically fill out the only available value when there is only 1 value to pick from, but be blank when multiple options are available. Is this possible? I tried this with Copilot but failed as it tried using Count which I believe is for repeats only, then tried another approach but that also failed (see Copilot outputs below).
Method 1:
Prepare Your CSV File: Ensure your CSV file is structured with columns for the key and the values you want to pull. For example:
key,value
A1,Option1
A2,Option2
A2,Option3
Load the CSV File: Use the pulldata function to load the values from the CSV file based on the answer in Field A.
Create a Calculation Field: Add a calculation field to determine the number of options available for Field B based on the answer in Field A. <- this is the step that threw an error in Survey123 Connect
type name label calculation
calculate options_count Options Count count(pulldata('csv_file', 'value', 'key', ${field_a}))Set Up Field B: Use the relevant column to control the visibility of Field B and the default column to set its default value.
type name label relevant default
select_one field_b Field B ${options_count} > 0 if(${options_count} = 1, pulldata('csv_file', 'value', 'key', ${field_a}), '')Filter Choices for Field B: Use the choice_filter column to filter the choices for Field B based on the answer in Field A.
type name label choice_filter
select_one field_b Field B key = ${field_a}
In this setup:
Method 2:
Prepare Your CSV File: Ensure your CSV file is structured properly with columns for the key and values.
Use pulldata to Retrieve Values: Use the pulldata function to fetch the values from the CSV file. <- this step failed to return all the values, just the first value, thus never generating a reliable count
Calculate the Number of Matching Choices: Use a calculation field to determine the number of matching choices.
Set Default Value Conditionally:
type name label calculation
calculate matching_values Matching Values pulldata('csv_file', 'value', 'key', ${field_a})
calculate count_values Count Values if(${matching_values} != '', count-selected(${matching_values}), 0)
text field_b Field B if(${count_values} = 1, ${matching_values}, '')
In this setup:
- matching_values retrieves the values from the CSV file based on the answer in Field A.
- count_values calculates the number of matching values.
- field_b sets the default value to the single matching value if only one is found, otherwise it remains blank.