Addition with condition

This formula will do addition for me ${Quarter_1}+${Quarter_2}+${Quarter_3}+${Quarter_4} but what if I want a value if any (Quarter_1, Quarter_2, Quarter_3, or Quarter_4) of the question was answered?. I notice all the question have to be answered for calculation to effective

Hi @sophy and welcome to the community!

That is because In the ODK Form Logic, unanswered number questions are nil. That is, they have no value. When a variable referencing an empty value is used in a math operator or function, it is treated as Not a Number (NaN). The empty value will not be converted to zero. The result of a calculation including NaN will also be NaN, which may not be the behavior you want or expect.

To convert empty values to zero, you can use either the coalesce() function or the if() function.

coalesce(${potentially_empty_value}, 0)
if(${potentially_empty_value}=“”, 0, ${potentially_empty_value})

In your case, you need to put coalesce before every potential empty integer value. Not just before whole calculation.

You can find more information on coalesce function through our community posts that have been discussed previously here .

See documentation, please: ODK XForms Specification and Form Logic - ODK Docs.

@Kal_Lam might be preferable to add it (coalesce) in the Help Center articles too.

1 Like