Calculate sum from loop and merge field | XM Community
Skip to main content

Hello,
I'm working on an evaluation form where the survey calculates a sum from a field within a loop and merge block.
First, the user enters the number (n) of items to start the loop and merge block, which will loop for n # of times.
Then, the loop and merge happens for that number of times (n). There is a field in that loop and merge block called "direct costs", so if n=5 they are entering the direct costs of 5 items.
After the loop and merge ends, I need to be able to show a sum of n, or the total direct costs.
I'm not sure if there's a function to sum up loop and merge values, and display them in a descriptive field. If I use a math operation: q://1_QID#/ChoiceTextEntryValue+q://2_QID#/ChoiceTextEntryValue+.....my max of 20 loop and merges, if someone enters 2 for n, the piped text returns an invalid expression due to all of the undefined values being used in that expression. The same goes for embedded data (although I'd like to avoid using embedded data, because it disables the ability to go back and edit entries preceding the embedded data in the survey flow)
Is this something I can even implement? I haven't been able to find a similar use case in the community questions...

Convert the undefined values in Number, if you can use the below then it won't give you an invalid expression error,
Number("${q://QID1/ChoiceTextEntryValue}") + Number("${q://QID2/ChoiceTextEntryValue}") + Number("${q://QID3/ChoiceTextEntryValue}")
For undefined values, Number converts it to 0.


After the loop and merge block in the survey flow just add embedded data element 'TotalDirectCost' and set its value to below:
$e{ q://1_QID464/ChoiceTextEntryValue + q://2_QID464/ChoiceTextEntryValue + q://3_QID464/ChoiceTextEntryValue + q://4_QID464/ChoiceTextEntryValue +........+ q://20_QID464/ChoiceTextEntryValue }
In qualtrics math operation if syntax is correct and if the expression does not get a value for some pipe text then it is considered as 0. So make sure the syntax is correct.


Thank you both. I will most likely use the first approach in order to avoid using embedded data.


Leave a Reply