
Extracting Calculated Fields from Tableau with Alteryx
DISCLAIMER: XML ‘hacking’ is not officially supported by Tableau and therefore we recommend to always backup your Tableau workbook before you start tweaking the underlying code.
Have you ever thought about extracting your calculations from your Tableau workbook? Maybe you want or need to document all your calculations so you can easily show your colleagues the logic behind your calculations. In this blog I will share an Alteryx workflow that I built to extract calculations from a Tableau workbook and I will explain why you should download this workflow.
Why would you want to extract your calculations?
Sounds good, where can I download the workflow?
You can download the workflow from the Alteryx Gallery here so you can follow along in this blog. The workflow has been built in the most recent version of Alteryx. You can download 10.5 here.
Sweet, but how does this workflow work?
The Alteryx workflow runs off a .twb file. Tableau has several file formats (.twb, .twbx, .tds, etc). You can read more about the various file types in this excellent blog by Tableau Zen Master Jonathan Drummey. What you need to understand is that a Tableau workbook is basically an XML file containing metadata about your data sources, worksheets, dashboards, colours, parameters and calculated fields.
If you open the .twb file in a text editor or IDE, in fact any tool that can read XML will do the trick, you will see something similar to this:
Below you see the Alteryx workflow I built. It’s a combination of XML parse and Regular Expression tools.
The first step is to open the Tableau workbook file (.twb) as an XML file in Alteryx. You can also inspect the XML from within Alteryx by changing the user setting (Options > User Settings > Edit User settings > Advanced > Display XML in Properties window).
Once the XML has been parsed you can use Regex to extract particular elements of an XML child. I also used a Select tool to filter out only the outer XML columns I needed. In this case I used Regex to take out the Caption, Calculation Name, Formula, Datatype, Role and Type of each calculation in my workbook. I then used the output tool to output these six columns to a CSV file. See below an example of three calculations that I created in Tableau and then extracted with Alteryx.
When your workbook changes the only thing you have to do each time is to re-run the workflow and your output is updated with the latest calculations.
Nice, where can I learn more about hacking the XML?
If you want to read more about the possibilities of ‘hacking’ the XML you should also read this blog from my colleague Emma Whyte.
Update: Tableau enthusiast Michael Mixon created another Alteryx workflow that enables you to extract calculated fields from your Tableau workbook. You can read and download it here.
This is great post Andre. It inspired me to create my own Alteryx workflow for auditing TWB files. The main thing I added to what you’d already done is a routine that cleaned up the calculations that included generic field references like Parameter1, [Calculation_0021112080235996] etc. Would love your feedback.
Link to my Alteryx community post w/ Alteryx workflow attached:
https://community.alteryx.com/t5/Data-Preparation-Blending/Tableau-TWB-Audit-Workflow-Sharing-and-Asking-for-Input/m-p/26422#U26422
Thanks Michael! Yes, I see your workflow gathers a lot more metadata then mine. Also like how you are splitting up the workflow in parts (data connection, file name etc.) This adds a little bit more context to the output. Great work! The calc cleaning macro you have added is pretty awesome to be fair – I should have included that.