Syntax for adding totals

Good day,

We are about to conduct a survey on agriculture. We have questions asked on total expenditure and the breakdown of the total expenditure thereof (rent, lease, buying stock, fertilizers etc). From experience we have noticed that respondents know the total expenditure more than the breakdown. We have then shied away from using the function to calculate the total expenditure from the breakdown and prefer the respondent to give it to us.

We have put a validation in the total expenditure to check if the total expenditure adds to the expenditure given in each breakdown. The challenge is that not all variables in the expenditure are applicable to everyone. for example if in the previous questions you do not use fertilizers, you do not answer expenditure breakdown on fertilizers. The variable fertilizers in the breakdown is now null for this particular respondent. When testing the validation fro this respondent, it keeps giving error as if there is an error even if all the applicable variables are answered. Iā€™m suspecting it is also the null as a number/figure because when you do not restrict those responding and allocate 0 the error is no longer there.

Maybe we need a syntax that excludes those variables that are not applicable from the validation.

example: total exp= rent + salaries + fertilizers + purchases + fuel
if you do not use fertilizers and fuel, expenditure on fertilizers and fuel will not be asked to you. The above function gives an error.

regards
Rabelani

Rabelani,

The solution would look like this:

totalExp= (rent ?? 0) + (salaries ?? 0) + (fertilizers ?? 0) + (purchases ?? 0) + (fuel ?? 0)

The (someVariable ?? 0) works as follows: if someVariable is NULL (e.g., unanswered or disabled), then Survey Solutions uses the value after ?? instead. (C# calls this the null-coalescing operator)

The choice of 0 makes it so that:

  • The expression always has a numeric value, even if components of the computation are missing (e.g., 250 + 0 = 250, but 250 + NULL = error)
  • The total expenditure is not affected by missing elements of the computation.
1 Like

Dear Arthur

Thank you for the prompt response, much appreciated.

Regards
Rabelani