Rolling windows, or lookbacks, calculate metrics like rolling averages using the current row combined with N previous rows. This can be useful for smoothing out volatile time series data to better understand long term trends.
first_column_I_want_to_sum
is the column with the values you want to add up (sometimes this is an average)column_I_want_to_order_by
is the column you want to order by when defining your lookback windowN
is the number of previous rows you want to include in your calculation (don’t forget the current row is included, so for a four week lookback window, we’ll use N=3)second_column_I_want_to_sum
is optional, another column with values you want to add up to compare to your first column, like when calculating averages (which is how we use it in this example)N
.