Xlsform constraint not working when all input values are not provided

Hello !

I have a total sum Qt calculated with five input questions q1 to q5. Note that some of these five questions may not show up, according to their relevance.
I want to constraint Qt to match with a previously entered value V.
But I’ve noticed that it’s not working when all input questions q1 to q5 are not answered.

Could anyone help me out please ?

Hi @Dieudonne, welcome to the community!

Does your Qt sum shows NaN?

Can you provide a screenshot so community could understand a little better?

1 Like

Yes Qt shows NaN until all input questions (q1 to q5) are answered.
Actually, input value (q1 to q5) derive from a select_multiple question where enumerators will not check all boxes necessarily.

Hi @Dieudonne,

Can you take a screenshot of the calculation and the choices of select_multiple question?

1 Like

This is the survey sheet screenshot to better show the issu. Thanks

This is the choices sheet screenshot

Can you also show the calculation of the Qt too?

1 Like

Sure.
Here is it please

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, changing the calculation of Qt to:
coalesce(${q1}, 0) + coalesce(${q2}, 0) + coalesce(${q3}, 0) + coalesce(${q4}, 0) + coalesce(${q5}, 0)

should hopefully work :slight_smile:

Please let me know if this works.
Best,

4 Likes

It works very fine. :blush:
Thank you very much!

2 Likes