liveBook · Manning

Date range comparison

As explained in Build Python Web Apps with Streamlit

A comparison date range is a date interval selected or calculated as the reference period for comparing a metric with its current-period value.

A date range comparison gives a metric context. Instead of showing only current-period sales, a dashboard compares them with a selected reference period so users can judge whether performance improved or declined. section 7.4

Build the comparison in three practical steps:

  • Let users choose a comparison type such as MoM, QoQ, YoY, or Previous period with a select box. section 7.4.1
  • Calculate the reference dates by subtracting one month, three months, one year, or the main range length from both endpoints. section 7.4.1
  • Filter a second dataset using the same non-date filters, calculate the metric again, and display its percentage change beside the main value. section 7.4.2

August compared with July

For a main range of August 1–31, 2024 and a comparison range of July 1–31, 2024, calculate sales separately. If August sales are $5M and July sales are $4M, show August sales as up 20% from the comparison period. section 7.4.2

Figure 7.7 Sales for RoyalQuill were $1.32M, but there’s no indication of whether that’s good or what the sales were in a previous comparable period.

Use the dashboard screenshot to identify the problem a comparison solves: a sales total alone does not indicate whether performance is good or bad. section 7.4

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines date-offset calculation, consistent filtering, percentage-delta handling, and Streamlit display without extending beyond dashboard implementation.
Examples
Uses MoM, QoQ, YoY, Previous period, and an August-versus-July sales comparison with a displayed percentage change.
What it assumes
Assumes an existing dashboard with selectable date ranges, filtered data, and metric calculations.
What you can do after
Apply the technique in Streamlit by calculating a second filtered dataframe and displaying its percentage delta beside each main metric.
How it connects
Connects date ranges with dashboards, metrics, filters, pandas dataframes, and Streamlit metric displays.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.