December 4, 2013

Tableau tip: Don’t waste the ends of your sparklines…make them actionable!

I’ve been meaning to write about how I use sparklines for a while now and the post Two Tips for Meaningful Sparklines in Tableau by Jim Wahl finally gave me the push I needed to get this written.

Sparklines are one of my favorite chart types to include in dashboards, yet I see many people using them without providing enough context.  Some people like to add bandlines, some like to add sets of dots, some like to add text, all in an effort to add meaning to sparklines.  These are perfectly fine, but I think there’s a better way to make sparklines actionable.  Is this the best way? Maybe not, but it is an alternative worth considering.

Sparklines were first introduced by Edward Tufte in his book Beautiful Evidence.  Tufte says: “A sparkline is a small intense, simple, word-sized graphic with typographic resolution.”  Stephen Few expands Tufte’s definition in his book Information Dashboard Design: “Their whole purpose is to provide a quick sense of historical context to enrich the meaning of the measure.  This is exactly what’s required in a dashboard.”

When someone is creating a dashboard, they should provide as much information and meaning as possible to make the information actionable.  I don’t see any examples from Tufte, Few or Jim Wahl that provide much meaningful context to the end of a sparkline.

Tufte provides some examples:

He might add a red dot to the end of the line along with some text to highlight the latest value.

image

While it’s a bit tough to see in this next example, Tufte has used red dots for the beginning and ends of the lines and blue dots to indicate the highest and lowest values.

image

It’s important to also note how Tufte always includes the values associated with all of the highlighted dots.
There are tons and tons of examples of how Stephen Few uses sparklines.  Consider this example from his whitepaper Dashboard Design for Real-Time Situation Awareness

image

Few says: “Meaningful context has been added to these metrics in the form of sparklines, which provide a quick sense of the history that has led up to the present.”  This small section of a dashboard is a classic Few design.  You’ll often see him use (1) sparklines, (2) a visual indicator of health (the red dots in this case), and (3) bullet charts closely together.

When I use sparklines, I like to combine all of the elements of Tufte and Few designs.  Let’s look at an example.

image

On the left you see the sparklines, but notice that I use the dot on the end of the line as an indicator to take action.  Tufte uses the dot one the end to indicate you’re at the end.  Does that make it actionable?  Not necessarily.  Few separates the indicator into its own space and does not mark the end of the sparkline.  My version saves space, increases the data-to-ink ratio, and provides a visual indicator to the reader in one chart.

The table to the right summarizes the sparkline, pulling from Tufte’s practices.  In this example, I’m concerned with comparingthe last two 7-day periods.  Notice how I used conditional formatting so that the dot on the end of the line is the same color as the text in the WoW and WoW% columns.  I don’t use bullet graphs because I feel that the text itself is sufficient; I don’t want to add a graph for the sake of having a graph for everything.

Simple, concise, actionable…all things you want in a dashboard.  Keep reading to see how I built these sparklines in Tableau.

Step 1: The date calculations I use in the example below are simple and efficient when you include a Max Date field in your data source.  Creating Max Date as a calculated field directly in Tableau won’t always work since you need the Max Date at the row level.  In this example, I’ve switched the Superstore Sales data source to Custom SQL and added a subquery to include the Max Date at the row level.

image

TIP: If you have a large dataset, Tableau will run more efficient queries if you push the custom SQL into a view in your database.  Tableau wraps its own SQL around the custom SQL, which can get quite messy and inefficient.  Creating a view will simply the query Tableau runs and improve performance.

Step 2: I like my sparklines to show the last 30 days, so I need to include a date filter.  I include my date filter as the first step so that my data set is smaller to work with from the outset.

image

A Boolean calculation works well here.  Notice how it leverages the Max Date field.  This wouldn’t work if the Max Date was a calculated field inside of Tableau.

image

To get my sparklines to look how I like them, the column and row shelves will need to look like this.  Let’s break the worksheet down into its pieces.

image

Step 3: Create a dummy header and place it on the columns shelf.  Place it on the Columns shelf and hide the fields labels for the columns.

image

Step 4: Right-click-drag Order Date to the columns shelf and choose the first option, Order Date (Continuous).  Notice how it only shows the last 30 days.

image

Step 5: Right-click on the Order Date pill and uncheck Show Header.  This hides the date axis.

Step 6: Drag Category on the Rows shelf and hide the headers.  The headers aren’t needed since they’re on the left side of the table; there’s no need to repeat them.

Step 7: Place Sales on the Rows shelf to the right of Category.  This gives us the lines.  Make them thinner, change the color to dark gray, and resize the chart to make them look like sparklines.

Step 8: Double-click on the Sales axis to bring up the axis options.  Uncheck Include Zero and choose Independent axis ranges for each row or column.  This gives us the view that best fits the space.  Few talks about the scaling options for sparklines in Chapter 10 of Information Dashboard Design.

Step 9: We need a calculated field to show a dot on the end of the line.  You might be tempted to simply turn on the line ends, but that won’t do the trick because you can’t color the line ends only.  The calculated field should only capture sales for the last day.  This is where our Max Date field comes in handy again.

image

Step 10: Drag the new Last Day Sales field onto the Rows shelf to the right of the Sales pill.  Right-click on the Last Day Sales pill and choose Dual Axis.  Right-click on the scale for Last Day Sales and choose Synchronize Axis.  Right-click on Sales pill and uncheck Show Header.

We’re almost done.  All we need to do now is color the dot.


