# Calculation Questions

Dear All,

I have a questionnaire formulated with multiple group sections (questionnaire attached). There are relevant questions assigned to these groups to afford skips. Some of these groups have got questions that calculate at the end of every group and result is noted in the questions highlighted blue . These calculations at the end of the group are working well. However there is a question that is aggregating all the calculations into one for.ex summing all the entries of the subtotals from each group(Blue highlight). This is the calculation that is not reflecting.

Is it because of the relevant conditions attached to the different subsections? If so, how do i navigate around them?

Is there a way to write a relevant conditions for an integer entry or text entry?...How is that done?
_20180403-Household-Survey-Questionnaire-Amendedd.xlsx (63.6 KB)

Possibly yes. But in reality its probably the fact that your overall total (of subtotals) calculation isnt accommodating the fact that some of the subtotal may be empty/nill (eg the associated group is irrelevant). Typically, a summary calculation of the form `\${subtotal1} + \${subtotal2} + \${subtotal3} + ...` implicitly assumes all the subtotals have a value; if not, eg `\${subtotal1}` is irrelevant/nil, then the calculation will try to convert its nil value to a number, which becomes `NaN` ('not-a-number'). And as soon as any calculation is 'contaminated' by a `NaN`, the whole dang calculation becomes `NaN`! So you'll need to introduce a check into your calculations to make sure that you return a 0 when any subtotal is empty/nil. eg

`if (\${subtotal1}='', 0, \${subtotal1})`

Yup. Perhaps post what yer trying to do and I'll explain how to do it.

2 Likes

Thank you Xiphware,
so about checking the calculations, which column do I use to check the calculations and how do I specifically do it? Is it for every calculation question?

`\${subtotal1} + \${subtotal2} + ...`

replace it with

`if(\${subtotal1}='',0,\${subtotal1}) + if(\${subtotal2}='',0,\${subtotal2}) + ...`

That way any irrelevant/unset/nil subtotals will add 0 to the total (as opposed to introducing NaN)

2 Likes

Fantastic! fires perfectly. Thanks a lot Xiphware

YW. Its a pretty common issue folks trip over: summing responses, some of which may be nil, and incorrectly assuming these will return 0. Yer not this first person to hit this, and almost certainly wont be the last [perhaps a good candidate for a FAQ, @LN ?]

Basically, if you ever start seeing NaNs in your form, I'd start by looking for calculations that might be pulling in nil/empty responses.

If you want to learn more and understand why such things happen, start reading up on XPath, or at least the aspects of XPath most related to ODK documented here .

1 Like