Calculations in the Case List and Details

 

Overview

In its basic form, the Case list and Case details definition pages only accept case properties of the module case type. However, there is an addon that allows the use of calculations in the case list/details, called Custom Calculations in Case List. In addition to calculations, the add-on also opens the possibilities beyond the module case type, allowing access to any fixture available in the user’s restore file. 

Enabling the Add-on

To turn on this feature, proceed to the Application Settings (1) -> Add-ons section (2) of the application:

Next, select the button with three-lines in the upper right hand corner and choose “Calculations” (3):

In the Calculation section, check the option “Custom Calculations in the Case List” Add-on (4) and hit save (5) to commit the changes.

Using the Feature

Once the Add-On has been enabled, a new option will appear in the Add Property dropdown list (1) in the Case List or Case Detail definition pages, selecting Calculated Property (2) will create a new field in which it’s possible to define an XPath expressions to perform calculations (3), including the use of case properties.

Writing expressions

Case properties and attributes in Calculated properties are evaluated the same way as Properties in Case List and Details, as long as they are within the scope of the current case being iterated over. For instance, if the selected property is age, Commcare will actually execute “current()/age”. For properties outside the scope, a full XPath query expression is required. See some examples below.

Calculations within the scope

Type

Description

Expression

Example

Type

Description

Expression

Example

Case attributes

Displays the ID of the current case

current()/@case_id

Case indexes

Displays the ID of the parent case of the current case

current()/index/parent

Case properties

Displays the value of the property

current()/age

Mathematical Calculation

Returns the estimated Date of birth according to the Age stored in the case

date(floor(today() - (current()/age * 365.25)))

Calculation beyond the scope

Type

Description

Expression

Example

Type

Description

Expression

Example

Calculation involving the parent case

Displays the name of the Parent case

instance('casedb')/casedb/case[@case_id=current()/index/parent]/case_name

Calculation involving child cases

Returns the number of child cases

count(instance('casedb')/casedb/case[@case_type='member'][index/parent=current()/@case_id])

Calculation involving User properties

Checks whether the User is assigned to the same location as the patient

if(instance('commcaresession')/session/user/data/commcare_location_id=village_id,"YES","NO")

Calculation involving Locations

Retrieves a location's name based on information stored in the case

instance('locations')/locations/location[@id=current()/village_id]/name

Calculation involving Lookup tables

Retrieves a field of a Lookup table item based on information stored in the case

instance('item-list:medicine')/medicine_list/medicine[index=current()/medication_index]/medication