0

Formula returning specific values depending on multiple choice selection

Hi all,

I have a multiple choice field with around 40 options and I would like to set up a formula that returns a certain text depending on the options selected. A certain range of options would correspond to the same text - so e.g:

  • if I select options 1, 2 or 3 the formula would return "X".
  • if I select options 4, 5 or 6 the formula would return "Y".
  • If I select both option 1 and option 6 the formula would return "X, Y".

What would be the best way to set up such a formula?

Many thanks in advance,
Giovanni

8 replies

null
    • Fred
    • 1 mth ago
    • Reported - view

    wow, 40 choices! Anyways, you can use the switch command, but you have to follow this unwritten twist.

    Switches go down the list of cases, so you have to remember to put the cases that have the lowest chances of happening on top and ending with cases with the highest chances of happening.

    Since you have a multichoice field, you can no longer use the equal sign. That is because you get an array of numbers so you have to start using different commands. Look at this post for more information. So you can do something like:

    let mcArray := numbers(dMC1);
    switch true do
    case mcArray[= 1] > 0 and mcArray[= 6] > 0:
        "case 1"
    case mcArray[= 2] > 0 or mcArray[= 4] > 0 or mcArray[= 6] > 0:
        "case 2"
    default:
        "error"
    end
    

    If you put line 3 under line 5 then Ninox will always evaluate the record as "case 2" because the existence of 6 is there before it checks for 1.

    • Giovanni_Zagarella
    • 1 mth ago
    • Reported - view

    Hi Fred,

    Thanks so much for taking the time to reply and for the insights. This definitely sheds some light on how the switch function works, and unfortunately I'm not sure it can be used in the way I have in mind. 

    To clarify, each of the multichoice options would be tied to only one specific case. But I would need the formula to return multiple cases depending on which choices selected.

    Assuming choice 1 = case 1 and choice 2 = case 2, if both 1 and 2 are selected the formula would return "case 1, case 2" as a result.

    let mcArray := numbers(dMC1);
    switch true do
    case mcArray[= 1] or case mcArray[= 3]> 0:
    "case 1"
    case mcArray[= 2] or case mcArray[= 4]> 0:
    "case 2"
    default: "error"
    end
    

    To give a practical example... Let's imagine my multichoice was "Countries" and the formula was used to show their corresponding geographical regions. If I selected Germany and Canada, I would like the formula to return "Europe, North America".

    I have the feeling this is not doable via the switch function though - unless I identified every possible scenario through a line of code, which is not feasible with 40 different options. Would you have a solution in mind?

    Thanks again,
    Giovanni

    • Alain_Fontaine
    • 1 mth ago
    • Reported - view

    There are about 40 primary choices, you said. But how many "X", "Y", etc do you have?

    • Fred
    • 1 mth ago
    • Reported - view
     said:
    I have the feeling this is not doable via the switch function though - unless I identified every possible scenario through a line of code, which is not feasible with 40 different options. Would you have a solution in mind?

     You are correct that a switch would be too complicated. I'm sorry for misunderstanding your request. You can try the following:

    let mcArray := numbers(dMC1);
    let firstGroup := [1, 4, 6];
    let secondGroup := [2, 5, 7];
    let thirdGroup := [3, 8, 9, 10];
    let fgCheck := for loop1 in mcArray do
            if count(firstGroup[= loop1]) > 0 then
                "X"
            end
        end;
    let sgCheck := for loop1 in mcArray do
            if count(secondGroup[= loop1]) > 0 then
                "Y"
            end
        end;
    let tgCheck := for loop1 in mcArray do
            if count(thirdGroup[= loop1]) > 0 then
                "Y"
            end
        end;
    concat(unique(fgCheck), unique(sgCheck), unique(tgCheck))
    

    Of course changing:

    1) dMC1 to be your multi choice field

    2) firstGroup, secondGroup, thirdGroup numbers in the array to match whatever grouping you need.

    3) Change the result in the if statements on lines 7, 12, 17

    You can easily add Groups by adding a new variable, then add a new Check code, then adding the new Check variable to the concat at the end.

    • Alain_Fontaine
    • 1 mth ago
    • Reported - view

    This toy database contains four possible solutions. The last one is completely different, since the multichoice has been made dynamic. This means that an auxiliary data table is needed. The advantage is that all the information needed to compute the result is contained, as data, in that table, which makes the solution easy to maintain.

    • Giovanni_Zagarella
    • 1 mth ago
    • Reported - view

    Thank you so much both for your support. Fred's solution was exactly what I had in mind and I managed to make it work on my database. Thanks again!

      • Fred
      • 1 mth ago
      • Reported - view

      Have you looked at Alain's code? They are do the same thing but in fewer lines.

    • Giovanni_Zagarella
    • 1 mth ago
    • Reported - view

    Ah indeed. I ended up going for solution 2 in Alain's test database and the end result is the same. Thanks again both for your time, much appreciated

Content aside

  • Status Answered
  • 1 mth agoLast active
  • 8Replies
  • 62Views
  • 3 Following