Avoid duplicates in repeat group

Hi, I have a repeat group question about children which asks the same set of questions (ID number, age, sex…) about each child. Is it possible to make sure that you can’t enter the same child ID in the same household for 2 different kids?

This is a bit difficult to accomplish in a form when the individual question is asked, since when duplicate inputs are provided the form doesn’t have a way to determine which of the two inputs was the “bad” input.

One way to correct for this is to create a hidden value outside of the repeat group which uses the count() and distinct-values() functions to make a flag that simply signifies that a duplicate input has been provided by the user in the set of ids (roughly: count( /data/repeatgroup/idquestion) != count(distinct-values( /data/repeatgroup/idquestion )), and then display a generic “Each ID must be unique” prompt after the ID question any time this flag is identified. In 99% of cases that would provide the right user experience: Showing a label to the user immediately after entering an duplicated value.

here’s a thought:

building on what Clayton suggested, perhaps if you had a string outside the repeat that joins the id’s with a space character, and inside the repeat, you use the function selected()

that should theoretically work, and it would tell you at the time of entering the ID if you had entered it already rather than having to check all the id’s after being done with the repeat

haven’t tried it, but i imagine it should work as desired


Thanks so much, Clayton! I created a hidden value outside the repeat group called childID_duplicate:
count(#form/childrenRepeat/childID) != count(distinct-values(#form/childrenRepeat/childID)), somehow “data” became “#form” automatically, and a warning label with display condition:
#form/childID_duplicate = true.
But I got the following error when testing:

Calculation Error: Error in calculation for /data/childID_duplicate uses an invalid reference inside a count function

Do you have any idea what could go wrong?

Interesting, it looks like there’s a limitation for the input type of the distinct-values function which requires an underlying selector.

I think you can work around that by switching the second half of the expression to be a space separated list like the following

count-selected(join(" ", distinct-values()))

which should be equivalent to what I suggested

Hi Clayton,

You solution initially worked but since we have blank values for Child ID it doesn’t work right now. Do you have any suggestions? Foe example, there are 3 children in a household but one of them does not meet our enrollment criteria so we don’t assign a child ID to him. In this case, with your solution, the dup warning shows up even if the two entered child IDs are unique.

You could create a hidden value inside of the repeat which is set to either the child_id (if one exists) or a random value if child_id is blank. I’d suggest using the uuid() function, which is effectively guaranteed to produce a string which won’t match any other string.

Setting the out-of-repeat-group count summation to the new hidden value instead of the child_id should provide the intended behavior.