Sum of answers if equal to or greater than 1

I have the following order of questions:
1 (multiple choice)- Choose the animals in your herd:
a)cow; b)duck; c)goose

example: only two animals have been chosen

2 (integer)- How many cows do you have? 8
3 (integer) - How many ducks do you have? 8

4 - Total number of animals: 16

I would like to add up the values given in questions 2 and 3, if the answer is that there are cows and ducks on the farm, but if the person selects a goose, the question would add up the value. My questionnaire only adds up if I select all the multiple choice options.

Hi @barata_clicia1,

Can you provide a screenshot?

If all of the answers are not selected does it show N/A?

1 Like


Hi @barata_clicia1 I faced a similar challenge sometime this week.
In this case, it appears that the calculation expression is not being fulfilled since there are missing values in the overall expression.

My Solution
To solve this, I implemented the Default Field and equated the unselected options to zero to fulfil the expression.

Hope this helps.

1 Like

It would then be necessary to create an IF logic code, so that if the other options are not selected, they are considered to have a value of zero, but how would I do this?

I managed to solve the problem:
if( ${_60_1_Quant_bezerros} =‘’,0, ${_60_1_Quant_bezerros} )+if( ${_60_2_Quant_novilhas} =‘’,0, ${_60_2_Quant_novilhas} )+if( ${_60_3_Quant_garrotes} =‘’,0, ${_60_3_Quant_garrotes} )+if( ${_60_4_Quant_boi_magro} =‘’,0, ${_60_4_Quant_boi_magro}) +if( ${_60_5_Quant_boi_gordo} =‘’,0, ${_60_5_Quant_boi_gordo} )+if( ${_60_6_Quant_vaca_magra} =‘’,0, ${_60_6_Quant_vaca_magra} )+if( ${_60_7_Quant_vaca_gorda} =‘’,0, ${_60_7_Quant_vaca_gorda} )+if( ${_60_8_Quant_touro} =‘’,0, ${_60_8_Quant_touro} )

2 Likes

You can use the coalesce() function, See documentation Form Operators and Functions - ODK Docs and ODK XForms Specification. Additional examples can be found with the search function of this forum, e.g. Search results for 'sum coalesce' - KoboToolbox Community Forum.

@Kal_Lam, might be could to add/explain coalesce in the Kobo Help Center doc. (Also not yet included in XLSForm.org).

1 Like

@barata_clicia1, :tada: