• Franco Arda

# How to add Tableau Parameters in Python code?

One of the most exciting parts of integrating Python into Tableau is the ability to add Tableau Parameters.

Let's take this very simple example where we call a random number with Python. With Tableau's Parameter, we can control "mu_parameter" (the mean) and "sigma_parameter" (the standard deviation). This looks like:

The exciting part is that we can control both variables (mean and standard deviation) with our Tableau Parameter.

Obviously, we would not need Python for a mean and standard deviation, but could use Tableau's calculated field. The goal was simply to show how easy it is to control Python arguments with Tableau's parameters for more advanced analytical code.

Random Normal code for Tableau's Calculated Field: The Numpy function numpy.random.normal draws random samples from a normal (Gaussian) distribution, named after Carl Friedrich Gauss. According to SciPy, the probability density function of the normal distribution is often called the bell curve because of its characteristic "bell-like" shape. SCRIPT_REAL("

from numpy import random as rd

idx, mu, sigma = _arg1[0],_arg2,_arg3

return (rd.normal(mu, sigma, idx)).tolist()

",

SIZE(), [mu_parameter], [sigma_parameter])

Important: for this script to work, you need to have TabPy running.

A few comments to the Calculated Field: - "Script_Real" is the name of the Tableau function that calls Python. - "Script_Real" simply is Tableau's term for a float. - _args refer to arguments passed to Python. - _arg1[0] refers to the index (idx) in Tableau (in my case, just a list of numbers from 1- 900). - [0] Python starts counting at 0, not 1. - _arg2 refers to mu (mean) and _arg3 refers to sigma (standard deviation). - Yes, the order of the arguments in Tableau is confusing. - Numpy's ndarray.tolist() simply returns a list. And in our case, this is simply a 1D array.

For those who want to learn more about integrating Tableau and Python should check out the fantastic Tableau book "Mastering Tableau 2019.1 - 2nd Edition" by David Baldwin (LinkedIn) and Marleen Meier.

In order empower Tableau users/viewers/analysts, we add two Tableau Parameters which can interact with the Python code. The first parameter is the "mu_parameter" (mean). In general, it's more common to set mu to 0, but I prefer 1. In my experience, analysts are more likely to drill deeper if something is a "bit off."

Here's the second Tableau Parameter for sigma (standard deviation). In real-life, we rarely need more than a maximum of 3 standard deviations (or 99.7% of the distribution), but I feel it looks better with a higher maximum.

Conclusion: I hope you're as excited as I am about adding Tableau parameters in Python code. The goal of this blog post was to introduce readers to integrating Tableau parameters in Python. In future posts, I will add business cases which included this technique.

The biggest use cases are Multiple Linear Regression (not the Tableau default) for dynamic pricing or forecasting. Companies using actively multiple linear regressions will be thrilled how easy it is to create an end-to-end Machine Learning solution in Tableau - even on your smartphone!

68 views

### Recent Posts

See All

#### Are we promoting more men than women?

Franco Arda, Frankfurt am Main (Germany)