Calculations for multi-select questions

Hi all,

I have this calculation for multi-select question type where I want to give a score to the user only if the user selects 'ALL' the correct options in the multi-select question.
test.xlsx (116.0 KB)

Here, opt_q5 & opt_q6 are multi-select questions
I have used the below logic but do not know where am i going wrong:-

calculate opt_q5a if(selected-at(${opt_q5},0),'1','0')
calculate opt_q5b if(selected-at(${opt_q5},2),'1','0')
calculate opt_q5_total ${opt_q5_a} and ${opt_q5_b}

I want opt_q5_total to be 1 if and only if both opt_q5a & opt_q5b are 1 esle 0

Welcome to the ODK forum, @Tarang_Khurana! We're glad you're here. When you get a chance, please introduce yourself on this forum thread. I'd also encourage you to add a picture as your avatar because it helps build community!

for opt_q5_total instead of...
${opt_q5_a} and ${opt_q5_b}

try...
if((${opt_q5_a}='1' and ${opt_q5_b}='1'),'1','0')

1 Like

Hi @Tarang_Khurana,

One other observation here is that I'm not sure if there is a logical test in your if statement?

For example, you say:
if(selected-at(${opt_q5},0),'1','0')

However, I wonder if you want to be making the first part of your if statement a logical test, like this:
if(selected-at(${opt_q5},0)='xxxxxx','1','0')

Re-reading your context, I wonder if you actually want to use "selected-at" or "selected".

For example, if your multiple choice options are:

  1. A
  2. B
  3. C
  4. D
  5. E

And you want to give a score of they correctly choose "A" and "C"...

Your formula could look more something like this:

if(selected(${q5}, 'A') AND selected(${q5}, 'C'), 1,0)

Or something like this:
Q5a = if(selected(${q5}, 'A'), 1,0)
Q5b = if(selected(${q5}, 'C'), 1,0)
Q5total = ${Q5a} + ${Q5b}

Just options in addition to what @danbjoseph has suggested, hope this helps you solve it!

Janna

PS, I'm on my phone so didn't actually test this in an XLSFORM, so apologies if my logic is off here!

2 Likes

Hey @danbjoseph @janna, thanks a lot for your quick responses.
I tried all of the above suggested, also tried a combination of the solutions you have shared, but it does not seem to work, not sure why.

Attaching the revised form, would be super helpful to hear your thoughts on this.
testv2.xlsx (116.0 KB)

I added a note to observe the calculated values and both of the above methods show to be working.
testv2_testing.xlsx (18.6 KB)

+------------------+---------------------------------------------------------------------+
|       name       |                             calculation                             |
+------------------+---------------------------------------------------------------------+
| opt_q5_a         | if(selected(${opt_q5},'q5c1'),'1','0')                              |
| opt_q5_b         | if(selected(${opt_q5},'q5c2'),'1','0')                              |
| opt_q5_total     | if((${opt_q5_a}='1' and ${opt_q5_b}='1'),'1','0')                   |
| opt_q5_total_alt | if((selected(${opt_q5},'q5c1') and selected(${opt_q5},'q5c2')),1,0) |
+------------------+---------------------------------------------------------------------+

1 Like

Thanks a lot @danbjoseph
I made a small mistake and did not add q5_total to the total score, hence was unable to observe that it worked !

1 Like