# Count choices in filtered option list of cascade

**URL:** <https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211>\
**Category:** Support\
**Tags:** odk-xlsform\
**Created:** [October 19, 2024, 2:54am UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211 "2024-10-19T02:54:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danbjoseph](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/danbjoseph/32/21901_2.png) [@danbjoseph](https://forum.getodk.org/u/danbjoseph)\
**Post date:** [October 19, 2024, 2:54am UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211/1 "2024-10-19T02:54:48Z")

</div>

I have a cascading select for locations and sub-locations. Not all locations have a sub-location and if that is the case, I want to use relevant to hide the sub-location question. Is there a way to do this?

I tried what is described in the post linked below, but it seems to always count the total number of sub-location options, not the length of the filtered list.  
[count\_filtered\_cascade\_options.xlsx](https://forum.getodk.org/uploads/short-url/bOaeMRV3sNfEas5blDL4kvfb0ve.xlsx) (16.2 KB)

> [@Counting the number of all options in a select\_one / select\_multiple variable](https://forum.getodk.org/t/counting-the-number-of-all-options-in-a-select-one-select-multiple-variable/17393/28):
>
> Actually, there is a 'trick'... wink. If you randomize the choices the XForm will turn them into a nodeset, which you can then use the XPath [count()](https://docs.opendatakit.org/form-operators-functions/#count) function to count how many there are. Have a play with this form in [https://opendatakit.org/xlsform/](https://opendatakit.org/xlsform/)[count-choices.xls](https://forum.getodk.org/uploads/short-url/95ThXNds9JVan7umiBGOkYmcRVr.xls) (19.5 KB) [Update] You can also trigger turning the choices to a nodeset with a simple [choice\_filter](https://docs.opendatakit.org/form-logic/#filtering-options-in-select-questions), which avoids randomizing your choices... A simple no-op filter like true() will suffice [count-choices2.xls](https://forum.getodk.org/uploads/short-url/kl5kNaFy2vcD3sG0VM44hDP3NjA.xls) (20 KB) [this tr…

---

<div class="post-metadata">

**Author:** ![ahblake](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/ahblake/32/22392_2.png) [@ahblake](https://forum.getodk.org/u/ahblake)\
**Post date:** [October 19, 2024, 3:26am UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211/2 "2024-10-19T03:26:11Z")

</div>

Your count just needs to be a tiny bit more specific and include a filter to only count those that match location.

`count(instance('sub_locations')/root/item[${location}=location_filter])`

[count\_filtered\_cascade\_options\_edit.xlsx](https://forum.getodk.org/uploads/short-url/yzK2qRdk9hMjYo32knWdmIQuhU2.xlsx) (16.4 KB)

 ![image](https://getodk.b-cdn.net/uploads/default/original/3X/2/d/2dd91fe2dac6225e1e8c5c61a43179079c530d0a.png)

 ![image](https://getodk.b-cdn.net/uploads/default/original/3X/e/4/e418846a8031ab6aeb23b27e6318fda41ce6e81c.png)

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [October 19, 2024, 6:59am UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211/3 "2024-10-19T06:59:28Z")

</div>

BTW, now that _all_ choice lists in XLSForms are converted to nodesets (see [https://github.com/XLSForm/pyxform/pull/614](https://github.com/XLSForm/pyxform/pull/614)) there is no longer any need for circa 2019 'tricks' to accomplish this. 🎉

So as @ahblake most astutely states, you just need to perform a little XPath 'black magic' to include the equivalent cascading select filter into your `count()` input nodeset. Yes it is a bit obscure, but there is tremendous (hidden) power in these XPath expressions that let you accomplish magical things like this! 🪄

---

<div class="post-metadata">

**Author:** ![danbjoseph](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/danbjoseph/32/21901_2.png) [@danbjoseph](https://forum.getodk.org/u/danbjoseph)\
**Post date:** [October 20, 2024, 2:36am UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211/4 "2024-10-20T02:36:06Z")

</div>

Terrific. Thanks both! Much appreciated.

---

<div class="post-metadata">

**Author:** ![danbjoseph](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/danbjoseph/32/21901_2.png) [@danbjoseph](https://forum.getodk.org/u/danbjoseph)\
**Post date:** [May 14, 2025, 8:49pm UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211/5 "2025-05-14T20:49:50Z")

</div>

Do I need to modify something for this to work inside of a repeat group? And will it work on a a list that's part of a `select_one_from_file` question?

---

<div class="post-metadata">

**Author:** ![LN](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/ln/32/1605_2.png) [@LN](https://forum.getodk.org/u/LN)\
**Post date:** [May 14, 2025, 9:09pm UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211/6 "2025-05-14T21:09:36Z")

</div>

> [@danbjoseph](#):
>
> And will it work on a list that's part of a `select_one_from_file` question?

Yes!

> [@danbjoseph](#):
>
> Do I need to modify something for this to work inside of a repeat group?

Your question suggests that it's not working as expected. 😬 The intent would be for no modification to be needed but it's possible there's an issue there because it is more complex. Can you show an example of what's not working?

---

<div class="post-metadata">

**Author:** ![danbjoseph](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/danbjoseph/32/21901_2.png) [@danbjoseph](https://forum.getodk.org/u/danbjoseph)\
**Post date:** [May 14, 2025, 11:10pm UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211/7 "2025-05-14T23:10:30Z")

</div>

EDIT: I was mistakenly referring to the question name and not the external file inside the `instance('...')` part of it. This file shows what wasn't working, it has my mistake: [counting\_options\_length.xlsx](https://forum.getodk.org/uploads/short-url/pLzOqe6N3WwJO9LmpGScNBXjDne.xlsx) (14.5 KB)

 ![Screenshot 2025-05-14 at 7.07.23 PM](https://getodk.b-cdn.net/uploads/default/original/3X/e/0/e0240f0c18b0caa4fca3a1c784f3f04e7eec40f4.png)

---

<div class="post-metadata">

**Author:** ![LN](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/ln/32/1605_2.png) [@LN](https://forum.getodk.org/u/LN)\
**Post date:** [May 16, 2025, 9:10pm UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211/8 "2025-05-16T21:10:15Z")

</div>

I'm so glad you got it working!

> [@danbjoseph](#):
>
> I was mistakenly referring to the question name and not the external file inside the `instance('...')` part of it.

I can absolutely see doing that too. Nothing at all along the way gave you any kind of warning or error??

---

<div class="post-metadata">

**Author:** ![danbjoseph](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/danbjoseph/32/21901_2.png) [@danbjoseph](https://forum.getodk.org/u/danbjoseph)\
**Post date:** [May 16, 2025, 9:26pm UTC](https://forum.getodk.org/t/count-choices-in-filtered-option-list-of-cascade/50211/9 "2025-05-16T21:26:01Z")

</div>

It just didn't output anything. No errors or warnings when uploading the draft.
