Series functions
Series functions compare each row with the rows around it, such as the change in daily event counts. Use them to calculate running totals, moving averages, and period-over-period changes. Before you start, review the NetScript language reference for formula syntax.
A series is a set of rows in a fixed order. Declare a series with series along, then apply a series function to it.
series along (COLLATION)
Use the series along operation to put your data in order, so series functions know which row comes before another. The collation is the property that sets the order, such as a date. The partition groups rows into separate series, such as one series for each user. Analytics orders the rows within each partition.
The series along operation takes the following parts:
- along (COLLATION_1, COLLATION_2) – (Required) Sets the order. Enter at least one property. Analytics uses COLLATION_2 and later properties only to break ties.
- desc – (Optional) Sorts a collation property in descending order, such as along (day desc). The default order is ascending. The desc keyword is valid only inside series along.
- foreach (PARTITION_1, PARTITION_2) – (Optional) Sets the partition. Later slices do not change a partition that you set with foreach, and the input to the series becomes fixed.
When you omit foreach, all rows start in one partition. Later slices with by add their properties to the partition.
Function
INPUT | series along (COLLATION_1, COLLATION_2)
INPUT | series along (COLLATION_1 desc)
INPUT | series along (COLLATION_1) foreach (PARTITION_1, PARTITION_2)Data type – input type
Example
Events | count by (day) | series along (day)
Events.Page | series along (time) foreach (Events.UserID)Result
The daily event counts, ordered by day
The pages that each user viewed, ordered by time for each userlag(INPUT)
Use the lag function to get the input value from the previous row in the partition. Use it to compare each value with the one before it, such as the change in daily event counts. For the first row, the result is null. Ensure your input is a series.
To look further back, add an integer offset as the second argument. The default offset is 1. For example, lag(x, 7) returns the value from seven rows earlier.
Function
lag(INPUT)
lag(INPUT, OFFSET)Data type – input type
Example
x = Events | count by (day) | series along (day);
x - lag(x)Result
If x is 1,2,3: result is null,1,1lead(INPUT)
Use the lead function to get the input value from the next row in the partition. Use it to compare each value with the one after it. For the last row, the result is null. Ensure your input is a series.
To look further ahead, add an integer offset as the second argument. The default offset is 1. The shift function is an alias of lead and works the same way.
Function
lead(INPUT)
lead(INPUT, OFFSET)Data type – input type
Example
x = Events | count by (day) | series along (day);
lead(x) - xResult
If x is 1,2,3: result is 1,1,nullrank(INPUT)
Use the rank function to number each row within its partition in collation order, starting at 1. Each row gets a unique rank, and Analytics breaks ties in an arbitrary order. Ensure your input is a series.
Function
rank(INPUT)Data type – number
Example
x = Events | count by (day) | series along (day);
rank(x)Result
1,2,3point_index(INPUT)
Use the point_index function to number each row within its partition, in the order of the series. The point_index function is an alias of the rank function and returns the same result. Ensure your input is a series.
Function
point_index(INPUT)Data type – number
Example
x = Events | count by (day) | series along (day);
point_index(x)Result
1,2,3collect
Use the collect operation to aggregate a series within each partition, such as the last page each user viewed. Follow collect with an aggregate function, such as sum, or with one of the following options:
- collect first – Returns the value at the first position in the series order.
- collect last – Returns the value at the last position in the series order.
The first and last options skip rows with a null collation value when a row with a known value exists. The first and last aggregations are valid only after collect. Ensure your input is a series.
Function
INPUT | collect first
INPUT | collect last
INPUT | collect AGGREGATE_FUNCTIONData type – input type
Example
Events.Page | series along (time) foreach (Events.UserID) | collect lastResult
The last page each user viewedcumulative_count(INPUT)
Use the cumulative_count function to count rows from the start of the partition through the current row. The function counts only rows where the input is not null. Ensure your input is a series.
Function
cumulative_count(INPUT)Data type – number
Example
x = Events | count by (day) | series along (day);
cumulative_count(x)Result
If x is 1,2,3: result is 1,2,3cumulative_sum(INPUT)
Use the cumulative_sum function to calculate a running total, such as the total number of events to date. For each row, the function adds the input values from the start of the partition through the current row. The input must be a number. Ensure your input is a series.
Function
cumulative_sum(INPUT)Data type – number
Example
x = Events | count by (day) | series along (day);
cumulative_sum(x)Result
If x is 1,2,3: result is 1,3,6cumulative_avg(INPUT)
Use the cumulative_avg function to calculate a running average, such as the average daily event count to date. For each row, the function averages the input values from the start of the partition through the current row. The input must be a number. Ensure your input is a series.
Function
cumulative_avg(INPUT)Data type – number
Example
x = Events | count by (day) | series along (day);
cumulative_avg(x)Result
If x is 1,2,3: result is 1,1.5,2cumulative_stddev_pop(INPUT)
Use the cumulative_stddev_pop function to calculate a running population standard deviation. For each row, the function covers the input values from the start of the partition through the current row. The input must be a number. Ensure your input is a series.
Function
cumulative_stddev_pop(INPUT)Data type – number
Example
x = Events | count by (day) | series along (day);
cumulative_stddev_pop(x)Result
If x is 1,2,3: result is 0,0.5,0.82cumulative_corr(INPUT_1, INPUT_2)
Use the cumulative_corr function to calculate a running correlation between two numeric series. For each row, the function covers the values from the start of the partition through the current row. Ensure both inputs are series.
Function
cumulative_corr(INPUT_1, INPUT_2)Data type – number
Example
x = Orders | count by (day) | series along (day);
y = Orders.Amount | sum by (day) | series along (day);
cumulative_corr(x, y)Result
For each day: the correlation between order count and total amount up to that dayrolling_sum(INPUT, ROWS_BEFORE, ROWS_AFTER)
Use the rolling_sum function to add up values in a sliding window of rows, such as a seven-day total. The window covers the current row, plus ROWS_BEFORE rows before it and ROWS_AFTER rows after it. Enter each row count as a whole number, such as 6 or 0. The input must be a number. Ensure your input is a series.
Function
rolling_sum(INPUT, ROWS_BEFORE, ROWS_AFTER)Data type – number
Example
x = Events | count by (day) | series along (day);
rolling_sum(x, 1, 0)Result
If x is 1,2,3: result is 1,3,5rolling_avg(INPUT, ROWS_BEFORE, ROWS_AFTER)
Use the rolling_avg function to calculate a moving average over a sliding window of rows, such as a seven-day average. The window covers the current row, plus ROWS_BEFORE rows before it and ROWS_AFTER rows after it. Enter each row count as a whole number, such as 6 or 0. The input must be a number. Ensure your input is a series.
Function
rolling_avg(INPUT, ROWS_BEFORE, ROWS_AFTER)Data type – number
Example
x = Events | count by (day) | series along (day);
rolling_avg(x, 1, 0)Result
If x is 1,2,3: result is 1,1.5,2.5first_where(INPUT, CONDITION)
Use the first_where function to find the input value at the first earlier row where a condition is true. For example, find the event count on the first day with more than 100 events. The function checks only rows before the current row and returns null when no row matches. Ensure your input is a series.
Function
first_where(INPUT, CONDITION)
INPUT | first_where (CONDITION)Data type – input type
Example
x = Events | count by (day) | series along (day);
first_where(x, x > 100)Result
For each day: the event count on the first previous day with more than 100 eventslast_where(INPUT, CONDITION)
Use the last_where function to find the input value at the most recent earlier row where a condition is true. For example, find the event count on the last day with more than 100 events. The function checks only rows before the current row and returns null when no row matches. Ensure your input is a series.
Function
last_where(INPUT, CONDITION)
INPUT | last_where (CONDITION)Data type – input type
Example
x = Events | count by (day) | series along (day);
last_where(x, x > 100)Result
For each day: the event count on the most recent previous day with more than 100 eventsnext_where(INPUT, CONDITION)
Use the next_where function to find the input value at the next later row where a condition is true. For example, find the event count on the next day with more than 100 events. The function checks only rows after the current row and returns null when no row matches. Ensure your input is a series.
Function
next_where(INPUT, CONDITION)
INPUT | next_where (CONDITION)Data type – input type
Example
x = Events | count by (day) | series along (day);
next_where(x, x > 100)Result
For each day: the event count on the next day with more than 100 events