Issue with cumulative sum inside a repeat group

1. What is the issue? Please be detailed.
We are developing a survey for avocado pickers, where each picker reports their individual harvest weight multiple times during the day. The goal is to show the cumulative total weight per picker in real-time within the repeat group, not just as a summary after the repeat.

Currently, the form workflow is:

A supervisor selects 10 handlers and 40 pickers for the day.

  1. Pickers report their harvest weight in a repeat group.
  2. The picker’s ID is selected, and the weight is recorded.
  3. Issue: We need the form to dynamically sum all previously entered weights for each picker within the repeat group so that the updated total is visible in real time.
    We have tried using indexed-repeat() but are facing challenges with keeping the cumulative total correct for each picker inside the repeat.

2. What steps can we take to reproduce this issue?
Create a repeat group where pickers enter multiple weight records.
Attempt to sum previous values per picker within the repeat.

3. What have you tried to fix the issue?
Using if(position(..) = 1, 0, Calculate: sum(instance('data')/data/Harvest_group/Harvestbyfield[picker_id = current()/picker_id]/kg_harvested[position(..) < position(current())]))
Calculate; ${previous_kg} + ${kg_harvested}

This works in some cases but does not always track correctly within the repeat.

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

begin_repeat picker_repeat Pickers' Harvest Data
select_one employee_id current_picker Select the picker
integer picker_weight Weight collected (kg)
calculate prev_total if(position(..) = 1, 0, indexed-repeat(total_weight, picker_repeat, position(..) - 1))
calculate total_weight ${prev_total} + ${picker_weight}
end_repeat

Any guidance would be greatly appreciated! thanks :raised_hands:

Would you mind to share a minimized extract or example for your form, please, to allow further investigation?

I trust you now that the sum variable after the loop gets permanently recalculated and can be referenced/shown inside the loop, e.g. in a note (or read-only integer type).

extract firn calculate repeat group.xlsx (12.9 KB)
Thanks wroos

Would you mind to move (or copy) row 35 (note) just before row 33 (end_repeat), please? Maybe this could solve your issue already.

It would be preferable if you could send a less minimal form extract/example, which can be tested stand-alone. (Make sure, please, to avoid any personal data, e.g. names, ids.)

Hey @Jellow ! :wave:

:white_check_mark: I've implemented the logic for you - please find the XLSForm and a short demo below!

:arrow_right: Based on your post, I realized that you need two different types of sums here:

  1. Cumulative sum for a specific picker within a repeat instance, which updates as more instances for the same picker are added within the same repeat group.
  2. Overall sum that can be displayed at the end.

:page_facing_up: I have to admit, this one was quite complex! I even had to revisit ODK’s documentation after a long time to implement this one. But here’s the secret sauce that made it work: [XPath Predicates For Filtering] - buried deep within the documentation!

EachPickerDemo

EachPickerSum.xlsx (11.9 KB)

Hope this helps! Wishing you a great day! :mushroom:

5 Likes

Hint: For nested repeats you could find an example here (esp. for Enketo): https://community.kobotoolbox.org/t/sum-calculation-within-a-repeat-group-to-get-subtotal/65178.

2 Likes

@MinimalPotato Thanks a ton for your support on this! Really appreciate the effort you put into implementing the logic and the video. The breakdown you provided is super helpful, and we managed to make it work :rocket::raised_hands: 1000X times thanks!

2 Likes

If you all have ideas on how we could make some of those docs and techniques easier to find, please share!

In the mean time, let's add an example like this to the docs. :+1:

1 Like

@Jellow , you’re always welcome, buddy! :smile:

Sure, @LN ! I think adding several use-cases, examples, and maybe a few images around it could really help with it! :star2: :raised_hands: