Blogs / Tableau / Exploring the basics of TabPy Coding

# Exploring the basics of TabPy Coding

Jul 9, 2020

Data being the most valued treasure, it has become inevitable to use data for advanced analytics purposes to gain useful business insights for the betterment of one’s organization. Python and Tableau are among the best technologies addressing business driven-advanced analytics challenges. This blog briefs about the basics of TabPy coding from Tableau after connecting Python with Tableau. Before discussing TabPy code let us discuss all the python functions used to write a basic TabPy code.

### Python Functions

To let Tableau know that the calculations are using TabPy, it must be passed through one of the four python functions. Python functions that are used in the calculated field to indicate TabPy are

1. SCRIPT_REAL
2. SCRIPT_INT
3. SCRIPT_STR
4. SCRIPT_BOOL

TabPy functions are always computed as Table calculations in Tableau. In addition to these TabPy functions, Tableau aggregate measures like sum(), ATTR(), MIN(), MAX() and so on can also be included in TabPy calculation.

#### What do all the TabPy functions represent and when should they be used?

1. SCRIPT_REAL: SCRIPT_REAL function is used when we desire to return an output of type real from the given calculation.
2. SCRIPT_INT: SCRIPT_INT function is used when we desire to return an output of type integer from the given calculation.
3. SCRIPT_STR: SCRIPT_STR function is used when we desire to return an output of type string from the given calculation.
4. SCRIPT_BOOL: SCRIPT_BOOL function is used when we desire to return an output of type boolean from the given calculation.

Whenever any of these functions are used in a calculated field the calculation is passed directly to a running TabPy external service instance.

### TabPy Example for each Python function

SCRIPT_REAL: Pearson Correlation Calculation

Pearson correlation coefficient or the bivariate correlation is the measure used to measure the linear relationship between two variables. Correlation can either be positive or negative indicating if a particular variable is impacting another variable positively or negatively. In our example, we are using Sample Superstore data to find the Pearson Correlation coefficient between Sales and Profit. Since the Pearson Correlation coefficient can be either positive or negative, we use SCRIPT_REAL in this calculation. Here Sales and Profit are the two variables of whom the correlation is assessed by passing them as argument1(arg1), argument(arg2). Tableau aggregates the data before sending it to TabPy using the level of detail of the view. The function corrcoef returns a result matrix and from that, the correlation coefficient is extracted so that a single column is returned.

SCRIPT_INT:  Multiplying Sales by two

In our example, we are using Sample Superstore data and placed the sales value of a category and added table calculation using a python script for multiplying sales value by two. Since the desired return output is of type integer we are using SCRIPT_INT in this calculation. Here Sales is passed as the argument(arg1) and all the values in the field are multiplied by two using for loop and the results are appended to the output list.

SCRIPT_STR: Reversing Case of all text

In our example, we are using Sample Superstore data and placed the Customer Name and added table calculation using a Python script for reversing the case of all the alphabets in a Customer Name. Since the desired return output would of return type string we are using SCRIPT_STR in this calculation. Here we have Customer Name as the argument1(arg1) and swap case function is used to change the case of each alphabet iteratively using for loop.

SCRIPT_BOOL: Checking for positive value in Profit

In our example, we are using Sample Superstore data and placed the profit value of a category and added table calculation using a Python script for checking the positive value in profit. Since the desired return output would either be true or false we are using SCRIPT_BOOL in this calculation. To identify if the value is positive or negative this SCRIPT_BOOL function helps to get the desired result as needed. Here argument1(arg1) is equal to Sum(Profit). The script is a basic conditional expression to check if the values in Profit measure is greater than zero or less than zero.

With this, our guide on the basics of TabPy coding comes to an end. With these guidelines let us catch up on how to Implement a Machine Learning TabPy code from scratch in our future blogs.

Corporate HQ:
5920 Windhaven Pkwy, Plano, TX 75093

+1 888-227-2794

+1 972-232-2233

+1 888-227-7192

solutions@visualbi.com 