Skip to Content

04. Time Series Trend Analysis

Intermediate2 hours

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 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.

šŸ’”

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.

šŸ’”

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.

šŸ’”

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.

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.

  1. Moving Average: Removes noise and shows the ā€˜real trend’.
  2. Window Functions: LAG, LEAD, SUM() OVER() etc. are the crown jewels of SQL analysis.
  3. 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.

Last updated on

šŸ¤–AI ėŖØģ˜ė©“ģ ‘ģ‹¤ģ „ģ²˜ėŸ¼ ģ—°ģŠµķ•˜źø°