Calculating inside a loop

Hi All,

Am doing a project which requires to calculate number of caregivers in a home(this is a loop).
Based on the number of children under each care givers I will need to do another loop for the children under each caregiver.
Is it possible to have 2 separate loops but the total sum in the 1st loop acts as the control for the 2nd loop and I will also need to pull in the names of the care givers into the 2nd loop so as to know how which child belongs to which caregiver.
After establishing the no of children I will then need to do a 3rd loop with some questions for each child pulling their names(if possible also the caregiver) in the 3rd loop so as to know which child is being referred to.
Unfortunately this is the approach required as opposed to loop with a loop within a loop.

Looking forward to any help I can get.



Hi @PaulN,

See an example on how to calculate inside a repeat group:

In the survey sheet:


In the choices sheet:



Hi, i have a kind of similar scenario, please help me.

I have a question that ask for the number of children , i expect an answer let’s say 4
I then have a question that follows the first question where the respondent breaks down the number of children which is 4 into respective age groups.

Lets say
0-4 yrs 1 child
4-10 yrs 2 children
11-15 yrs 1 children

15 yrs 0

I need to ensure that the age group division is correct , that is the sum of the children in question 1 is equal to the total in the individual groups.

Any help on how i can implement the above?


Hi @Kal_Lam,

Thanks for the sample.
Quick question;
If I wanted to save the calculation into a type: integer so as to use it for the 2nd repeat loop.



Hi @billy_1,

Your query should be solved from the post that i replied earlier:

Hi @PaulN,

Your calculation should be saved in the integer question type. Please download the data-set in xls version. You will see the variable there which you are able to use for the second repeat loop as well.


Please resend the excel as I only have a screenshot.



Hi @PaulN,

See the attached xlsform: Household (Count) Repeat Example.xlsx (37.0 KB)

Thank you for the feedback.

1 Like

Dear all, I am a new beginner for this tool; I am developing a simple form for data verification as monitoring tool to ensure the data consistent between field recording and reporting number to office. I am stuck in processing to create data sum up of 3 months for verification purpose. Please see examples of verification here.

I am looking forward to hearing your advice or supports.
My best

Hi Kal,

Do you mind sharing the XLSFORM of the above solution? I have used the same approach as you but I keep getting zero as the results of the sum.

Thank you

Hi @kimanikihiu,

Welcome to the community!

Reference xlsform:
Household (Count) Repeat Example.xlsx (37.0 KB)

Have a great day!

Hello @Kal_Lam I have a more complex case. I have done the calculations of total cost and net profit for different crops using select_one question type. Now the issue is that it works for the first selected crop and does not continue summing after the first.

Please I need your help.

Welcome to the community, @DiffoB! Could you provide more details (kindly please try to explain your issue in detail by providing dummy questions, choices, and the conditions you wish to see) so that the community could also have a look at it?

Thank you for responding
I mean that I have a series of integer questions above and I want to sum all of them as total cost. Then use the total cost to get the net profit which is (Total Cost - income). This will be done through repeats for different crops. At the end I’d like to give the net profit for different crops from the repeats but it only works for the first crop and then does not work for the subsequent crops. Please find a sample attached to it.
Sample.xlsx (18.9 KB)

@DiffoB, you could use the sum() function as used in the post above. Then you could subtract it from the desired variable (depending on how you wish to calculate).

Thanks @Kal_Lam It works very well but it only works for the first repeat then breaks at the second