Repeats based on cascading selection

1. What is the issue? Please be detailed.

My goal is to have users select a district and loop through several questions about each health facility linked to the selected district. Districts and health facilities are pre-loaded in choices.

2. What steps can we take to reproduce this issue?

See the spraying_survey example form.

3. What have you tried to fix the issue?

I understand fixed and dynamic repeats based on a user-specified number of repeats, but I'm not sure how to loop based on this type of selection.

4. Upload any forms or screenshots you can share publicly below.

I attempted to modify the spraying example:
spraying_survey_test.xlsx (16.0 KB)

In this toy example, I would like to:

  • select the district
  • repeat through all health facilities in the selected district
    • print "Let's talk about each health facility in [DISTRICT NAME] district" for
    • ask "What is the structure of [FACILITY NAME]?"

For example, if a user selected district=1 (Chienge):

  • Let's talk about each health facility in Chienge district.
    • What is the structure of Chipungu RHC?
    • What is the structure of Kabwe RHC?
    • What is the structure of Kalembwe RHC?
    • And so on for all 6 health facilities in this district

I think there is enough information in choices to make this possible, but I'm not clear on the right approach.

I think there are some potentially useful ideas in this answer, but I've not stumbled on the solution yet.

Alright, getting a bit closer with
spraying_survey_test4.xlsx (16.3 KB)

I'm getting the question to repeat for each health facility BUT the health facilities are not limited to the ones linked to the selected district.

This form includes all 25 health facilities in the repeat.

I believe that my repeat_count needs to limit the count to where districtid=${district} but my current entry generates the total count of the list elements (not the filtered list).

count(instance('health_facility')/root/item)

@LN does anything come to mind for you about how to limit this count? I picked up on the general idea from your answer here. I'm trying to limit the repeats to elements of the health_facility list where districtid equals the selected district.

Thanks for sharing your approach and progress! You are indeed very close.

Yes, that's right, you can do this by adding a filter to your existing count expression:

count(instance('health_facility')/root/item[districtid = ${district}])

You will also need to apply the same filter when accessing the specific facility in your repeat:

instance('health_facility')/root/item[districtid = ${district}][position()=${facility_index}]/label

This first filters the list by district and then uses the repeat position to index into the resulting filtered list.

It's easy with these really dynamic forms to end up with something that's hard to analyze. Make sure to make some test submissions and have a good analysis plan! In particular, I would strongly recommend saving the health facility id in each repeat instance to join on.

spraying_survey_test4.xlsx (16.4 KB)

2 Likes