tableau / TabPy

Execute Python code on the fly and display results in Tableau visualizations:
https://tableau.github.io/TabPy/
MIT License
1.56k stars 598 forks source link

Unable to Compute Table Calculations using multiple rows, such as a rolling average #464

Closed ethanj20 closed 3 years ago

ethanj20 commented 3 years ago

Environment information:

Issue: I'd like to be able to use TabPy to compute values across multiple rows, such as a rolling average of a value (I understand that computing a rolling average is possible without TabPy, but I'd like to use it for more advanced things as well using the same data structure). For example, see the below image where a 3 day rolling average of storage is computed.

RA1

Sample Code: I have written the following python script and deployed it to a server:

` import warnings warnings.filterwarnings("ignore") from tabpy.tabpy_tools.client import Client import numpy as np import numpy.ma as ma import pandas as pd

def array_function_1(x1): col1 = np.array(x1) col1_fix = np.where(np.isnan(col1), ma.array(col1, mask=np.isnan(col1)).mean(axis=0), col1) col1_df = pd.DataFrame(col1_fix) Y = col1_df.rolling(3).mean() return np.array(Y).tolist()

connection.deploy('Array Function Example', array_function_1, 'This is an example array function', override = True)`

This script will compute a 3 day moving average when provided a list.

In Tableau, I have connected to a sample data source that looks like this:

RA2

My workbook looks like this to start:

RA3

Then I created a calculated field like this:

RA4

When I try to use that calculated field, I get the following error:

RA6

I suspect this is because I am using the "SCRIPT_REAL" function, but I am returning an array. So assuming that the response from the python script has to be a real number, is it possible to return a different real number for each row in the table to compute the rolling average? Is this type of computation even possible in TabPy? Must I pass a rolling portion of the table to the script?

Please let me know if the problem is not clear, and I can provide an example workbook/files.

nmannheimer commented 3 years ago

Your issue here is how the table calculation is being computed. You need to edit the table calc and select "Specific Dimension" then put your Date dimension on addressing (by checking the box next to it. Leaving the box unchecked means the code is trying to evaluate for each different date member, checking it will evaluate it once, passing all fields as lists of the same length as the number of marks in the view.

For more info look here: https://help.tableau.com/current/pro/desktop/en-us/calculations_tablecalculations.htm#specific-dimensions

nmannheimer commented 3 years ago

For more details on how to use analytics extensions look here: https://www.youtube.com/watch?v=nRtOMTnBz_Y

https://www.youtube.com/watch?v=0BN_Y2CxdYY