Overview
Formulas in Kodexa allow you to calculate values for data elements based on other data elements in your Data Definition. Instead of extracting a value from the document, you can derive it through mathematical operations, text manipulation, logical conditions, or date calculations.
What is a Formula?
A formula is an expression that calculates a value using:
Data elements - Reference other elements in your Data Definition
Operators - Arithmetic (+, -, *, /), comparison (<, >, =), logical (AND, OR, NOT)
Functions - Built-in functions for text, math, aggregation, and special operations
Constants - Numbers, strings, or arrays
Example: Calculate a line total from quantity and price: {Quantity} * {Price}
Creating a Formula
To use a formula for a data element:
Open your Data Definition
Select or create a data element
Set the Data Source to "Formula"
Navigate to the Semantics tab
Write your formula in the formula editor
Formula Editor Features
Code Editor
The formula editor provides:
Syntax highlighting - Visual differentiation of formula components
Real-time validation - Immediate feedback on syntax errors
Error messages - Precise indication of problems with suggestions
Multi-line support - Write complex formulas across multiple lines
Available Elements Dropdown
Below the editor, a dropdown shows all data elements available for use in your formula:
Hierarchical tree view of your Data Definition
Filterable list to find elements quickly
Click an element to insert it into your formula as
{ElementName}Shows only elements accessible from the current element's scope
Validation and Error Help
When your formula has errors, the editor displays:
Error location - Shows the position of the problem in your formula
Offending symbol - Highlights what caused the error
Function suggestions - If you mistype a function name, suggests the closest match
Clear error messages - Explains what went wrong and why
Formula Format
Data Types
Formulas support these data types:
Number:
123,45.67String:
"Hello, World!"(use double quotes)Attribute Reference:
{ElementName}(reference data elements)Array:
[1, 2, 3]or["apple", "banana"]
Operators
Arithmetic Operators
Operator | Purpose | Example |
+ | Addition |
|
- | Subtraction |
|
* | Multiplication |
|
/ | Division |
|
^ | Exponentiation |
|
Comparison Operators
Operator | Purpose | Example |
< | Less than |
|
<= | Less than or equal |
|
= | Equal to |
|
> | Greater than |
|
>= | Greater than or equal |
|
!= | Not equal |
|
Logical Operators
Operator | Purpose | Example |
&& | AND (both must be true) |
|
|| | OR (either can be true) |
|
! | NOT (opposite) |
|
Grouping
( ) - Group expressions and control order:
(3 + 2) * 4=20& - Concatenate strings:
"Hello" & " " & "World"
Available Functions
Text Functions
Function | Purpose | Example |
uppercase() | Convert to uppercase |
|
lowercase() | Convert to lowercase |
|
concat() | Concatenate strings |
|
isblank() | Check if empty |
|
len() | Get string length |
|
split() | Split string |
|
replace() | Replace part of string |
|
contains() | Check if contains text |
|
startswith() | Check if starts with text |
|
endswith() | Check if ends with text |
|
regex() | Test regex pattern |
|
Mathematical Functions
Function | Purpose | Example |
round() | Round to decimal places |
|
ceil() | Round up |
|
floor() | Round down |
|
abs() | Absolute value |
|
max() | Maximum value |
|
min() | Minimum value |
|
todecimal() | Round with strategy |
|
Rounding strategies for todecimal():
round_up- Always away from zeroround_down- Always toward zeroround_ceil- Toward positive infinityround_floor- Toward negative infinityround_half_up- Nearest neighbor, up on tie (default)round_half_down- Nearest neighbor, down on tieround_half_even- Nearest neighbor, even on tie
Aggregate Functions
Function | Purpose | Example |
sum() | Sum of numbers |
|
multiply() | Product of numbers |
|
average() | Average of numbers |
|
count() | Count items |
|
sumifs() | Sum with condition |
|
countifs() | Count with condition |
|
stddeviation() | Standard deviation |
|
Conditional Functions
Function | Purpose | Example |
if() | Conditional logic |
|
ifnull() | Check if null |
|
Date Functions
Function | Purpose | Example |
isdate() | Check if valid date |
|
isafterdate() | Compare dates |
|
isbeforedate() | Compare dates |
|
datemath() | Date arithmetic |
|
Date Math Function
The datemath() function has two modes:
Mode 1: Date Arithmetic
Calculate dates by adding/subtracting time units:
datemath(date, unit, offset)
Supported units: days, weeks, months, years
Examples:
datemath("2023-04-01", "days", 5)="2023-04-06"datemath("2023-04-01", "weeks", 2)="2023-04-15"datemath("2023-04-01", "months", -2)="2023-02-01"datemath("2023-04-01", "years", 2)="2025-04-01"
Mode 2: Natural Language Parsing
Parse human-readable date expressions:
datemath(expression)
Starting points:
"now"- Current date and time"today"- Start of current day"last Monday","next Tuesday"- Relative days
Time adjustments:
"today + 1 day"- Tomorrow"now - 3 hours"- Three hours ago"last Monday - 2 days"- Two days before last Monday
Supported units: days, hours, minutes, seconds, weeks, months, years
Common Formula Patterns
Calculate Line Total
{Quantity} * {UnitPrice}
Calculate Tax
{Subtotal} * 0.08
Calculate Total with Tax
{Subtotal} + ({Subtotal} * {TaxRate})
Conditional Discount
if({Quantity} >= 10, {Price} * 0.9, {Price})
Full Name from Parts
{FirstName} & " " & {LastName}
Days Until Due
datemath({DueDate}, "days", 0) - datemath("today", "days", 0)
Handle Missing Values
ifnull({Discount}, 0, {Discount})
Age Calculation
if({Age} >= 18, "Adult", "Minor")
Best Practices
Use clear element names -
{UnitPrice}is clearer than{UP}Test incrementally - Build complex formulas step by step
Use parentheses - Make order of operations explicit
Handle nulls - Use
ifnull()to prevent calculation errorsFormat numbers - Use
round()ortodecimal()for consistent decimal placesValidate with test data - Check formula results with sample documents
Comment complex logic - Use descriptive element names as documentation
Tips
Use the Available Elements dropdown to avoid typos in element names
The editor validates formulas in real-time - fix errors before saving
Function suggestions help when you mistype a function name
Formulas calculate after all referenced elements are extracted
Complex formulas can reference other formula-based elements
Use arrays with aggregate functions for calculations across repeating groups
The formula evaluates each time the document is processed
