A calculation field allows you to specify a formula or calculation about variables present within the study.
To make calculations with values of different fields, it must be possible to reference other fields, and this is done by using the Variable name entered into the study fields.
To create a calculation field, select the appropriate button in the Form Builder Field List:
Within the dialog window, complete the field as normal.
The Calculation template box is used to specify the formula/calculation you wish to perform. To reference other fields, place braces '{ }' around the field variable name e.g. {INCL03}.
See the image below for a full example:
Please note that the calculation field value is updated when the fields that are used in the calculation are filled in, and if a user opens the form for a participant in which the calculation field is located.
It is always necessary to open the form which contains the calculation field to make sure the calculation is updated.
For example, if we want to calculate the BMI using two fields 'weight' and 'height' and the fields 'weight' and 'height' are located in the form Baseline while the BMI calculation field is located in the BMI form, it is necessary to open the BMI form to run the calculation.
In recent versions of Castor, a feature has been released to remove this burden from site users and allow the calculations to update in the background, without the need of actively open the forms where the calculations are present.
To ensure that every value is updated, make sure to enable it from the Study Settings:
Common JavaScript syntax
To get you started, here are a few common JavaScript operators to use for basic calculations along with their description.
| Description | Operator |
| Variable | {Variable} |
| Addition | + |
| Subtraction | - |
| Division | / |
| Multiplication | * |
|
Exponential (x to the power of y) |
Math.pow(x, y) |
| Equal to | == |
| Not equal to | != |
| Greater than | > |
| Greater than or equal | >= |
| Less than | < |
| Less than or equal | <= |
| Logical "AND" | && |
| Logical "OR" | || |
Referring to surveys
It is also possible to base the calculations in study forms upon data entered in a survey.
When creating the calculation field, enter the variable names defined in your survey fields.
For example, using responses from a survey where the patient provides information about weight and height with variable names {surv_pat_weight} and {surv_pat_height}.
These variables can be used to create a calculation field inside the study forms that refers to that field, for example:
{surv_pat_weight}/Math.pow({surv_pat_height},2)
Note: Castor can only calculate in reference to the latest filled in version of a survey.
This means that it is not possible for calculations to refer to different versions of a survey inside a participant’s record, only to the most recent version provided.