series_lag_fl()
Applies a lag on a series.
The function series_lag_fl() is a user-defined function (UDF) that takes an expression containing a dynamic numerical array as input and shifts it backward. It’s commonly used for shifting time series to test whether a pattern is new or it matches historical data.
Syntax
series_lag_fl(y_series, offset)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| y_series | dynamic | ✔️ | An array cell of numeric values. |
| offset | int | ✔️ | An integer specifying the required offset in bins. |
Function definition
You can define the function by either embedding its code as a query-defined function, or creating it as a stored function in your database, as follows:
Query-defined
Define the function using the following let statement. No permissions are required.
let series_lag_fl = (series:dynamic, offset:int)
{
let lag_f = toscalar(range x from 1 to offset+1 step 1
| project y=iff(x == offset+1, 1, 0)
| summarize lag_filter = make_list(y));
fir(series, lag_f, false)
};
// Write your query to use the function here.
Stored
Define the stored function once using the following .create function. Database User permissions are required.
.create-or-alter function with (folder = "Packages\\Series", docstring = "Shift a series by a specified offset")
series_lag_fl(series:dynamic, offset:int)
{
let lag_f = toscalar(range x from 1 to offset+1 step 1
| project y=iff(x == offset+1, 1, 0)
| summarize lag_filter = make_list(y));
fir(series, lag_f, false)
}
Example
Query-defined
To use a query-defined function, invoke it after the embedded function definition.
let series_lag_fl = (series:dynamic, offset:int)
{
let lag_f = toscalar(range x from 1 to offset+1 step 1
| project y=iff(x == offset+1, 1, 0)
| summarize lag_filter = make_list(y));
fir(series, lag_f, false)
};
let dt = 1h;
let time_shift = 1d;
let bins_shift = toint(time_shift/dt);
demo_make_series1
| make-series num=count() on TimeStamp step dt by OsVer
| extend num_shifted=series_lag_fl(num, bins_shift)
| render timechart
Stored
let dt = 1h;
let time_shift = 1d;
let bins_shift = toint(time_shift/dt);
demo_make_series1
| make-series num=count() on TimeStamp step dt by OsVer
| extend num_shifted=series_lag_fl(num, bins_shift)
| render timechart
Output

Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.