Select to view content in your preferred language

Retrieve a value from a repeat

529
1
02-23-2022 07:51 AM
CaraWhalen
New Contributor

Hello!

I am wondering how to pull a specific value from a repeat. I have a Survey123 that will be used for counting the number of birds present at grouse leks. The survey has a repeat in which the observer documents how many male, female, and unknown grouse are present at the lek at multiple observation times during a survey.

The Survey123 is setup to pull the maximum number of males and the maximum number of females observed at the lek during a survey. However, I also want to know what times the maximum male and maximum female observations occurred, and this is the part I'm struggling with. I want to pull the observation time value from the repeat that is associated with the maximum male count (and the same for females).

CaraWhalen_1-1645631023720.png

I understand the concept of using position(..) and indexed-repeat to retrieve a value from a repeat, but I don't know how to implement them in conjunction with the max() function.

Any help would be appreciated!

Thank you!

0 Kudos
1 Reply
AndyMcClary
New Contributor III

I'm pretty sure that you're going to need to use a custom javascript function to accomplish this. Something like:

function indexMax(fieldname){

var maxValue = fieldname[0];

var maxIndex = 0;

for (var i =1; i<fieldname.length; i++){

if (fieldname[i]>maxValue){

maxIndex = i;

maxValue = fieldname[i];

}

}

return maxIndex;

}

You would call this function in a calculate field (outside of the repeat) using the pulldata function pulldata("@javascript","functions.js","indexMax",${MaleCnt})

Which will give you the index of the record with the max value, which you can then use to get the time value with the indexed-repeat function. One caveat with this is that as written it will give you the index of the first record with the max value but that could be easily adjusted depending on how you want to handle ties.

0 Kudos