Another table calculation related challenge this week, set by Luke, visualising cumulative starts for NFL Quarterbacks per team from 2006.
Luke provided the data within a Tableau workbook template on Tableau Public, so I started by downloading the workbook and understanding the data structure.
The challenge talks about teams playing over 17 weeks, but the data showed some data associated to weeks 28-32. So I excluded these weeks by filtering them out.
I then started to build out the data in tabular form, so I could start to build up what was required. I added Team, Season, Week and Player ID to Rows, and just to reduce the amount of data I was working with while I built up the calcs, chose to filter to the Teams ARZ, ATL & BLT.
What we’re looking to do is examine each Player ID and work out whether it is the first record for the Team or whether it differs from the previous row’s data. If so then we’re at the ‘start’ of a run, so we record a ‘counter’ value of 1. If not, the values match, so we need to increment the counter.
We’ll do this in stages.
Firstly, let’s get the previous Player ID value.
Prev Player ID
This ‘looks up’ the record in the previous (-1) row. Change this field to be discrete and add to the Rows. Set the table calculation to compute by all fields except Team.
Each Prev Player ID value matches the Player ID from the row before, unless its the first row for a new Team in which case the value is Null.
Then we can create a field to check if the values match
Match Prev Player ID
MIN([Player Id])=[Prev Player ID]
Add this to the view and set the table calc as above, and the data shows True, False or NULL
Now we can work out the consecutive streak values
IF (NOT([Match Prev Player ID])) OR ISNULL([Match Prev Player ID]) THEN 1
If we don’t match the previous value or we’re at the start of a new team (as value is NULL), then start the streak by setting the counter to 1, otherwise increment the counter. Add this to the view and set the table calc for both the nested calculations as per the settings described above.
Next we need to identify the last value of the Consecutive Streak for each Team.
WINDOW_MAX(IF LAST()=0 THEN [Consecutive Streak] END)
The inner IF statement, will return the value of Consecutive Streak stored against the last row for the Team. All other rows will be Null/blank. The WINDOW_MAX() statement then ‘spreads’ this value across all the rows for the Team.
Add this onto the view, and set the table calc for all the nested calcs.
Finally, we need one more bit of data. The chart essentially plots values from 2006 week 1 through to 2020 week 17. We need to ‘index’ these values, so we have a continuous week number value from the 1st week. We can use the Index table calculation for this
Add this field to the view, set it to be discrete (blue pill) and position after the Week field on the Rows. Set the table calc as usual, so the Index restarts at each Team.
Now we’ve got all the data points we need, we can build the viz. I did this by duplicating the tabular view and then
- Remove Prev Player ID and Match Prev Player ID
- Move Season, Week and Player ID from Rows to Detail
- Move Current Streak from Text to Rows and change to be discrete (blue)
- Move Index from Rows to Columns and change to be continuous (green)
- Move Consecutive Streak from Text to Rows
- Change mark type to bar, and set to fit width to expand the view.
- Change Size to be Fixed, width size 1 and aligned right
- Set the border on the Colour shelf to be None.
- Remove the Team filter and adjust the row height
All that’s left now is to set the tooltip (add Player to the Tooltip shelf to help this), and then apply the formatting. You can use workbook formatting (Format -> Workbook menu) to set all the font to Times New Roman.
Hopefully this is enough to get you to the end 🙂 My published viz is here.
Happy vizzin’! Stay Safe!