It’s possible to create calculations based on python scripts with Tableau, but there are many steps involved. Here’s a how-to guide.
How Python fits into Tableau (and how that differs from Power BI)
Regarding Python, different visualisation tools use it for different purposes. For instance, PowerBI allows you to create new chart types with Python, but you can’t use it in calculated key figures.
With Tableau, the important thing to note is that Python will return the result of a calculation but can’t be used to create a new chart type.
Install Python and the chart libraries
First you need to install Python on your machine from the python website.
You also need to install popular libraries for charts such as matplotlib and seaborn.
You also need to install the Tableau package TabPy.
In order to do so, use the pip tool (it’s normally packaged with python).
Open the Windows command prompt (cmd) and type commands such as:
pip install matplotlib
pip install seaborn
pip install tabpy
Install and start TabPy
Start the service in the Windows command prompt (cmd) with the command below. You might get a warning from your firewall asking you to authorise this program.
tabpy


Connect Tableau Desktop to TabPy
Open Tableau Desktop and enable the service via Help > Settings and Performance > Manage Analytics Extension Connection.
Enter Server = localhost; Port = 9004. Click “OK”.
I’ve got version 2020.4 and I didn’t get a confirmation message but it still worked.
You can now use Python with Tableau!
Using Python in a calculated field
The next step is to implement some Python code.
Implementation is done within a calculated field. You can enter scripts there that do just about anything. Like any other calculated field, you can leverage parameters for instance.
Resources:
Leave a Reply