Formulas calculation (if statement)

if(${age} < 1), ‘0-11 months’, if(${age} >= 1 and ${age} < 15), ‘1-14 years’, if(${age} >= 15 and ${age} < 19), ‘15-19 years’, if(${age} >= 20 and ${age} <= 24), ‘20-24 years’, if(${age} >=25),‘25+years’)))))

Welcome to the community @swill123! Try this (it should solve your issue):

if((${age}<1),'0-11 months',if((${age}>=1 and ${age}<15),'1-14 years',if((${age}>=15 and ${age}<20),'15-19 years',if((${age}>=20 and ${age}<=24),'20-24 years',if((${age}>=25),'25+years','')))))

In the survey tab of your xlsform:

Data entry screen as seen in Enketo:

Reference xlsform:

If Statement.xlsx (9.6 KB)

1 Like

@swill123 you can also apply the formula

if((${age}<1),‘0-11 months’,if((${age}>=1 and ${age}<15),‘1-14 years’,if((${age}>=15 and ${age}<20),‘15-19 years’,if((${age}>=20 and ${age}<=24),‘20-24 years’,‘25+years’)))) where you have ensured that age as a question cannot be blank. If you have age as a blank, then I suggest you revert to the calculation provided by @Kal_Lam

Stephane

1 Like

Hello,
Another option might be to create a 2nd duplicated select and use a calculation to map each original age to the grouping, with the grouping as label and jr:choice-name or grouping as name (label) with intelligent choice_filter.

Disadvantage: You cannot hide the second select.
Advantage: Can allow you to get a pair of name and label also for the grouping.

2 Likes

Hi Kal_Lam,
how can I skip a question if the previous question includes any response.

Q1. Where did you go yesterday? (text)
Q2. How long does it take to get there? (select one) (This question will be asked only if Q1 includes response)

less than 30 minutes
30 minutes to 1 hour
1 hour to 2 hour

@TunLin, FYR:

I also can’t figure out where I am missing it:
if((${P1}<=40), ‘NON-COMPLIANCE’, if((${P1}>40 AND if((${P1}<70),‘MODERATE COMPLIANCE’, if((${P1}<=70), ‘COMPLIANCE’))))

my formular is failing: if((${P1}<=40), ‘NON-COMPLIANCE’, if((${P1}>40 AND if((${P1}<70),‘MODERATE COMPLIANCE’, if((${P1}<=70), ‘COMPLIANCE’))))

what am I missing?

Welcome to the community, @PChipopa! Maybe you could do it as outlined below:

if((${P1}<=40), ‘NON-COMPLIANCE’, if((${P1}>40 and ${P1}<70),‘MODERATE COMPLIANCE’, if((${P1}>=70), ‘COMPLIANCE’,'')))

Thanks it worked well. well done.

1 Like

Hello, I am a beginner to KOBOToolbox. Every time I use the If statement, I get this response:

“Failed to execute ‘evaluate’ on ‘XPathEvaluator’: The string ‘If(false(),“poor”,“non-poor”)’ is not a valid XPath expression.”

What am I doing wrong? Appreciate any help on offer

Welcome @TushaarKOBO,
if must be small letters.

1 Like

Thanks. But the following is still not working. Much appreciate any help:

if((${q1}=0),1200, if((${q1}=1),2000, if((${q1}=2),3000, if((${q1}=3),3600, if((${q1}=4),4000, if((${q1}=5),4200)))))

Hello @TushaarKOBO,
It seems the number of your brackets ( …) is not balanced.
I would recommend always checking your form during development with the online validator.

1 Like

@TushaarKOBO, try this out as advised by @wroos (you had a bracket mis-match):

if((${q1}=0),1200,if((${q1}=1),2000,if((${q1}=2),3000,if((${q1}=3),3600,if((${q1}=4),4000,if((${q1}=5),4200,''))))))

1 Like

Thanks so much. I was stuck on this these past 3 days.

1 Like

if${tubewell}=1,"${y1}","${y2}"

what is the problem with the above? It says unexpected comma.
Regards,
Tushaar

Hello,
if(…). Add brackets, please.

1 Like

@TushaarKOBO, maybe this post discussed previously should help you understand and use the if-statement properly:

1 Like

Thanks so much for your help and guidance.

1 Like