Creating a SQL Custom Expression
When creating a billable metric, you can choose between using a simple aggregation field or a custom expression. Custom expressions allow you to define more advanced computation logic, which is useful when the required aggregation involves complex calculations that Lago should handle.To add a custom expression through the user interface:
- Define the
name
andcode
for the billable metric; - Choose an aggregation type;
- Select
Custom expressions
as the aggregation option instead ofUnique field
; - Enter your custom expression in the expanded side panel;
- Test the custom expression; and
- Save the billable metric.

Create a custom expression for a billable metric
Custom expressions can be used with any aggregation type except
COUNT
.Supported Expressions
A variety of SQL custom expressions are available for use. Here are a few examples:- Concatenation:
CONCAT(event.properties.user_id, '-', event.properties.app_id)
- Math operations:
(event.properties.cpu_number * 25 * event.properties.duration_msec) + (event.properties.memory_mb * 0.000001 * event.properties.duration_msec)
- Rounding:
ROUND(event.properties.duration_msec * 1000)
Event attributes
Lago expressions may include the following event attributes:event.code
(event code)event.timestamp
(event timestamp)event.properties.[property_name]
(event property)
event.properties.my_property
is a valid event attribute.
Atoms
Lago expressions may include atoms:123
(integer)123.45
(decimal)'Hello, world!'
(string)
Operators
Lago expressions may include basic operators:+
(addition)-
(subtraction)*
(multiplication)/
(division)- unary minus (
-12
)
Functions
CONCAT
The CONCAT
function is used to concatenate two or more strings.
str1
: The first string to concatenatestr2
: The second string to concatenatestrs
: (Optional) Additional strings to concatenate.
CONCAT(event.properties.user_id, '-', event.properties.app_id)
ROUND
The ROUND
function is used to round a number to a specified number of decimal places.
value
: The number to roundprecision
: (Optional) Number of decimal places. Defaults to0
.
ROUND(14.2355)
returns14
ROUND(14.2355, 0)
returns14
ROUND(14.2355, 2)
returns14.24
ROUND(14.2355, -1)
returns10
FLOOR
The FLOOR
function is used to round a number down to the nearest integer.
value
: The number to roundprecision
: (Optional) Number of decimal places. Defaults to0
.
FLOOR(16.2365)
returns16
FLOOR(16.2365, 0)
returns16
FLOOR(16.2365, 2)
returns16.23
FLOOR(16.2365, -1)
returns10
CEIL
The CEIL
function is used to round a number up to the nearest integer.
value
: The number to roundprecision
: (Optional) Number of decimal places. Defaults to0
.
CEIL(14.2345)
returns15
CEIL(14.2345, 0)
returns15
CEIL(14.2345, 2)
returns14.24
CEIL(14.2345, -1)
returns20
Testing your SQL Custom Expression
Lago provides a testing tool to help you validate the custom expressions you’ve created. A sample event is used to test your expression. You can override any field in the test event. If your custom expression is incorrect, Lago will return an error, and you won’t be able to save it until it’s valid. Keep the following in mind:- You can dynamically reference any event field within your expression; and
- Use precise paths to reference fields accurately. For instance, to use the event timestamp, the path is
event.timestamp
. To reference a custom property sent with the event, the path isevent.properties.your_field
.

Test your custom expression