Advanced calculate function using if-statement

Hi All,

Currently trying to input a calculate function on my form

Calculate function is
=IF(${How_many_days_in_the_past_one__004}<1,1,IF(${How_many_days_in_the_past_one__004}>=1,2,IF(${How_many_days_in_the_past_one__004}<=6,2,IF(${How_many_days_in_the_past_one__004}>6,3))))

It works on excel, but not when deploying the form to the server. Attempting to change values for another calculate function

When attempting to delpoy, kobo says this
your form cannot be deployed because it contains errors:

Unexpected KoBoCAT error 400: {“detail”:“ODK Validate Errors:\n>> XForm is invalid. See above for the errors.\norg.javarosa.xform.parse.XFormParseException: Invalid calculate for the bind attached to “${dairy_vit_A_code}” : Bad node: org.javarosa.xpath.parser.ast.ASTNodeAbstractExpr@7a0ac6e3 in expression =IF( ${How_many_days_in_the_past_one__004} <1,1,IF( ${How_many_days_in_the_past_one__004} >=1,2,IF( ${How_many_days_in_the_past_one__004} <=6,2,IF( ${How_many_days_in_the_past_one__004} >6,3))))\n\nResult: Invalid”}

Any tips for this one?

Thanks,
Ben

Dear Ben,
you need to use if (small letters) for KoBo.


IF or If generates an error.
Best regards
Wolfgang

1 Like

Hi @benmcdermott,

Would you mind trying out (using the if statement) as shown in the image below:

Reference xlsform:
If Condition.xlsx (11.5 KB)

GOOD DAY!

Hi @Kal_Lam,

Thanks for providing this solution, it worked perfectly!

Would you have any ideas what the issue with this code is?

if(and(${During_the_last_completed_4_we}=“yes”, ${If_Yes_How_often_did_this_happen}=rarely__1_2_ti, 1), if(and(${During_the_last_completed_4_we}=“yes”, ${If_Yes_How_often_did_this_happen}=sometimes__3_1, 1), if(and(${During_the_last_completed_4_we}=“yes”, ${If_Yes_How_often_did_this_happen}=often__10__tim, 2, 0))))

Trying to put in an extended if code for calculating household hunger scale. Need output values of 0, 1 or 2. The question before is a yes/no question with a skip logic built in, hence why I was thinking of adding an ‘and’ function. Before using just if, i was getting a value of 1

Thanks for your time,
Ben

Hi @benmcdermott,

I see an additional “and” in the syntax you have been using.

GOOD DAY!

Hi @Kal_Lam,

Are you saying to only get rid of one ‘and’ ? If so, could you highlight which one? Or to not use them at all?

Thanks,
Ben

Hello,
if(…) should not start with in any “and”.
Best regards

1 Like