Ever felt like you're wrestling with a data kraken – a tangled mess of sales figures bouncing all over the place? Fear not, for we have a trusty tool in our arsenal: the simple moving average (SMA). Forget squinting at those wild fluctuations – SMAs are here to smooth out the volatility and reveal the underlying trends hiding beneath the surface.
Think of it like this: imagine you're on a bumpy, data-filled road. Every day, you jot down the sales numbers you encounter. The raw data is great, but it's hard to see the big picture. Enter the SMA. By averaging a specific number of past data points (say, the last 5 days' sales), you create a smoother path, effectively ironing out those daily spikes and dips. This lets you focus on the general direction of travel – is sales trending upwards, downwards, or just taking a scenic detour?
Decoding Simple Moving Averages (SMAs)
In BigQuery, creating SMAs is a breeze:
Aggregate the measure at the daily level
Use window functions to get sma levels
Consider the SQL code below which calculates six different Simple Moving Averages (SMAs) from a timeseries of sales data.
WITH
daily_sales AS (
SELECT DATE(created_at) AS date
,SUM(net_revenue) AS sales
FROM `project_id.schema_id.orders`
GROUP BY date),
simple_moving_average AS (
SELECT date
,sales
,AVG(sales) OVER (ORDER BY date ROWS BETWEEN 5 PRECEDING AND CURRENT ROW) AS sales_sma_5
,AVG(sales) OVER (ORDER BY date ROWS BETWEEN 10 PRECEDING AND CURRENT ROW) AS sales_sma_10
,AVG(sales) OVER (ORDER BY date ROWS BETWEEN 20 PRECEDING AND CURRENT ROW) AS sales_sma_20
,AVG(sales) OVER (ORDER BY date ROWS BETWEEN 50 PRECEDING AND CURRENT ROW) AS sales_sma_50
,AVG(sales) OVER (ORDER BY date ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) AS sales_sma_100
,AVG(sales) OVER (ORDER BY date ROWS BETWEEN 200 PRECEDING AND CURRENT ROW) AS sales_sma_200
FROM daily_sales)
SELECT *
FROM simple_moving_average
ORDER BY date
The simple_moving_average
CTE calculates six different moving averages (sales_sma_5
, sales_sma_10
, sales_sma_20
, etc) using the AVG()
function along with the OVER
clause. The OVER
clause defines the window over which the average is calculated. In this case, it's a moving window specified by ROWS BETWEEN ... AND ...
:
ROWS BETWEEN 5 PRECEDING AND CURRENT ROW
: Calculates the average of the sales values for the current row and the five preceding rows.ROWS BETWEEN 10 PRECEDING AND CURRENT ROW
: Calculates the average of the sales values for the current row and the ten preceding rows, etc.
Plotting the SMAs
Now, the real power of SMAs shines through. By plotting your sales data alongside the different moving averages, you can visually identify trends that might have been lost in the data chaos. Perhaps a 5-day SMA highlights short-term fluctuations, while a 20-day SMA reveals a longer-term upward trajectory. This ability to play with window sizes allows you to zoom in and out on the trend, giving you a well-rounded understanding of your sales behaviour.
To illustrate how SMAs can be used to unveil trends, I've created a sample dashboard on Tableau Public where you can dynamically switch between SMAs: Unveiling Trends with Simple Moving Averages.
So, the next time you're drowning in a sea of daily data, remember the calming power of SMAs. With BigQuery & Tableau as your trusty sidekick, you can transform that data kraken into a smooth, informative graph, ready to reveal the trends that will propel your business decisions forward.