Time Aggregations
When consuming entities and their features, we can tell Lynk on which time frame to aggregate the features, by using the time_agg property in the USE config block.
Examples for time aggregations:
- Calendric: day / month / year etc 
- Rolling windows : "last 30 days" / "first 7 days after signup" etc 
- No time frame (full date range) 
For example, let's assume we have an entity customer and it has a simple metric feature count_orders that counts how many orders a customer has.
-- SQL API
SELECT  customer_id,
        count_orders
FROM    entity('customer')
The above SQL API request will calculate for each customer the count of total orders on all available time range in the underlying data asset - in this case orders (where the measure count_orders comes from).
If we would like to get the daily amount of count_orders per customer, we can change the query as follows:
-- SQL API
USE {
  "time_agg" {
    "time_grain" : "day"
    }
}
SELECT  customer_id,
        time_agg as date_day,
        count_orders
FROM    entity('customer')
In the above example, a record per customer and day will return, counting the amount of orders per customer and day.
Using time_agg
time_aggUSE
USESet this object on the SQL API / REST API query, to define all the query level configurations, including time_agg. See USE for in depth information on this.
time_agg
time_aggtime_agg is an object that holds all the options on how to apply time aggregation to our query.
The options for time_agg are:
- time_grain
- window_size
- direction
time_grain [optional]
time_grain [optional]Optional, defaults to day.
A time grain refers to the level of granularity at which time is divided in the result of the API query. It determines how entity-level aggregations or calculations (e.g., sums, averages, counts) are grouped.
For example, if the time grain is day and the main entity is customer, the returned result will be on a customer + day level.
The supported values for time_grain are:
- year 
- quarter 
- month 
- week 
- day 
- hour 
- minute 
For example:
-- SQL API
USE {
  "time_agg" {
    "time_grain" : "week"
    }
}
SELECT  customer_id,
        time_agg as date_week,
        count_orders
FROM    entity('customer')The above example returns the result of the feature count_orders for each customer and week.
window_size [optional]
window_size [optional]Optional, defaults to 1.
Window size is for aggregating rolling windows - this property will determine the window size of the rolling window. Note that the time grain will be taken into account here for determining the "size" of the window as well.
The supported values for window_size are:
- integer values 
- unbounded
window_size example, using an integer value:
-- SQL API
USE {
  "time_agg" {
    "time_grain" : "month",
    "window_size": 1,
    "direction": "backward"
    }
}
SELECT  customer_id,
        time_agg as date_month,
        count_orders
FROM    entity('customer')The above example returns the cumulative results of the feature count_orders in the last 3 months, for each customer and month.
window_size example, using unbounded:
-- SQL API
USE {
  "time_agg" {
    "time_grain" : "month",
    "window_size": "unbounded",
    "direction": "backward"
    }
}
SELECT  customer_id,
        time_agg as date_month,
        count_orders
FROM    entity('customer')The above example returns for each customer and month, the cumulative results of count_orders up until that hour. The window of time in this case starts from the first order of each customer that matches the business logic of the metric count_orders.
If we would choose direction: forward, the returned results would be the cumulating the feature count_orders for each data point (customer and month) up until the last known order in the underlying data asset.
direction [optional]
direction [optional]Optional, defaults to backward.
Determines the direction of the rolling window.
The supported values for direction are:
- backward
- forward
For example:
-- SQL API
USE {
  "time_agg" {
    "time_grain" : "day",
    "window_size": 7,
    "direction": "forward"
    }
}
SELECT  customer_id,
        time_agg as date_day,
        count_orders
FROM    entity('customer')The above example returns the result of the feature count_orders in the next 7 days for each customer and day.
How time aggregations work
Once the time_agg option is passed to the query through the USE config, Lynk applies time aggregation to all the features on the query.
For example
-- SQL API
USE {
  "time_agg" {
    "time_grain" : "day",
    "window_size": 7,
    "direction": "backward"
    }
  "start_time" = "2025-01-01",
  "stop_time" = "2025-02-01"
}
SELECT  customer_id,
        time_agg as date_day,
        first_order_date,
        count_orders
FROM    entity('customer')Using time_field
time_fieldThe aggregation is applied according to the specified time_field, as follows:
- Using the feature level - time_fieldproperty.
- If no - time_fieldis specified on the feature level, Lynk will use the default- time_fieldon the data asset level.
- For features on which time_field was not specified on either feature nor asset level, Lynk will not apply any time aggregation logic. 
Last updated
