Use of if condition on responses of multiple choice question for a calculate question

Hello!,

I am trying this to identify a person based on responses of kind of occupation done in past 1 yr, and identify him based on responses:
Q1 (Multiple choice) Occupation(s) in past 1 year
Options:
Unemployed (xml value = 1)
Worked on daily wages in unorganized sector (xml value = 2)
Worked on salary in unorganized sector (xml value = 3)
Worked in organized sector (xml value = 4)
Ran business (xml value = 5)

Q2 (calculate) if(${occupation}=1 or ${occupation}=2,YES,NO) {cal_identify}

Q3 (note) Person had regular source of income: ${cal_identify}

So if a person selects either 1 or 2 or both, then it should become ‘Yes’ otherwise ‘No’, but it is not working.

Kindly help, thanks.

For a select_multiple, you need to use the selected(…), see https://getodk.github.io/xforms-spec/#fn:selected

Hello @wroos!

I tried this but even if the wrong combination of options are used, it is giving response as true. I am not able to select the person only if option 1 and /or 2 is selected. If either of the option is selected with options 3 to 5, then the person is not eligible.

Guide me to get the desired result.

Thanks in advance!

Could you share

  • the related part of your form (e.g. screenshot), incl. the choices
  • your current try for the calculate.

Probably the calculate must result in strings, e.g. ‘YES’ or ‘NO’.

@peeyush, maybe you could refer to this post discussed previously, which should give you a clue of how to handle if condition for a select-multiple question.

And what if option 2 is selected with 3 or 4 or even option 5? Does that too go at No?

Dear All,

First of all my apology that I made typo error in my describing my problem posted above, kindly read the corrected description as:

I am trying this to identify a person based on responses of kind of occupation done in past 1 yr, and identify him based on responses:
Q1 (Multiple choice) Occupation(s) in past 1 year
Options:
Unemployed (xml value = 1)
Worked on daily wages in unorganized sector (xml value = 2)
Worked on salary in unorganized sector (xml value = 3)
Worked in organized sector (xml value = 4)
Ran business (xml value = 5)

Q2 (calculate) if(${occupation}=1 or ${occupation}=2,NO,YES) {cal_identify}

Q3 (note) Person had regular source of income: ${cal_identify}

So if a person selects either 1 or 2 or both, then it should become ‘No’ otherwise ‘Yes’, but it is not working.

Kindly help, thanks.

Please check the reply for correction in description of problem.

Dear Mr @wroos ,

I am sharing the spreadsheet with all the ways I have tried mentioned as Trial 1, 2 and 3
aNK7vtgBn6A3LAHkNu7Pnn.xlsx (6.9 KB)

Dear Mr @Kal_Lam,

Desired output in Q3 should be ‘No’ when only 1 or only 2 or both 1 & 2 are selected. For all other combinations the desired output should be ‘Yes’ . In the above spreadsheet, I am partially successful with Trial 1 but when both 1 & 2 are chosen then it doesn’t work.

Thanks!

You may try
if( selected(${occupation}, ‘1’) or selected(${occupation}, ‘2’) , ‘NO’, ‘YES’ ).
See link to syntax documentation.

1 Like

I tried this but it is not giving desired output for combinations other than specified above.

I find that both if() and selected() can work if ‘or’ and ‘and’ could be used together, but the documentation tells they can’t be used together in single calculate question.

Let me try some workaround.

Did you change your first trial row like written in the last post, please?

Yes, I did.

Problem in result: Even if, any of the option 3, 4, 5 are selected with 1 &/or 2, the output remains NO, ideally it should become YES.

Ok, I now understood, that 3, 4, 5 will dominate a combination with 1 or 2.
So, you may try
if( selected(${occupation}, ‘3’) or selected(${occupation}, ‘4’) or selected(${occupation}, ‘5’), ‘YES’, if(selected(${occupation}, ‘’), ‘’, ‘NO’) ).
I also added an empty result, if nothing is selected (yet).

1 Like

or and and can be combined, but not inside the same (bracket) level. You may need to put them in bracket parts with brackets, e.g if( (${a} = 1 or ${a} = 2) and (${b} = ‘4’ or ${b} = ‘5’), … , … )

1 Like

This is returning error:

  • FormLogicError: Could not evaluate: if((selected( /model/instance[1]/aNK7vtgBn6A3LAHkNu7Pnn/occupation ,1,2 and selected ( /model/instance[1]/aNK7vtgBn6A3LAHkNu7Pnn/occupation ,!=3,!=4,!=5))=‘TRUE’,‘NO’,‘YES’), message: Cannot read properties of undefined (reading ‘toString’)

but I get the idea, I am trying to make it correct, if it works will share here.

Your syntax is not correct.
.

Would you mind to try this, please.

1 Like

Hi!
I had copy pasted your suggested syntax, but it gave me following error, while trying to preview the form:

  • FormLogicError: Could not evaluate: if( selected( /model/instance[1]/aNK7vtgBn6A3LAHkNu7Pnn/occupation , ‘3’) or selected( /model/instance[1]/aNK7vtgBn6A3LAHkNu7Pnn/occupation , ‘4’) or selected( /model/instance[1]/aNK7vtgBn6A3LAHkNu7Pnn/occupation , ‘5’), ‘YES’, if(selected( /model/instance[1]/aNK7vtgBn6A3LAHkNu7Pnn/occupation , ‘’), ‘’, ‘NO’) )., message: Too many tokens.{“stack”:[{“t”:“root”,“tokens”:[{“t”:“str”,“quote”:“'”,“v”:“”},{“t”:“arr”,“v”:[{}]}]}],“cur”:{“v”:“”}}

In the meanwhile I have found the workaround for my query, sharing the screenshot below:

This is giving me desired response, I have kept the response mandatory in my question to avoid any blank response situation.

Thanks!

Hello @peeyush
You can test it here, with the syntax from above. You may just upload and Preview the file to the Online Validator ODK - XLSForm Online v2.x.
RegularIncome02.xlsx (10.6 KB)

I don’t have any error message in KoBo Preview, in Validator (Preview), FormBuilder or in KoBo deploy. Maybe you used " instead of 2 single '?
You don’t need three steps.


This is the initial (empty) situation. That is why, I would also provide an empty calculation result.

Here edit/save in FormBuilder

Side-note: A person might also have other “regular source of income”, e,g, student grant, humanitarian aid, social aid from government aid, remittances from family members/friends, pension. Also, being paid on daily basis, sometimes can be a rather “regular” income/working condition. And, (self-employed) “business”, like street hawker, can mean a rather irregular income.

1 Like

Thanks it worked!

On side note: The above mentioned condition was an example of similar problem I was facing while while drafting a form with different set of questions, so the complete logic of questions is not shared here.