# Survey123 Connect calculate sum of values resulting from sum in repeat

1553
4
09-15-2022 12:29 PM
New Contributor II

I have four fields that calculate the sum of repeats (related to the amount of funding). I want to calculate the sum of all of the summed repeats, however, when I use the following calculation, nothing appears in the field (I have tried two options and neither work):

option 1: \${sum_funds_est_nfhp}+\${sum_funds_est_other}+\${sum_funds_est_match_nonfed}+\${sum_funds_est_match_fed}

option 2: int(\${sum_funds_est_nfhp})+int(\${sum_funds_est_other})+int(\${sum_funds_est_match_nonfed})+int(\${sum_funds_est_match_fed})

I've attached the group with the repeats for reference.

Thanks!

1 Solution

Accepted Solutions
Occasional Contributor II

Just put 0 into the [default] fields for A, B and C and set the [required] field to yes.

That way there's always a number value in the field to permit a valid calculation. If a user accidentally deletes the zero value (eg they put a number into the wrong field and then delete the whole field) they can't submit until it's corrected.

This won't work if the A, B or C fields are set to hidden or relevant. If they are hidden, the required field fails because of a bug and will permit the form to be submitted with blank fields. If they have a relevant condition, they will be set to null if that condition fails, and the calculation will fail.

In those cases you will need to adjust the calculations based on the relevant/hidden conditions.

4 Replies
Occasional Contributor II

Why did you only attach the survey tab? It requires more effort than uploading the entire XLSX sheet and the only way contributors can even try to replicate the problem is to delete all your constraints or rewrite all the other tabs and all your choice lists.

I'm not going to rewrite the material, so I have just deleted all the constraints, and that works perfectly. That suggests that the problem is being caused by the constraints.

New Contributor

Laurence,

Any idea how to write the calculation to give the sum of all the values that are answered?  I have a similar survey but not all fields are required.  Using the formula in your example, it works, but only if you answer all the questions referenced in the "sum" calculation.

For Example:

 int(\${A})+int(\${B})+int(\${C})

but if there is no value entered for "B" you get no calculation.

Any thoughts?

Occasional Contributor II

Just put 0 into the [default] fields for A, B and C and set the [required] field to yes.

That way there's always a number value in the field to permit a valid calculation. If a user accidentally deletes the zero value (eg they put a number into the wrong field and then delete the whole field) they can't submit until it's corrected.

This won't work if the A, B or C fields are set to hidden or relevant. If they are hidden, the required field fails because of a bug and will permit the form to be submitted with blank fields. If they have a relevant condition, they will be set to null if that condition fails, and the calculation will fail.

In those cases you will need to adjust the calculations based on the relevant/hidden conditions.

New Contributor II

Thanks, Laurence. Next time i'll include the whole XLS sheet. There were a few fields in there that I didn't want to share originally, which is why I didn't include the whole sheet to begin with.

I have the same issue as Leah, where not all of the fields are required, so your second response is super helpful.

Thanks, Kate