After creating a workbook in Tableau, it is sometimes desirable to document the calculations so users know exactly what a calculation is doing without having to open the workbook.
Documenting the calculations manually can be tedious and time-consuming, especially if the workbook contains many calculations.
To solve this problem, I’ve written a script that will extract the calculation information from a Tableau workbook.
Accessing the Script
To make the script accessible to others, I’ve hosted and deployed it using Flask, which means you can extract calculations from your Tableau workbooks by simply uploading the twb file.
Do note that although the twb file does not contain the actual underlying data of your workbook, it will contain some data such as your data source information, field names, parameter values, and other meta-data.
All twb files uploaded are periodically deleted from the server, however, if you are not comfortable uploading your twb file–then I recommend running this code in your environment. Reach out to our team for access to the web app.
You can access the source code in my github–the get_calc function extracts the calculations.
About the Script
Tableau workbook files (.twb files) are XML files containing metadata such as data source information, calculations, aliases, grouping, sets, and more. By parsing this XML data, we can get the calculation metadata and extract the data into a table containing the calculation field name, formula, fields used, and comments.
My script is using the ElementTree XML package in python to parse the XML. Using the package, we can search for elements in the XML that contain the calculation. To know the name of the elements that contain the calculation, we can use an XML tree visualizer to easily browse the XML structure.
By exploring the XML in this way, you can quickly see that calculation metadata are stored in the “column” elements.
Once we find the column element, we can get the value for the column caption (name) and calculation formula. If there is a comment, it will appear in the formula with a preceding double-backslash “//.”
In addition to a table of fields and their definitions, my script will output a relationship map that shows how fields are using other fields in their formula. Clicking a field will highlight the originating fields. This map is useful to understand which fields are dependent on other fields.
It is possible to do other interesting things with by extracting or manipulating the XML file. For example, I wrote a separate script that translates the workbook into a different language.
Although these modifications are possible, please be aware that none of these modifications are supported by Tableau–so if your workbook breaks, you won’t have any support. Please remember to save your modifications as a new file!