Overview

Calculations for Hidden Values

You can use a special question type called a hidden value to store values from other forms and carry out calculations without it being visible to the mobile worker. The important field is the calculate field, which you can put calculation expressions. Some common examples include:

  • Age in years: int((today() - date(#form//dob)) div 365.25)
  • Age in months: int((today() - date(#form//dob)) div 30.4)
  • Age in weeks: int((today() - date(#form//dob)) div 7)
  • Estimated Date of Delivery (EDD) from Last Menstrual Period (LMP): date(#form//lmp + 280)*
  • Summation of three questions: int(#form//question_one) + int(#form//question_two) + int(#form//question_three)

 

Operation ordering

Icon

Expressions in CommCare are evaluated left to right. The following example illustrates how this impacts expressions:

 

A and B or C

 

is equivalent to:

 

((A and B) or C)

 

Should you wish to evaluate expressions differently you must use parentheses to indicate which elements should be evaluated together e.g.:

 

A and (B or C)

 

 

Display Logic for Hidden Values

 

  • You can put a display condition in a hidden value
  • The hidden value will hold the null value '' until its display condition is met.  Once its display condition is met, it holds the calculated or loaded value.

  • If you update the case from a hidden value, it will only update the case if its display condition is met.

 

Examples

Simple Calculation Example

Icon

This is an example of a simple calculation where the Date of Birth is being calculated. The logic statement goes in the "Calculate Condition" of the Hidden Value.



Complex Calculation Example

Icon

This is an example of a hidden value where the Estimated Date of Delivery (EDD) is being calculated. The logic statement goes in the "Calculation Condition" of the Hidden Value.

 

For further guidance on doing calculations please see Hidden Values Tutorial Part 1: Adding Two Questions Using a Hidden Value and Hidden Values Tutorial Part 2: Calculations Using a Date.

Other calculations can be found on this page.

*EDD calculations: http://www.perinatology.com/calculators/Due-Date.htm and http://nrhm-mctsrpt.nic.in/MHFW/UI/FAQAnc.aspx