Step 11: I like my dots to be colored by the week over week change.  This requires me to create several calculated fields.  You could combine all of these calculated fields into a single calculation, but I like separating the parts of the calculation to make it easier to understand and so that each calculation is reusable.

Create all of these calculated fields in this order (special thanks to Joe Mako for helping me get these calculations working and showing me why they’re more efficient than what I had been doing):
  1. Last 7 Day Sales:
    IF [Order Date] >= DATEADD('day', -6, [Max Date]) THEN [Sales]
    END
  2. Prior 7 Day Sales:
    IF [Order Date] >= DATEADD('day', -13, [Max Date]) AND [Order Date] <= DATEADD('day', -7, [Max Date]) THEN [Sales]
    END
  3. Total Sales - Last 7 Days:
    IIF(LAST()=0, RUNNING_SUM(SUM([Last 7 Day Sales])), null)
  4. Total Sales - Prior 7 Days:
    IIF(LAST()=0, RUNNING_SUM(SUM([Prior 7 Day Sales])), null)
  5. WoW (week over week):
    [Total Sales - Last 7 Days] - [Total Sales - Prior 7 Days]
  6. WoW %:
    [WoW]/[Total Sales - Prior 7 Days]
Step 12: Click on the Last Day Sales pill on the rows shelf and then drag the WoW % field onto its color shelf.

image

Step 13: Right-click on the WoW % pill, go down to Compute Using and choose Order Date.

Step 14: Double-click on the color legend and change the settings to something like these:

image

You might need to do a bit more formatting to get your viz just the way you want it, but in the end, you’ll want it to look something to this:

image

Notice that I keep the row banding.  I like to include banding on both the sparkline chart and the table so that the reader’s eyes go across the dashboard.

This might seem like a lot of steps, but once you do it a couple of times, it’s pretty quick; you’ll be able to do this in only a couple of minutes.

Building the table is super simple now that you have all of the calculations (this is why I create all of them individually).  Download the workbook here to see how all of this was built.

5 comments:

  1. I would modify your custom SQL by moving "SELECT MAX([Orders$].[Order Date])" from SELECT clause to FROM clause, like:

    SELECT

    FROM [Orders$],
    (SELECT MAX([Orders$].[Order Date]) as [Max Date] FROM [Orders$])

    It will be more efficient because calculation will happen just once, not in every row.

    ReplyDelete
  2. Nice advice!

    Is the term "actionable" referring to the sparkline dot changing color? The user knows what to take action on based on the highlight, correct?

    ReplyDelete
  3. Hi Andy,

    Interesting technique. What about shading the last week instead of just the last point? Or perhaps using grid lines to divide the weeks. Or using weekly sparklines to match the KPIs? (The days vs. weeks context obviously confused me. Technology's dip between weeks three and four is barely visible because of daily spikes earlier in the month.)

    This is basically my pet peeve with sparklines. They often look much better than they work. I was surprised to see the Few example (from 2007) in the 2013, second edition of IDD. The highlighted "abandonment reduction" shows an increase in the last few days, but it's impossible to know if this is a 1% or a 100% change. I could create visually identical sparklines from significantly different underlying data. Hovering on the sparkline to show the labels is not a (good) solution.

    This problem is what led to bandlines. (There's a long discussion somewhere on Few's web site relating to his grade book dashboard competition.)

    There are exceptions, including Tufte's glucose example, which has > 100 data points from a process with random variation. The handful of statistical outliers "squish" the normal variation. But I still prefer the next example in Tufte's description, where he adds a shaded normal range.

    The basic point I was trying to make with the Tableau Stats Month blog post is that it's possible to make sparklines too simple, too concise and increase the possibility of someone taking the wrong action. Or losing confidence in the dashboard.

    I'm a fan of your work and regularly read your blog for inspiration. Thanks for giving me some more to think about re sparklines. :)

    Jim

    ReplyDelete
    Replies
    1. Thanks for the comments Jim! People at FB grasp sparklines quickly, so perhaps I'm simply lucky in that regards. I like your idea of shading the entire last week. I'll have to try that.

      Also, my sparklines usually include two lines each. One for the daily # as a light gray line and a 2nd line for the 7d avg, which I make a dark gray since that's the one I want to focus the reader on.

      In this case, the outliers are more meaningful. I didn't create that example because the super store sales data isn't rich enough for 7d avg lines to be meaningful. I'll have to post another example with better comparisons.

      Delete
  4. Hi Andy,

    Reproducing your steps using Tableau 8.1 and the "Sample - Superstore Subset (Excel)" I realized that the last order for each department was not made on the same day. The custom SQL is returning the max date for all departments combined (12/31/2013) instead of the last order by department, thus not showing a point on the end of the Sparkline for Technology (12/30/2013).

    To fix this I took out the custom SQL code and used the following code to the get the last day sales by department:
    1: Last Order Date = Window_Max(MAX([Order Date]))
    2: Last Day Sales = IF MAX([Order Date])=[Last Order Date] THEN SUM([Sales]) END

    This way you avoid using custom SQL and you can highlight the last data point on the Sparkline even if not all of them happened the same day.

    Also, I used a Top 30 filter on the Order Date to get the last 30 days without using the calculated Max Date from the custom SQL.

    On including bullet charts, I think they are useful when each department has a different aggregated target, like a budget they have to meet and you are showing the performance to date. If the goal is to be below/above certain # each day I would use a reference line on the Sparkline and color the line to show the instances that are on/off target.

    Best,
    Gabriel

    ReplyDelete