# Can I select 5 Choices, and then the repeat loop happens only for those 5

**URL:** <https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960>\
**Category:** Support\
**Created:** [May 22, 2019, 5:33am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960 "2019-05-22T05:33:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prasanna\_Sundaram](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/prasanna_sundaram/32/6436_2.png) [@Prasanna\_Sundaram](https://forum.getodk.org/u/Prasanna_Sundaram)\
**Post date:** [May 22, 2019, 5:33am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/1 "2019-05-22T05:33:37Z")

</div>

Hello,

New Question, Same form, is there a way that I can make a choice of say 3 out of 10 things, and then the nested repeat loop happens only for those 3 things.

e.g. I have to collect information by team in different classrooms, some classrooms will have 3 teams, some will have 10 . Is there a way that before the repeat starts, the teacher chooses Teams (which are standard named as Team 1, Team 2 .....) and then the repeat function happens only for that chosen list ?

---

<div class="post-metadata">

**Author:** ![ARIF\_AZAD\_KHAN](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/arif_azad_khan/32/2921_2.png) [@ARIF\_AZAD\_KHAN](https://forum.getodk.org/u/ARIF_AZAD_KHAN)\
**Post date:** [May 22, 2019, 6:00am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/2 "2019-05-22T06:00:03Z")

</div>

if the nested repeat information are same and repeat creates based on a multiple selection item then you can use this xls idea.[multiple\_selected\_item\_repeat.xlsx](https://getodk.b-cdn.net/uploads/default/original/2X/3/38e88d7023dee103124a08784043715237c9043c.xlsx) (14.7 KB)

---

<div class="post-metadata">

**Author:** ![Prasanna\_Sundaram](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/prasanna_sundaram/32/6436_2.png) [@Prasanna\_Sundaram](https://forum.getodk.org/u/Prasanna_Sundaram)\
**Post date:** [May 22, 2019, 6:18am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/3 "2019-05-22T06:18:31Z")

</div>

Thanks a lot. I will try running the file you have shared. Would you be able help me understand what does the jr. function do?

Jr:Count ....

count-selected(${item}) - you put this twice, once under Jr:Count and once under Repeat\_Count . Could you help me understand why?

By the time I am here, I am at the sea 🙂 . If I can help understand the logics, i will probably be able to use it multiple contexts -

selected-at(${item}, position(..)-1)  
jr:choice-name(${item\_post}, '${item}')

---

<div class="post-metadata">

**Author:** ![Prasanna\_Sundaram](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/prasanna_sundaram/32/6436_2.png) [@Prasanna\_Sundaram](https://forum.getodk.org/u/Prasanna_Sundaram)\
**Post date:** [May 22, 2019, 6:26am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/4 "2019-05-22T06:26:43Z")

</div>

I am sorry, I am clearly far too novice. I am not able to see how to use this or modify it to run it on the phone once to see what this logic would look like etc.

---

<div class="post-metadata">

**Author:** ![ARIF\_AZAD\_KHAN](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/arif_azad_khan/32/2921_2.png) [@ARIF\_AZAD\_KHAN](https://forum.getodk.org/u/ARIF_AZAD_KHAN)\
**Post date:** [May 22, 2019, 6:57am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/5 "2019-05-22T06:57:38Z")

</div>

To learn XLS form authoring you can check the documents:

[https://docs.opendatakit.org/form-question-types/](https://docs.opendatakit.org/form-question-types/)

> **[XLSForm Docs](https://xlsform.org/en/)**
>
> This site will help you author XLSForms. XLSForm is a tool used to simplify the creation of forms. XLSForm will convert forms authored in Excel into XForms that can be used with a number of web or mobile platforms.

---

<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:** [May 22, 2019, 7:23am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/6 "2019-05-22T07:23:47Z")

</div>

Nothing beats a good example to plagiarize... 😉

[selectrepeat.xls](https://getodk.b-cdn.net/uploads/default/original/2X/f/ffa32413c259456b15e3f8cbbf514842e4c3c36f.xls) (19.5 KB)

Try running this under [XLSForm Online](https://opendatakit.org/xlsform/) to see how all these pieces fit together. You should be able to adapt to your specific need. Also check the online docs for [count-selected()](https://docs.opendatakit.org/form-operators-functions/#count-selected), [selected-at()](https://docs.opendatakit.org/form-operators-functions/#selected-at), [jr:choice-name()](https://docs.opendatakit.org/form-operators-functions/#jr:choice-name) and [repeat\_count](http://xlsform.org/en/#repeats)

---

<div class="post-metadata">

**Author:** ![Prasanna\_Sundaram](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/prasanna_sundaram/32/6436_2.png) [@Prasanna\_Sundaram](https://forum.getodk.org/u/Prasanna_Sundaram)\
**Post date:** [May 22, 2019, 7:34am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/7 "2019-05-22T07:34:32Z")

</div>

Each one of you here is a gift from the universe!! Thanks so much :).

I just used the logic you have provided, it was shared by another kind person on another group. I somehow seem to be getting the First Team Name on all repeat sheets.

Row 34, 35 is where I set the logic calculations and 36 is where the note drops.

Row 29 is where they choose the teams.

What am I doing wrong here? On both the examples, they work on my phone, my own sheet somehow throws up the same team name of Team-A

[inquilab-introduction-activity-form-3.xlsx](https://getodk.b-cdn.net/uploads/default/original/2X/e/e25142d0da224ec9db57b47310288a650d32a805.xlsx) (17.9 KB)

---

<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:** [May 22, 2019, 8:18am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/8 "2019-05-22T08:18:21Z")

</div>

OK. So the 'problem' you are witnessing in your form is a consequence of the fact that you have an _inner_ group _within_ your repeat group, inside of which you are using selected-at(). As a consequence, when you perform

`selected-at(${fruit}, position(..)-1)`

the `'..'` is basically jumping up one group level to figure out which iteration it is in. Normally, that is when you are directly inside a repeat group, this corresponds to the repeat iteration (1,2,3,4...). However, because you are using it within a (single) _inner_ group, `position(..)` is always going to return 1 (!). Hence why you keep getting that dang First Team Name! 😄

The workaround in your case is to jump up another level so that the position() function actually gets the correct iteration that you are in; that is

`selected-at(${fruit}, position(../..)-1)`

OK. I realize this might be getting a bit over your head, but dont worry! 🙂 We're getting pretty down-n-dirty into obscure XPath stuff here, but it is probably worth explicitly documenting in case other's fall into your same trap (@ln?):

> if you try to use position(..) - to find out what iteration your are in - you must be immediately within the encompassing repeat group, otherwise you'll get an invalid index (probably always 1...)

To answer your immediate problem, try this form instead, which - like yours - has a group within a repeat group:

[selectrepeat2.xls](https://getodk.b-cdn.net/uploads/default/original/2X/1/17e9090e0a533bb0217dcf3bf91f55fc97bc5d3c.xls) (20 KB)

---

<div class="post-metadata">

**Author:** ![Prasanna\_Sundaram](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/prasanna_sundaram/32/6436_2.png) [@Prasanna\_Sundaram](https://forum.getodk.org/u/Prasanna_Sundaram)\
**Post date:** [May 22, 2019, 9:16am UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/9 "2019-05-22T09:16:28Z")

</div>

Thank you @Xiphware : This explanation completely helps. Thanks so much for everything !!! 🙂

---

<div class="post-metadata">

**Author:** ![Almasaeid\_Aida](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/almasaeid_aida/32/11045_2.png) [@Almasaeid\_Aida](https://forum.getodk.org/u/Almasaeid_Aida)\
**Post date:** [April 12, 2020, 4:00pm UTC](https://forum.getodk.org/t/can-i-select-5-choices-and-then-the-repeat-loop-happens-only-for-those-5/19960/10 "2020-04-12T16:00:15Z")

</div>

Thanks a lot it's very helpful 🙂
