Select to view content in your preferred language

Calculate default value when only 1 option is available

924
2
10-20-2024 11:28 PM
LindsayRaabe_FPCWA
MVP Regular Contributor

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:

  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
  2. Load the CSV File: Use the pulldata function to load the values from the CSV file based on the answer in Field A.

  3. 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}))
  4. 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}), '')
  5. 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: 

  1. Prepare Your CSV File: Ensure your CSV file is structured properly with columns for the key and values.

  2. 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

  3. Calculate the Number of Matching Choices: Use a calculation field to determine the number of matching choices.

  4. 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.
Lindsay Raabe
GIS Officer
Forest Products Commission WA
2 Replies
ChristopherCounsell
MVP Regular Contributor

I'd separate this into two questions. One that's calculated and one that provides choices.

Use relevancy to show the question based on the number of choices available.

Now how you get this, I'd look at:

  • Creating a CSV file for pulldata against type and one/many values, prepared and update alongside the original CSV
  • JavaScript. You (Copilot) could write a function to count the values matching Type and then apply the outcome to the relevant filter.

Otherwise no it's not possible as far as I'm aware and the Survey123 team has it in their backlog. I personally try to avoid overly complicated applications of formulas to circumvent the lack of available functionality as more often then not it bites you.

https://community.esri.com/t5/arcgis-survey123-questions/default-value-from-cascading-select/td-p/88...

LindsayRaabe_FPCWA
MVP Regular Contributor
Thanks for the suggestions, but I think I'm taking your other approach and avoiding it too. The user will just have to do the extra 1 or 2 taps/clicks on this case. At least the majority of scenarios only have 1 choice and it's not a massive form so not a big case for taps minimisation.
Lindsay Raabe
GIS Officer
Forest Products Commission WA