How to Use Calculated Fields
If your questions are not answered on this page, please contact us.
Creating a Calculated Field
To create a calculated form or task field, create or edit a form or task type on the WellTraq configuration tab and click the "Calculated" button to insert a new calculated field. You will notice that this new field has a data type of "Calculated Decimal" and an expression that you can define by clicking the "Edit Expression" button. That button will open the Edit Expression dialog where you can define an expression for your new calculated field.
Writing an Expression
The Edit Expression dialog enables you to easily build your expression using the buttons for all available inputs and the most common operators. Clicking the buttons will automatically insert the specified operator or variable at the currently selected position in the "Expression Text" text box. You can also manually type any of these operators or variables into the text box as well as a variety of other more advanced operators and functions (see Further Reading below). To manually type in a variable, use square brackets as follows:
Parameter Type | Usage |
Current value of a field | [Field:Field Name] |
Previous value of a field (Forms only) | [PrevField:Field Name] |
Current value of a property (Forms only) | [Property:Property Name] |
When you're finished, you can use the test inputs on right side of the Edit Expression dialog to verify that your expression is working as intended. Note that the expression must return a decimal number, and only numeric and date data types can be used as variables.
Example
A possible expression for a field named "Tank Volume" might be:
3.142 * ([Property:Tank Diameter] / 2) * ([Property:Tank Diameter] / 2) * [Field:Liquid Level]
Calculated Date Fields
WellTraq also supports calculated date fields. You can create a calculated date field by changing the data type of your calculated field to "Calculated Date". Everything else works the same as for a calcualted decimal field, except your field's expression must return a date value for the result to be valid. There are currently four supported date functions that you can use in your expression:
Function | Description | Usage |
CurrentDate | Returns the current date. | CurrentDate() |
DatePart | Takes one date parameter and one time unit parameter. The time unit specifies which part of the date this function will return. | DatePart(date, timeUnit) |
DateAdd | Adds the specified amount of time to the date parameter and returns the result. | DateAdd(date, timeValue, timeUnit) |
DateDiff | Computes the difference between two specified dates and returns the result in the specified unit of time. | DateDiff(timeUnit, startDate, endDate) |
Format | Replaces the format item in a specified string with the string representation of a corresponding object in a specified array. | Format(formatString, args) |
ParseInt | Converts the string representation of a number into its integer equivalent. | ParseInt(string) |
Acronym | Returns the acronym of a group of words using the first alphanumeric character of each word separated by spaces. | Acronym(string) |
Substring | Returns a substring from the given string. The substring starts at the specified character position and has a specified length. | Substring(string, startIndex, length) |
IndexOf | Returns the zero-based index of the first occurrence of the specified string. | IndexOf(string, stringToSearchFor) |
The time unit for the above functions is simply an integer that specifies a unit of time. The values are defined as follows:
- 0, Millisecond
- 1, Second
- 2, Minute
- 3, Hour
- 4, Day
- 5, Month
- 6, Year
Date Examples
This expression will return the number of days that have elapsed since the last inspection.
DateDiff(4, [Field:Inspection Date], CurrentDate())
And here is another expression that will compute when the next inspection should be performed if inspections are performed on a monthly basis.
DateAdd([Field:Inspection Date], 1, 5)
Further Reading
WellTraq's calculated fields are based on the NCalc expression evaluation library. For more information about this library, including a comprehensive list of supported operators and built-in functions, see the NCalc GitHub wiki.
For any further questions regarding calculated fields or for assistance configuring your forms and tasks, please don't hesitate to contact us.