Counting the number of all options in a select_one / select_multiple variable

Agreed. Perhaps @yabasha and @gerlonggs can provide some more specific usecases to help guide when and in what circumstances counting choices is needed/desirable.

1 Like

Perhaps, but no more or less than the existing jr:choice-name(), which is of a similar ilk...

@Xiphware Agreed. Perhaps @yabasha and @gerlonggs can provide some more specific usecases to help guide when and in what circumstances counting choices is needed/desirable.

Here's a usecase I am currently working on; I have two forms, a Registration form and a Consultation form. The Registration form registers patients in a facility and creates an appointment with a doctor. The Consultation form is to pull data from the Registration form using the search() function into a select_one when a doctor’s name is selected. I would like to count the number of patients in the select_one since there’s a maximum number of patients that can be seen by a doctor per day.

Hello,

I'm interested to know how I can use this trick as I am willing to randomize my select choices. Kindly assist

check this document https://docs.opendatakit.org/form-question-types/#randomizing-choice-order

Hi Arif,
The documentation doesn't seem to show how I would count the number of options once I have randomized the choices.

What trick do you mean? There is no trick the topic is in features category that means it's just a proposal.

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() function to count how many there are.

Have a play with this form in https://opendatakit.org/xlsform/

count-choices.xls (19.5 KB)

[Update] You can also trigger turning the choices to a nodeset with a simple choice_filter, which avoids randomizing your choices... A simple no-op filter like true() will suffice

count-choices2.xls (20 KB)

[this trick courtesy @LN :wink: ]

3 Likes

Here's one solution that has worked for me. I have a calculate field with this sort of expression in the calculate field:

count(instance('list_product')/root/item[string-length(name)>0])

Where "list_product" is the name of the list itself, and the "string-length(name)>0" just ensures every list item that has a length larger than 0 gets counted so basically everything.

2 Likes

@Xiphware,

Great trick! works absolutely fine if list is part of external_choice or choice list.
I am loading the values from external CSV.
Therefore entry in my choice list is only one, so I always get count 1 whereas list is more 10-15.
It it possible to calculate/count the options from list?
Thanks in advance!

1 Like

A post was split to a new topic: Count selected options

did anyone find a solution to @Prashant_K's query?

@Dipankar_Das, the solution posted above by @Hussein_Lightwalla will do what you need.

If the CSV is list_product.csv this will count the total number of options.

You could also filter this CSV, eg if you had columns type and location in the file and you previously asked questions to get ${product_type} and ${product_warehouse} you could use the following to count the number of options that match the type and location fields:

count(instance('list_product')/root/item[${product_type}=type and ${product_warehouse}=location])

2 Likes

Thank you @ahblake.
This works wonders for me :))

Also, in addition to this, is there any solution if you want the same calculation when you're working with search() and has multiple columns in it?

Can you explain what it is you are trying to do?

yes. I will try to explain to the best of my ability.

so, I am trying to make an attendance sheet for multiple sports team, that cascades from District>block>VCDC(another division under a block)>Group/Team name>Sports Trainer> Name of all the children (mulitple_select_type) under that specific team assigned to a specific sports trainer.

In the end, I am trying to calculate the total number of children present (counting all the selected) and total number of children in that specific group (which is where I need help) so that it facilitates my calculation to count the number of children absent.

so I have used choice_filter in my previous attempt and it works just like the way I want it, but that requires me to upload 9 media files into the server.

But now I am trying to employ the search function which requires to upload a single csv file to do the same cascading (which is working except the calculation) and also, calculate the total number of children from specific group/team.

I hope this makes it clear what I am trying to achieve here. :))

CTR_Attendance_form (test).xlsx (8.8 KB)

Why not add a column to the single CSV that indicates which choice list it belongs to and merge all the lists together, then stop using the search appearance and use select_one_from_file with a choice filter instead (eg for selecting VCDC that matches the selected block, type='vcdc' and block=${Block} etc). Then you can count-selected to get those in attendance and count the instance where ${Group}=group (should be enough, but could add type='child' and stcm=${stcm} etc as needed) to get the total count for the group and take the difference as those absent.

Or is your CSV so massive that this is slow to use?

You can generate the unique list of values from your main list in many ways, a quick one in excel would be to use UNIQUE (in combination with SORT &/or FILTER), if the range above was converted to a table Table1, then =SORT(UNIQUE(FILTER(Table1[block],Table1[type]="child"))) would return the array block1, block3 spilled into cells as it's only acting on rows where column block matches child.

image

1 Like