04. Time Series Trend Analysis
1. Overview and Scenario
Situation: CS tickets suddenly exploded, paralyzing the team. The team manager urgently asks:
āIs this temporary, or is it a continuing trend? When did this start?ā
We need to read the āflow of timeā. Not just āhow many todayā, but āhow much more than yesterday (WoW)ā, āwhatās the long-term trend?ā - letās analyze.
2. Data Preparation
Weāll use the cs_tickets_dummy table as our base.
Handling time data (opened_at) is the key.
BigQuery (SQL)
# BigQuery connection setup
from google.cloud import bigquery
client = bigquery.Client()3. Daily/Weekly Trend Analysis
To reduce data noise, we first need to aggregate by daily or weekly.
ā Problem 1: Visualize Daily Ticket Volume
Q. Calculate the daily ticket volume for the last 90 days and visualize with a line graph.
BigQuery (SQL)
Hint: Extract only the date using DATE(opened_at) function and GROUP BY.
View Solution
SELECT
DATE(opened_at) as date,
COUNT(*) as ticket_count
FROM `your-project-id.retail_analytics_us.cs_tickets_dummy`
GROUP BY date
ORDER BY date;4. Moving Average
Daily data is noisy and hard to see trends. Using a 7-day Rolling Average makes patterns much more visible.
ā Problem 2: Calculate 7-Day Moving Average
Q. Calculate the 7-day moving average of daily ticket counts.
BigQuery (SQL)
Hint: Use ROWS BETWEEN 6 PRECEDING AND CURRENT ROW with Window Functions.
View Solution
WITH daily_stats AS (
SELECT DATE(opened_at) as date, COUNT(*) as count
FROM `your-project-id.retail_analytics_us.cs_tickets_dummy`
GROUP BY 1
)
SELECT
date,
count,
ROUND(AVG(count) OVER (
ORDER BY date
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
), 2) as ma_7day
FROM daily_stats
ORDER BY date;5. Week-over-Week / Month-over-Month Change Rate (WoW / MoM)
āHow much did it increase compared to last week?ā is one of the most important questions in business. This is called Week over Week (WoW) or Month over Month (MoM) analysis.
ā Problem 3: Calculate Weekly Change Rate
Q. Calculate weekly ticket counts and the change rate (%) compared to the previous week.
BigQuery (SQL)
Hint: Use LAG(daily_count) OVER (ORDER BY date) function to get the previous weekās value.
View Solution
WITH weekly_stats AS (
SELECT
DATE_TRUNC(opened_at, WEEK(MONDAY)) as week_start,
COUNT(*) as weekly_count
FROM `your-project-id.retail_analytics_us.cs_tickets_dummy`
GROUP BY week_start
)
SELECT
week_start,
weekly_count,
LAG(weekly_count) OVER (ORDER BY week_start) as prev_week_count,
ROUND(
(weekly_count - LAG(weekly_count) OVER (ORDER BY week_start)) * 100.0 /
LAG(weekly_count) OVER (ORDER BY week_start),
2
) as wow_change_pct
FROM weekly_stats
ORDER BY week_start;6. Advanced: Time Series Anomaly Detection
If tickets suddenly tripled? Thatās not just a trend - itās an event (Anomaly). Letās find anomalies with simple rule-based detection.
ā Problem 4: Find Anomalies Using 3-Sigma Rule
Q. Define days when daily ticket count exceeds mean + 2 * standard deviation as āabnormal spike daysā and find them.
BigQuery (SQL)
View Solution
WITH stats AS (
SELECT
DATE(opened_at) as date,
COUNT(*) as count
FROM `your-project-id.retail_analytics_us.cs_tickets_dummy`
GROUP BY 1
),
bounds AS (
SELECT
AVG(count) as avg_val,
STDDEV(count) as std_val
FROM stats
)
SELECT
s.date,
s.count,
CASE
WHEN s.count > (b.avg_val + 2 * b.std_val) THEN 'Anomaly High'
WHEN s.count < (b.avg_val - 2 * b.std_val) THEN 'Anomaly Low'
ELSE 'Normal'
END as status
FROM stats s, bounds b
ORDER BY s.count DESC;š” Summary and Insights
Analyzing the flow of time reveals stories beyond simple numbers.
- Moving Average: Removes noise and shows the āreal trendā.
- Window Functions:
LAG,LEAD,SUM() OVER()etc. are the crown jewels of SQL analysis. - Anomaly Detection: Even simple statistics can detect business crises early.
Next Steps: Weāve analyzed customers and time. But ānot all customers are the sameā. In the next chapter, weāll learn Segmentation - grouping and analyzing customers.