If Statement in Choice Filter for Cascade Selects

1. What is the issue? Please be detailed.
I have a Cascade Selects question that has several layers, has multiple values in some cells, and I want to put a a choice filter with that has several nested If Statements which look like are not recognized. The issue is I'm using duplicate responses with some optional questions. The reason I'm doing it this way is because it will be easier to maintain a list this way instead of having unique named choices for every combination of responses.

This is what is currently in the choice filter in row 16 select_activity

If(${q_place2}!='','selected(username, ${username}) and selected(places0, ${q_place0}) and selected(places1, ${q_place1}) and selected(places2, ${q_place2})', If(${q_place1}!='','selected(username, ${username}) and selected(places0, ${q_place0}) and selected(places1, ${q_place1})', 'selected(username, ${username}) and selected(places0, ${q_place0})'))

which returns: XPath evaluation: cannot handle function 'If'

3. What have you tried to fix the issue?

My situation looks most similar to this but I couldn't find anything else similar on the forum. While my form validates by using these three by themselves, I don't know how to use them in combination with each other to get the behavior I want.

selected(username, ${username}) and selected(places0, ${q_place0}) and selected(places1, ${q_place1}) and selected(places2, ${q_place2})

selected(username, ${username}) and selected(places0, ${q_place0}) and selected(places1, ${q_place1})

selected(username, ${username}) and selected(places0, ${q_place0})

The basic logic is to start by looking at the column in the choices tab that is furthest to the right to see if its empty and proceed to the left.

I should also note that I made a post earlier today regarding Collect not displaying proper responses when regex is used in a choice filter with multiple comma separated values.

4. Upload any forms or screenshots you can share publicly below.
Farm_Time_share.xlsx (13.3 KB)

1 Like

Hi @Tyler_Depke Looks like your issue arises because of the use of if() function in a choice filter. As far as I know if() is not natively supported in ODK's XPath for choice filters. ODK only recognizes basic XPath functions such as selected(), count(), etc for the choice filter, most likely why your single selected() functions work by themselves.

If they work by themselves, have you tried putting them together this way:

(selected(username, ${username}) and selected(places0, ${q_place0}) and selected(places1, ${q_place1}) and selected(places2, ${q_place2})) 
or 
(selected(username, ${username}) and selected(places0, ${q_place0}) and selected(places1, ${q_place1}) and not(${q_place2})) 
or 
(selected(username, ${username}) and selected(places0, ${q_place0}) and not(${q_place1}) and not(${q_place2}))

This way, the logic moves from right to left by evaluating conditions step-by-step without requiring an explicit if().

1 Like

@Clement , it looks as though the behavior with the not() functions included messes things up a bit.

Using your formula, there are responses that are filtered out that should still be displayed. This occurs for any rows that have text in columns to the right that are not empty. Two examples I can give in the current form are:

  1. When user 'thatsme' selects cashew for places0 and trees for places1 it doesn't display the 'water' response because places2 for that row has a value in it.
  2. Likewise when user 'thatsme' selects nursery for places0, it should but doesn't display:
    Verify
    Water
    Verify Others
    Plumbing/Irrigation Setup/Install

Notice that all of these rows have values in places1 column or both places1 and places2 columns

With the exception of those scenarios, it doesn't look like there are any other problems. I'm still working on this but haven't figured it out yet. Does the order of the or statements matter? I'm going to experiment with going left to right instead of right to left.

1 Like

So I don't know if there are official rules on the forum with regards to text generated by ChatGPT as some forums have put a specific ban on that. Anyway, ChatGPT did help get the choice filter logic figured out to give the proper behavior.

"selected(username, ${username}) and selected(places0, ${q_place0}) and 
(
    (${q_place2} != '' and selected(places2, ${q_place2}) and selected(places1, ${q_place1})) or 
    (${q_place1} != '' and selected(places1, ${q_place1}) and ${q_place2} = '') or 
    (${q_place1} = '' and ${q_place2} = '')
)"

Here is the full XLSForm with working choice filter and the full list of farm activities. This is the start of a multilingual time sheet intended to be used to track labor activities on a farm.
Farm_Time good filter share.xlsx (14.9 KB)

1 Like

Computers are so pedantic :sob: This is subtle -- the problem being complained about here is that If with a capital I is not known. There should be no problem with using if with a lowercase if.

I think that's an absolutely wonderful use of ChatGPT. If you know what you're trying to achieve and have the skills to evaluate the results, my experience has been that LLMs are great tools for speeding up form design. The key thing is that you have to evaluate the results critically.

The new expression does look equivalent to the original one. Here's an even further simplified version you may like:

selected(username, ${username}) and selected(places0, ${q_place0}) and 
(${q_place1} = '' or selected(places1, ${q_place1})) and 
(${q_place2} = '' or selected(places2, ${q_place2}))

Luckily we haven't had any issues with poor quality generated responses so we don't have any specific policy. The forum doesn't particularly incentivize prolific posting so I think we'll be ok! My personal opinion is that chatbot AIs can be a great way to clarify one's own thinking and incorporating some of the results of that in forum discussion seems great. They can be really excellent rubber ducks! (CC @Joseph_Muganga with whom I was chatting about this some time back :blush:)