Interquartile Range (IQR)
IQR analysis reveals which deliveries behave typically and which stray far beyond normal variation.
Calculate the first and third quartiles, derive the interquartile range, and place fences at 1.5 * IQR below Q1 and above Q3. Any observations beyond those limits are flagged for review because they lie outside the central 50 percent of behaviour.
Image source: generate_visuals.py
Code examples
Implementing the IQR rule
import pandas as pd
# Sample weekly sales totals for each store
sales = pd.Series([12, 14, 15, 16, 18, 19, 20, 22, 24, 45])
# Calculate the first (Q1) and third (Q3) quartiles
q1 = sales.quantile(0.25)
q3 = sales.quantile(0.75)
# Interquartile range measures the middle spread
iqr = q3 - q1
# Anything outside the fences is treated as an outlier
lower_fence = q1 - 1.5 * iqr
upper_fence = q3 + 1.5 * iqr
# Filter the series to only show outliers
outliers = sales[(sales < lower_fence) | (sales > upper_fence)]
print(outliers)
' Calculate quartiles for the sales range
=QUARTILE.INC($A$2:$A$11,1)
=QUARTILE.INC($A$2:$A$11,3)
' Derive the interquartile range and fences
=Q3 - Q1
=Q1 - 1.5*IQR
=Q3 + 1.5*IQR
' Flag observations outside the fences
=IF(OR(A2<Lower_Fence,A2>Upper_Fence),"Outlier","OK")
// Quartiles based on average weekly sales per store
IQR Q1 =
PERCENTILEX.INC(
VALUES('Sales'[Store]),
CALCULATE(AVERAGE('Sales'[Weekly Sales])),
0.25
)
IQR Q3 =
PERCENTILEX.INC(
VALUES('Sales'[Store]),
CALCULATE(AVERAGE('Sales'[Weekly Sales])),
0.75
)
// Spread between the quartiles
IQR Range = [IQR Q3] - [IQR Q1]
// Upper and lower fences for the rule
IQR Lower = [IQR Q1] - 1.5 * [IQR Range]
IQR Upper = [IQR Q3] + 1.5 * [IQR Range]
// Return an informative flag for reporting
Outlier Flag =
IF(
AVERAGE('Sales'[Weekly Sales]) < [IQR Lower]
|| AVERAGE('Sales'[Weekly Sales]) > [IQR Upper],
"Outlier",
"Within IQR"
)
Key takeaways
- Box plots condense thousands of deliveries into shapes that reveal inconsistent depots fast.
- Outlier markers flag exceptional runs without shaming normal operational variance.
- Operational context—like ferry crossings—explains wide boxes before performance judgements.