Sum multiple choice questions

How do you create a hidden value that sums the value of all the relevant multiple choice questions? I have tried “sum(question1)+(question2)+(…)” and it is not working. There are 6 questions within a form with 3 multiple choice answers per question. I gave the “choice value” to each answer a numeric value and want to total the results. Then I will use another hidden value to label the child as high risk, moderate risk, or low risk based on the sum of the answers to the 6 questions.

Hi,

When you say “multiple choice” questions do you mean checkbox questions that allow you to choose more than one value for each question? If so, unfortunately you can’t use the “sum()” function to add all of the values within a multi-select question type, the sum function only works across repeat group elements, although I can explain how to manage this same interaction if you need checkbox questions.

If you are asking about 6 “single select” questions (where only one choice can be chosen) one caveat would be that the system intentionally doesn’t presume an “empty” (no-answer) question should count as a ‘0’, so if you had 6 questions you were adding together you’d need to use an if() or coalesce() function to let the calculation know that no answer should count as a 0, like

coalesce(/data/values_1, 0) + coalesce(/data/values_2, 0)…

-Clayton

1 Like

Thank you! I am using “single select” questions and the coalesce() function worked in my hidden value. I was not familiar with that function and still am not sure what coalesce can do or really is doing, but it is working in the application. Thanks again!

Hi,

Glad to hear it worked. The coalesce() function simply gives the app a ‘fallback’ value to use if a different value is empty. It’s a shorthand for something like “if( /data/myvalue = ‘’, ‘fallback’, /data/myvalue)”

-Clayton