Select to view content in your preferred language

Calculation Doesn't Fire Inside Repeat

03-29-2019 07:19 AM
Regular Contributor
integertotal_repeatsNumber Repeatscount(${dummy_counter})
decimalmean_vegAverageround(sum(${num_veg}) div ${total_repeats},2)
begin repeatquadsQuad Data Entry
hiddendummy_counterincraments repeat count1
integernum_veg# Vegetative0
decimalnum_veg_sqdifSq Difference Vegpow((${num_veg} - ${mean_veg}),2)0
end repeat

A portion of the XLS form is above, the problem is that the num_veg_sqdif doesn't automatically re-calculate after new repeat records are added. Notice how the calculation uses the mean of all values inside the repeat. This mean value changes when a new repeat is added, thus all previously calculated num_veg_sqdif values need to be re-calculated.

The only way to do that is to manually page back through the repeat records and hit the arrow button (looks like the recycle symbol) on the num_veg_sqdif field.

The survey123 docs say this: "Unlike the default column, which is evaluated only when the survey is opened or the question becomes relevant, calculations are evaluated every time answers in your survey that are inputs to the calculation change or become relevant."

Why isn't the re-calculation happening inside the repeat, is this expected behavior?

BTW: this entire post would be a moot point if there were  a few more aggregation functions that work with repeats, submitted idea here: 

2 Replies
Esri Esteemed Contributor

Hi Dan,

With regard to the form you have, the issue is that the average updates, but the square difference calculations for the repeats not currently in focus do not update.  This is expected behavior for Survey123.  We are working on supporting more complex calculations such as standard deviation through javascript functions that can use a repeat's entries as input parameters; expect to see information in the Early Adopter Community soon on this.

As for additional aggregation functions, we are limited in our inclusion by the XLSForm specification - ODK XForms Specification 

Tags (1)
New Contributor III

@JamesTedrick I was searching how to do Standard Deviation in Survey 123 and found this post, thought I would tag it too with the answer. Appreciate all the good help you give James! This post helped me find what I was looking for. I was just trying to do live calculating Standard Deviation for samples in a repeat. I found an example in the "Samples" in Survey123 Connect called JavaScript Examples. This used JavaScript to calculate the SD for a "sample" (u-1) if you needed population it would need to adjust the formula. There is not a lot out there explaining this, would be nice if some of the s123 documentation was updated, SD seems like a pretty valuable tool.

0 Kudos