add_moving_average
Add a new column to the data extract with moving averages.
This guide explains how to configure the add_moving_average instruction. To learn about another instruction, go back to the Available custom script instructions overview.
Introduction
Use the add_moving_average instruction to add a new field to the data extract which calculates the moving average for a specific number of values. Moving averages are calculated with the formula (value_in_current_row + values_in_previous_N_rows + values_in_next_N_rows) / (2 * N + 1)
where N
is the number you specify in the Window Size field. The first and last N rows in the new field are left empty.
Creating a custom script transformation using the add_moving_average instruction
To create and configure a custom script using the add_moving_average instruction, follow these steps:
-
In the Instructions step, select the add_moving_average instruction.
-
To configure the custom script instruction, fill in the following fields. Required fields are marked with an asterisk (*).
-
Field*
-
Specify the field which contains the original values.
-
Window size*
-
Specify the number of values before and after the current row to use for calculating moving averages.
-
New field name*
-
Specify the name of the new column to add to the data extract.
-
Subtable
-
Enter the name for a subtable that you want to use within this custom script.
A subtable is a temporary table that only exists for this custom script. You can apply additional instructions within the same custom script to the subtable. However, the subtable cannot be used in any other custom scripts.
If a subtable does not exist for the current custom script, the transformation is applied to the data extract, and the enriched data is output into the subtable. If the subtable already exists for the custom script, the subtable is used as the input for the transformation and optionally as the output.
Example
Transformation configuration
-
Field
-
Clicks
-
Window Size
-
1
-
New Fieldname
-
Clicks_Moving_Average
Data table before transformation
Date |
Clicks |
---|---|
2021-11-01 |
7 |
2021-11-02 |
3 |
2021-11-03 |
18 |
2021-11-04 |
9 |
Data table after transformation
Date |
Clicks |
Clicks_Moving_Average |
---|---|---|
2021-11-01 |
7 |
|
2021-11-02 |
3 |
14 |
2021-11-03 |
18 |
15 |
2021-11-04 |
9 |
|