Calculate sum of multiselect question with repeat group

Dear community,
After several days of researching the forum, various attempts I gave up and decided to ask for help.
I have a question that asks to specify nationality(ies) of beneficiaries. It is a multiselect question as there might be several nationalities. There is also an option Other in case the nationality is not listed as option. The Other option is a repeated group, as it is possible that there are several non-listed nationalities. We ask to specify existing and new beneficiaries per nationality. Default values are 0 and it is possible that some numbers are not filled in by the user (that is what causes the problem I think).
What I need is to calculate the sum of all nationalities. And that is where things go wrong - either it gives the NaN value or it does not sum up values from 2nd other nationality.
What I tried - several calculate questions with sum, coalesce, if function, if selected. I read all about coalesce and tried a multitude of different combinations but still cannot find the right one.
I would appreciate any help, form is attached. multiselect_repeat_sum.xlsx (21.1 KB)
P.S. Sometimes NaN does not appear but after clicking Validate - they do appear.

So which are the variables that you wish to add (sum)?

Dear @Kal_Lam ,

I wish to sum all variables.

So those from within the repeat group?

From within the repeat group and from Multiselect Nationality.
So Jordanian (Existing and New) + Syrian (Existing and New) + Other Repeat group (Existing and New)

@angelinasavchuk, we have a workaround discussed in the community previously which should solve your issue:

dear @Kal_Lam ,

I checked all of them and none seems to work. I think I have a slightly different case then the one discussed previously. I have a repeat group within the multiselect question.

I see a form design issue in your xlsform. You do not need the relevant expression you have used under J12 to J14:

Maybe you will need to validate your xlsform with this online validator before you upload them to the server.

@Kal_Lam Thanks for spotting it! I removed it, but still face the issue of NaN values when summing up all the variables

Have you validated your xlsform with the online validator? Is it clean and free of syntax issues?

Yes, I verify the form there after every change.

Please feel free to share your updated xlsform with the community.

Here is the updated form. multiselect_repeat_sum (1).xlsx (21.2 KB)
This is how the form looks if Other is not selected


If it is selected, the NaN disappear
any ideas what could cause it?

The relevant expression you have entered in K11 i.e. selected(${Nationalities}, 'other') for the repeat group is affecting this. Try removing the same and it should work show you the calculated value.

Maybe as a workaround you could add some expressions in the note questions to display if and only if the value for the same exists.

Dear @Kal_Lam ,

Thank you, it does work indeed but now the question Other appears even if it is not selected. Is there a workaround around that?

Try using the repeat_count by defining the total number of other in a separate question. That should also solve your issue.

My colleague has managed to fix it! The problem was that when others were not selected there was not a number (so there was a NaN) in that field so when doing the sum in the totals, as it had one field being a NaN, the rest were also NaN, regardless of the values for Jordanian and Syrian.
In case you need a solution - here it is.
multiselect_repeat_sum_mod (2).xlsx (14.2 KB)

1 Like

Thank you @angelinasavchuk for sharing this with the entire community.