Important TabPy Functions You Need To Know - CNDRO.LLC
3262
post-template-default,single,single-post,postid-3262,single-format-standard,wp-custom-logo,theme-bridge,bridge-core-2.9.4,woocommerce-no-js,tribe-no-js,ehf-template-bridge,ehf-stylesheet-bridge-child,qode-page-transition-enabled,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,columns-4,qode-child-theme-ver-1.0.0,qode-theme-ver-27.8,qode-theme-bridge,qode_header_in_grid,wpb-js-composer js-comp-ver-6.7.0,vc_responsive,elementor-default,elementor-kit-2634

Important TabPy Functions You Need To Know

                                                                       Photo by AltumCode on Unsplash

In our previous tutorial, we did an introduction on what TabPy(Tableau Python Server) is and why everyone should start using it. Just as discussed, TabPy is an API which enlarges Tableau’s capabilities by allowing users to execute Python scripts and saved functions via Tableau’s table calculations.
With TabPy, we can handle our data exploration and visualization better, and the ability to use Python programming language here is the main deal.

Now, let’s discuss each of the python functions we can use to write our TabPy code.

  • SCRIPT_BOOL: This function is mainly used whenever you want to return a Boolean output from a given calculation. e.g. TRUE/FALSE
  • SCRIPT_INT : This function can be used whenever you want to return an output of type integer from the given calculation. e.g. -2,-1, 0, 2
  • SCRIPT_REAL: This function is also used whenever you intend to return an output of type real from the given calculation. e.g. -.25, 0, 2/3
  • SCRIPT_STR: This is used for returning an output of type string from the given calculation.
    e.g. “Jack”, “red”.

Let’s move further and see how we can use each of the functions;

SCRIPT_BOOL: Verify if Profit is Greater than Zero

In our demonstration, we want to loop through our profit values and see if some of the values are greater than Zero or not. The output we get here should be a True or False response.

From the image  above, we used our SCRIPT_BOOL function here. We opened an empty list named verify, which is to store our result. On the next line we looped through _arg1, this _arg1 stands for the Profit measure which we passed in at the last line. Looping through this _arg1, we checked if our data values is greater than zero and pass in the result by appending via the empty list and return the list.

We used the calculation in our view, which is what we have below;

SCRIPT_INT: Looping through Profit Value

With the SCRIPT_INT function, we’ll loop through our profit value and now divide the profit by 100 and round the expected value. Let’s check below and see how it’s done.

From the image above, we demonstrated by looping through the _arg1, the _arg1 here represents the profit and now we could see an empty list was declared which we named “total”. This empty list is where we passed the result of our calculation after dividing by 100 and rounding it up. The reason this empty list was used is for Tableau to handle our result efficiently and if possible to avoid errors.

We used this calculation in our view, which is what we have here below;

SCRIPT_REAL: Finding Relationship Between Sales and Profit

With the SCRIPT_REAL function, we’ll calculate the correlation we have between the sales and Profit, which is more like knowing the relationship we have between both. Our result should be either negative or positive correlation, between the range of -1 and 1.

In our calculation here, we will import a library, which is the Numpy library and we need to use 2 measures now(i.e. Sales & Profit). Just as we’ve done earlier whereby we’ve been passing our argument as _arg1. Here, the _arg1 will stand for Sales and _arg2 will stand for Profit. Let’s see the calculation below;

We tested with the view we have here below;

SCRIPT_STR: Extracting Customer’s Last Name

In this demonstration, we’ll use our SCRIPT_STR function to extract each customer’s last name. Here, we will loop through the customer’s name and use the split function in python to split on whitespaces and with list indices, we extract the Last name. We used the code below;

Let’s use this calculation in our view, we have each of the customer’s Last name extracted as seen below;

Hope you enjoyed this post. Thanks for reading.

No Comments

Post A Comment