How do I add and then divide in the calculate column

Is this the correct format

(${state_presence} + ${security_forces} + ${freemovement} + ${incidents} + ${health} + ${water_elec} + ${media} + ${livelihood} + ${social_life} + ${cohesion} + ${gender}) / 11

I am adding up the results of a series of responses and dividing the total by eleven in this case. Not sure if I should have 11 in quotations

Thanks for any help.

Your form has syntax errors:

  1. The division operator is div, not /. See https://getodk.github.io/xforms-spec/#xpath-operators.
  2. You have duplicate names for questions: incidents and media. Names must be unique (case-insensitive).

You can always use Central or the Online validator to check your form during development, please.

As selects are stored as strings, you might better add a cast: number(${SelectOneVariable}).
See https://getodk.github.io/xforms-spec/#fn:number.

And if any field can be empty, you need to use coalesce(${SelectOneVariable}, '0'). See https://getodk.github.io/xforms-spec/#fn:coalesce. This function works with strings. An empty field will then count with 0 for your average. If you want to only calculate an average of the sum of non-empty fields, you would need another approach/formula.

So, you might finally best use (number(coalesce(${SelectOneVariable}, '0')) + ...) div 11.

For testing your calculations, you could temporarily change the type to decimal. (Or show them in a note.)

2 Likes

Hi

Thank you for the detailed explanation. I will try your recommendation.

Yes the online validator is what I usually use to finalize the document and translate everything to XML but wanted not to make ten million mistakes before getting to that phases on this rather long document.

Good catch on the empty field issue. I am requiring an answer, so no field will be empty, but I am curious to see how the coalesce command works. I have read about it but avoided it because it sort of bends my mind when trying to work out its logic.

Is this the correct syntax? ((number(coalesce(${state_presence},'0')) + (number(coalesce(${security_forces},'0')) + (number(coalesce(${freemovement},'0')) + (number(coalesce(${incidents},'0')) + (number(coalesce(${health},'0')) + (number(coalesce(${water_elec},'0')) + (number(coalesce(${media},'0')) + (number(coalesce(${livelihood},'0')) + (number(coalesce(${social_life},'0')) + (number(coalesce(${cohesion},'0')) + (number(coalesce(${gender},'0'))) div 11 ?

Sorry, brackets number is unbalanced. The online validator and Central test feature will notice this and show an error. You can remove the one ( before number(..), everywhere.