running_total
This is part of our beta expression language AQL. Learn more. Request beta.
Definition
Modify a measure into a running measure that accumulates values over one or multiple dimensions as it progresses. Note that the running total will only be calculated on the data that is visible in the exploration.
To calculate a running total of all data, please use the running_total! function instead. Here is a comparison between running_total
and running_total!
:
Syntax
running_total(measure)
running_total(measure, running_dimension, ...)
running_total(orders.total_orders, orders.created_at)
// with pipe
orders.total_orders | running_total(orders.created_at | year())
// with multiple running dimensions
running_total(orders.total_orders, orders.created_at, orders.status)
Input
measure
: The measure that you want to turn into a running measure.running_dimension
(optional, repeatable): The dimension you want your aggregation to run along. If not specified, the returned measure will run along all dimensions that in your exploration
Output
New measure that runs along the specified dimension(s)
Sample Usages
In the following examples, let’s assume that we have an orders
model with the following fields
Model orders {
dimension id {}
dimension created_at {}
measure total_orders {
definition: @aql count(orders.id);;
}
}
Simple running sum
Below is an example of how to define a running total measure to calculate cumulative orders on a monthly basis
orders.total_orders | running_total(orders.created_at)
Running dimension is not included in exploration
If the specified running dimension is not included in the exploration, the returned value will be the same as the original measure before applying the running_total()
function
orders.total_orders | running_total(orders.created_at)
Running dimension is coarser than grouping dimensions
You have a running total metric that runs along a time dimension in year
grain as below
orders.total_orders | running_total(orders.created_at | year())
If the dimension included in the exploration has a finer granularity than the specified running dimension, the value will be repeated for any record that shares the same coarser granularity
In this case, the running total will still be calculated for the whole year, and the yearly value will be repeated for every month existing in the data: