Editor

Month-over-Month Growth

Intermediate · Write the code · from Build a Sales Performance Dashboard with KPI Analysis

From the lesson: Step 5: Calculate Month-over-Month Growth

Trends matter more than snapshots. A $50,000 month sounds great, but if last month was $80,000, there's a problem. Month-over-month growth shows whether the business is improving or declining.

Read this part of the lesson

Write a function monthly_growth(df) that:

1. Groups the DataFrame by the month column

2. Calculates total revenue per month

3. Calculates the percentage change from one month to the next

4. Prints each month's revenue and growth rate

Format: MONTH: $REVENUE (GROWTH%) where growth shows +/- sign.

For the first month (no previous month), print: MONTH: $REVENUE

The months should appear in order: January, February, March.

import pandas as pd
def monthly_growth(df):
# Your code here
pass
data = {
'date': ['2024-01-05', '2024-01-20', '2024-02-10', '2024-02-25',
'2024-03-05', '2024-03-15', '2024-03-25'],
'product': ['A', 'B', 'A', 'B', 'A', 'B', 'A'],
'quantity': [2, 3, 4, 2, 5, 3, 2],
'unit_price': [100, 200, 100, 200, 100, 200, 100]
}
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'])
df['revenue'] = df['quantity'] * df['unit_price']
df['month'] = df['date'].dt.strftime('%B')
monthly_growth(df)

More Sales Report exercises

All Python exercises · 50 practice problems · Practical Projects quiz