Building an AI‑Powered Anomaly‑Detection Agent for the Golden State Warriors Basketball Ticket Sales - Part 1
Build an agent with ChatGPT and Power BI
Building an AI‑Powered Anomaly‑Detection Agent for Warriors Ticket Sales
Quick take: In a single afternoon you can stand up an autonomous agent that ingests Warriors ticket‑sales data (sense), spots abnormal demand patterns (think), and highlights them in Power BI (act)—all driven by ChatGPT‑generated code. This post walks you through the exact prompts, Python, and Power BI clicks you need, plus why each step matters.
Why this is an AI Agent
An AI agent is software that autonomously perceives its environment, reasons over data, and acts toward a goal with minimal human oversight. That cycle is often described as “sense → think → act.” Our goal‑oriented loop is as follows:
Loop Stage: What Happens in This Tutorial
Sense: Load and clean daily ticket‑sales data
Think: Let ChatGPT create anomaly‑detection logic
Act: Flag outliers, visualize them, and (in Part 2 of this series) trigger reports
Why roll your own? Power BI’s native Anomaly Detection is great for quick insights, but a custom agent lets you set your own thresholds, embed narratives, and schedule downstream actions. This is flexibility a stock visual in Power BI cannot match.
Prerequisites & One‑Time Setup
Enable Python in Power BI Desktop: File → Options and Settings → Options → Python Scripting. Point to a local Python 3.x install.
Optional: If you will refresh in the service, plan to use a gateway or Microsoft Fabric Notebook.
Dataset You Can Reproduce
Grab the sample CSV below for 5 days of mock data. It is realistic with ticket data built on NBA attendance norms for transparency. The schema:
Date,TicketType,SeatCategory,UnitsSold,AvgPrice,TotalRevenue
(For speed, the five‑row excerpt below is enough to follow along.)
2025-04-01,Single,Lower,1500,120,180000
2025-04-02,Single,Upper,2300,75,172500
2025-04-03,Package,Lower,300,200,60000
2025-04-04,Single,Lower,1450,118,171100
2025-04-05,Single,Upper,2400,78,187200
If you want real data for experimentation, the Kaggle NBA Database (https://www.kaggle.com/datasets/wyattowalsh/basketball) includes attendance and game info you can join with public ticket‑price feeds.
Step 1 – Sense: Data Preparation
ChatGPT prompt
User → ChatGPT
“Write pandas code that:
• loadswarriors_sales.csv
;
• parsesDate
;
• sorts chronologically;
• drops rows with null numeric fields;
• addsRevenuePerUnit = TotalRevenue/UnitsSold
;
• computes a 7‑day moving average ofUnitsSold
calledMA_7
.
Return only the Python script.”
LLM‑generated script (copy/paste into Get Data → Python Script)
import pandas as pd
df = pd.read_csv("warriors_sales.csv", parse_dates=["Date"])
df = df.sort_values("Date").dropna(subset=["UnitsSold", "TotalRevenue", "AvgPrice"])
df["RevenuePerUnit"] = df["TotalRevenue"] / df["UnitsSold"]
df["MA_7"] = df["UnitsSold"].rolling(window=7).mean()
Outcome: The agent now perceives clean, enriched data ready for reasoning.
Step 2 – Think: Detect Anomalies
Why a 20 % threshold?
A ±20 % swing around a 7‑day moving average is a practical starting point for ticket sales, balancing sensitivity and noise. Statistical guides often begin with a 2σ (~5 %) or 3σ (~0.3 %) z‑score; revenue data tends to be noisier, so ±20 % is a middle‑ground industry heuristic.
ChatGPT prompt
“Add to the previous DataFrame an
Anomaly
column that isTrue
when the absolute percent difference betweenUnitsSold
andMA_7
exceeds 0.20. Return the code.”
LLM‑generated code
df["Anomaly"] = ((df["UnitsSold"] - df["MA_7"]).abs() / df["MA_7"]) > 0.20
anomalies = df[df["Anomaly"]]
Paste this extension into the same Python script step or a subsequent one. Your agent now reasons and flags outliers.
Step 3 – Act: Visualize in Power BI
Bring fields into a Line Chart
X‑axis:
Date
Y‑axis:
UnitsSold
Conditional format the
UnitsSold
line byAnomaly
(red forTrue
).Add
MA_7
as a second line for baseline context.(Optional) Insert a Card visual showing Total anomalies (last 30 days).
Color cues focus the reader’s eye on exceptions, a best practice for attention‑directing charts.
(Add Screenshot Placeholder: “Figure 1 — Anomalies highlighted in red on a line chart”)
Proof of Autonomy: The Continuous Loop
Once loaded into a scheduled dataset, the agent will re‑run every refresh—re‑sensing new rows, re‑thinking the threshold, and re‑acting in visuals—no extra clicks required. That is the hallmark of an autonomous AI agent.
What is coming next week — Part 2
Part 2 teaches the agent to explain itself:
ChatGPT‑generated natural‑language tooltips ("UnitsSold dropped 31 % on Apr 06 due to a mid‑week game").
Scheduled PDF dashboards emailed via Power Automate cloud flows.
Recap
Custom autonomy: Flexible thresholds and narratives beyond stock Power BI anomaly visuals.
Agentic speed: Data→Insight in minutes, not days.
Foundation for scale: The same loop can watch merchandise sales or concession revenue with a two‑line prompt tweak.
Your turn: Change the dataset, swap in your own columns, and watch your AI Anomaly‑Detection Agent surface the stories hiding in your numbers.