Handling Quarter-Over-Quarter Calculations in Tableau

Check out the dashboard here.

The Problem: Selecting Q1 and Getting an Undefined Difference

Quarter-over-quarter comparisons are crucial for understanding business trends, but they can sometimes present challenges, especially when dealing with the first quarter (Q1) of a timeline. When users interact with a parameter to select a specific quarter in your Tableau dashboard, they expect to see key metrics like profit differences compared to the previous quarter. However, if Q1 is selected, there is no "Q0" to compare against, resulting in null or nonsensical values. This can create confusion for users who might see an empty or misleading value when trying to understand changes from one period to the next.

The Solution: Dynamic Calculated Fields

To solve this, we need to create a calculated field that dynamically handles the situation when Q1 is selected. 

Create a Quarter Selector Parameter

First, create a parameter to allow the user to select a quarter. This will drive the display and calculations:

  • Name: Quarter Selector
  • Data Type: Integer
  • Values: 1, 2, 3, 4 (representing Q1, Q2, Q3, Q4)

This parameter will give users the ability to choose which quarter they want to analyze.

Calculating the Percent Difference Between Quarters (QoQ)

Here's how you can create a calculated field to show the percent difference between the current and previous quarter:

Calculated Field: Difference in Profit

(SUM(IF DATEPART('quarter', [Order Date]) = [Quarter Selector] THEN [Profit] END) 

- SUM(IF DATEPART('quarter', [Order Date]) = ([Quarter Selector] - 1) THEN [Profit] END)) 

/ SUM(IF DATEPART('quarter', [Order Date]) = ([Quarter Selector] - 1) THEN [Profit] END)

Explanation:

  • SUM(IF DATEPART('quarter', [Order Date]) = [Quarter Selector] THEN [Profit] END): This part calculates the total profit for the selected quarter.
  • SUM(IF DATEPART('quarter', [Order Date]) = ([Quarter Selector] - 1) THEN [Profit] END): This part calculates the total profit for the previous quarter.
  • (...) / SUM(IF DATEPART('quarter', [Order Date]) = ([Quarter Selector] - 1) THEN [Profit] END): The difference between the current quarter's profit and the previous quarter's profit is divided by the previous quarter's profit to get the percentage change.
  • Handling Q1: If Q1 is selected, the calculation for the previous quarter ([Quarter Selector] - 1) results in 0, which means there is no valid previous quarter to compare to, and the calculation will return NULL. This avoids showing misleading values for Q1.

Write a Calculated Field to Show Previous Quarter

Next, create a calculated field that determines what value to display based on the selected quarter. This field should provide meaningful output for all quarters, including when Q1 is selected.

Calculated Field: Previous Quarter Difference Label

IF [Quarter Selector] = 1 THEN NULL

ELSEIF [Quarter Selector] = 2 THEN "∆Q1:"

ELSEIF [Quarter Selector] = 3 THEN "∆Q2:"

ELSEIF [Quarter Selector] = 4 THEN "∆Q3:"

END

Explanation:

  • IF [Quarter Selector] = 1 THEN NULL: If Q1 is selected, we want to return NULL (or simply leave it blank) since there is no previous quarter to compare against.
  • ELSEIF [Quarter Selector] = 2 THEN "∆Q1": For Q2, show the difference from Q1.

Step 3: Customize Visual Indicators

To further improve the user experience, you can apply this calculated field as a label or a tooltip in your visualization to dynamically show which quarter is being compared. This makes it immediately clear to users what context they are viewing.

Handling Color Indicators

Often, we also want to apply color coding to differentiate between positive and negative changes, or null values. For example, if the profit difference is positive, you might want the color to be blue; if it's negative, orange; and if there's no comparison (such as Q1), you want it to be white to represent "no data available."

Calculated Field for Coloring:

IF ISNULL([Difference in Profit]) THEN "White"

ELSEIF [Difference in Profit] > 0 THEN "Blue"

ELSE "Orange"

END

This field can be used to color the squares or bars in your chart appropriately, providing a consistent visual representation of the data and making it easy to understand at a glance.

Check out the dashboard here.

Author:
Salome Grasland
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2024 The Information Lab