SQL Code to Prepare Data for Sankey in Tableau
I continue to get regular emails about building sankey diagrams and sankey-style slopegraphs in Tableau based on the instructions that I posted here. I previously posted an Excel VBA for preparing data for Tableau here. A big thank you to Russell Spangler for creating this SQL code that basically does the same thing.
In this example there are 5 rows of data with 2 rankings, position 1 and position 2 followed by some random fields of junk data. To create the curves in Tableau I use 49 points, which means every row of data needs to be converted into 49 rows of data. In addition, there needs to be an additional column t with values ranging from -6 to 6 in increments of .25 (which is 49 values). For example, the State of America Ranking has 50 curves, one for each state totalling 2,450 rows of data. The Redesign of My Monthly Energy Bill from Duke Energy has 100 curves in the sankey diagram, 1 for each % up to 100%, which totals 4,900 rows.
The SQL code below will do this transformation automatically so that the data can be immediately imported into Tableau.
After running this SQL code there will be 49 rows for each original row of data and a new column for t with the values populated. Download the SQL code here.