One-minute mini tutorial
I found my early effort 1, 2 don't create Sankeys as nice as I wanted. Then I fell on Ken Flerlage's Sankey template, which is based on the earlier works of Jeffrey Schaffer and Olivier Catherin. Yes, that's what I am looking for, because I will hand over it to someone who is a beginner in Tableau. Greatly inspired by Ken's work, I went on to create a simpler template as described here. Now I believe that anyone with basic Tableau skills can create Sankey charts in case of need. It is as easy as 1-2-3. Here are the 3 steps:
1.Connect your data source from the Sankey Template Workbook
Download the template here.
2.Union your data table with itself
This doubles the size of your data set.
3.Rename fields and replace data source
Rename 2 dimensions & 1 measure as Source, Target & Size. Then replace the template data source by yours.
Voila. You are done with your new Sankey chart.
You can stop here. Continue if you wish to know more details.
Postscripts
-Using CountD(Dim) or [Number of Records] as SizeThere are a few use cases where people want to represent Number of Records by Size. Then just create this calculated field for the measure Size:
- [Number of Records]
- {Fixed Dim: Max(1)}
-An extra column after union [Table Name]
Besides Source, Target & Size, the Sankey chart needs an extra column Table Name. It is auto generated by union within Tableau. This field is to identify the upper and lower tables in the union. You can do the union outside of Tableau but you need to always creating this Table Name column in your script.
-When data source is other than flat file
The above method assumes data is in a flat file such as Excel or CSV. Depending on your data source, you can union your data table in SQL, Python or R. Then create data set directly with 4 required field names: Source, Target, Size and an extra one Table Name (auto-generated by Tableau union. If not Tableau, we need to add it). Please populate the last column Table Name with 'table1' and 'table2'.
Below is a simple SQL snippet: (add aggregation via group by if you wish.)
Select Dim1 as Source, Dim2 as Target, Measure as Size, 'table1' as [Table Name] From Data
Union
Select Dim1 as Source, Dim2 as Target, Measure as Size, 'table2' as [Table Name] From Data
Then all you need to do is replace the data source. Extract the data source and automate the viz if you wish.
-Minimize data set size
For Sankey chart, all we need is actually 4 fields: Source, Target, Size and an extra Table Name (auto-generated by Tableau union). After union, drag them to a blank sheet, then hide the unused the fields and extract the data. We will get a data extract with 4 fields only. The data pane in Tableau will look a lot cleaner. Data extraction can be a bit faster.
That's it. Enjoy Tableau! BTW, I would be glad to know if this works for you.
PS.
To create a third level, just cascade 2 Sankey charts in dashboard:
https://vizdiff.blogspot.com/2020/01/creating-3-stage-sankey-chart-via.html
Sankey Chart Series in Tableau
5.A Template Approach to 3-Stage Sankey Chart
3.Creating Sankey Chart with Grouping
2.Creating Multi-Stage Sankey Chart via Template Cascading
1.Creating Sankey Chart Made Easy via Template
Works perfect! Thank you so much for sharing!
ReplyDeleteI want to add a third level to the template, so I duplicated and modified every calculated field and everything was good until I had to create the new curve, I modified Curve A Polygon Win 2 in this way: IIF(INDEX()>=50, [Curve A Min Win 2],[Curve A Max Win 2]) but it didn't work.
ReplyDeleteSend me the workbook Alexandermou2000@g
DeleteI faced the exact same issue. I didn't modify Curve A, but for Curve B I used IIF(INDEX()>=50, [Curve B Min Win],[Curve B Max Win])
DeleteIt is not working! Thanks in advance for your help Alexander.
Trying to modify the template is not so easy. The calculation there is fairly complicated. Send me your workbook and write a note about what you want to achieve. @gmail.com
DeleteHi, my stacked bar on the left isn't aligning to the sankey. I'm not sure why. How can I fix this?
ReplyDeletecan't do much without seeing the workbook. If you wish, send it to my gmail with succinct description: alexandermou2000@g.
DeleteThis is fantastic Alex. Building out Sankey's in Tableau from scratch is slow and complicated. With this template you can have a result in seconds. Loved it.
ReplyDeleteHi Alex, thank you for this fantastic an easy to use template. I see people tried to add a third level ... may it be possible that you publish a 3 level sankey?
ReplyDeleteJust cascade two sankey's and you will have a 3-level one.
DeleteThank you Alex!
ReplyDeleteIs it possible do it on tableau public ?
Yes it works with tableau public.
DeleteThanks Alex. Are these templates royalty free?
ReplyDeleteYes. Please cite this source. Thanks.
DeleteHi Alex - Thanks! Again, Is it also possible that you publish a third level and provide some option where the user could add more levels as per the requirements.
ReplyDeleteYou can cascade two sheets to make it happen.
DeleteHere is the tutorial on creating a 3-stage Sankey https://vizdiff.blogspot.com/2020/01/creating-3-stage-sankey-chart-via.html
DeleteHello, excellent visual! Much faster than building from scratch. Thank you. Quick question: is it possible to edit the color scheme of the curves?
ReplyDeleteYes. Just go ahead and edit the colors in the template.
DeleteHello, Thank you for your prompt response. Unfortunately, in the template I have tried via row banding, column banding and a few others without success.
ReplyDeleteIts not the most important detail. What would be neat to do is if I could move the a category, from Target, up or down. For example, right now, category "Strongly Agree" is in the middle of the categories and I would like to move it to the top and then have "Strongly Disagree". Is this possible or can it only be done in the coding?
Thanks!
What does that mean?
ReplyDeleteDidn't work for me. The bar charts did but the curves in the middle didn't.
ReplyDeleteIs it possible to create a Sankey in Tableau that merely tracks a COUNT of records from one side to the other? For example could a Sankey show the flow of users between Status1 and Status2 if you data was structured like:
ReplyDeleteName Status1 Status2
Bob Active Closed
Alice Active Active
Frank Closed Pending
Matt Pending Active
Susan Closed Active
George Closed Closed
Absolutely. let Status1=Source, Status2=Target, Countd(Name)=Size give it a try and let us know if it works.
Deletethank you for sharing, looks perfect,
ReplyDeletebut in my case it gives error of Path(bin), after replacing it doesn't recognize the Path(bin). please if you guide why this happening and how to resolve it. thanks
Could you send me your workbook with description of the problem? you can find my email in the comment area above.
DeleteFaced same issue, error in Path formula which only taking Table instead of Table Name (which has a red exclamation mark)
DeleteMake sure both data sources have the same field name: either Table Name or Table. Rename one to be exactly the same as the other, before replacing the data source. Don't forget to self-union your data source first.
DeleteHi Alexander - I am having the same issue with Path(bin). When I try to replace it, it does not update and rather keeps the old version of Path(bin). Can you please advise?
DeleteThe newest version and the 3-stage is excellent.
ReplyDeleteI was wondering if the Target categories can be manually sorted after the sankey is produced? I have ordinal categories that seem to get arbitrarily mixed once in the sankey form. Thank you.
Yes. Right click on Target pill and select sort. Let me know if that works for you. You have to sort Target the same way on every worksheet which has the dimension Target.
DeleteThank you! This is huge. It is a great teaching tool for my students when teaching categorical data.
DeleteAnother approach, go to the dimension panel and right click on Target>Default Properties>Sort. This will define the sorting once for all sheets. Try it and see if this works better. BTW, if you don't mind, where do you teach?
DeleteThe "Target>Default Properties>Sort" method is what I had done as I couldn't see any other way of doing it. This is what I thought you originally had meant. And it worked perfect.
DeleteI (Brent Marinan) teach both Marketing and Statistics at the University of Arizona. I previously only built Sankeys (from scratch) myself and had used it only as a teaching-tool/illustration. With your template, I can now put the tool in the students hands and they can build their own Sankey (doing it from scratch would be too much for them).
Something of note: With the categorical survey data, I rename "record count" with "Size". Unfortunately, thought, the 2020 Tableau desktop versions remove the ability to rename this measure. So I just use the 2019.4 version.
Strange. Try renaming Size as "record count" instead. As long as field names are identical in both data sources, it should work. BTW, try 2020.2 instead of 2020.1.
DeleteThe other way of sorting is sort the dimension pill on canvas per worksheet. Right click the pill and select sort. This will alter the default sort.
DeleteUnfortunately, no luck renaming Size nor any luck with 2020.2 Desktop. I am good with the earlier version though.
DeleteOne question: I tried to save it to Tableau Public (via Server) but an error came up mentioning it needed to be extracted. If its possible, what would be the process to be save it to Tableau Public?
To save (publish) it in Tableau Public, you need to always extract data source first. Then save/publish the workbook to the server. Since it's a public server, many people are publishing their workbooks to it. Extracts will save storage capacity.
DeleteOkay, makes sense. A first, simple one published with your template. Lots still to learn. Thank you again.
Deletehttps://public.tableau.com/profile/brentm5539#!/vizhome/FrequencyofVisitbyGender/SankeyChartTemplate
Hi, does it allow a filtering using dimensions other than "Target" and "Source"?
ReplyDeleteYes. Regular dimension filters will work as usual.
DeleteHi, how do I download the template? When I click on the link above and download the workbook, I cannot open it.
ReplyDeleteThank you,
Lorenzo
Did you download the latest Tableau Public and install it in your computer?
DeleteHi Alex, I've tried with a simple dataset and the bars at the side appeared but not the curves in the center. Could you provide some guidance please? Thank you!
ReplyDeleteHi Alex, I've tried with a simple dataset and the bars at the side appeared but the curves in the center didn't. Could you provide some guidance please? Thank you!
ReplyDeleteDid you follow the steps, especially the first: union the data source by itself?
Deleteis there a way to get horizontal bars for the left/right charts with labels?
ReplyDeleteSince the vertical bars is on a sheet. You can replace them by your custom horizontal bars at the dashboard level.
DeleteDid you union the data table with itself? In SQL you need to add an extra column “Table Name” which denotes the upper and lower tables as t1 and t2. Try with an static excel table first.
ReplyDeleteSQL snippet is in the above post.
ReplyDelete