Skylark1818
Skylark1818

Reputation: 39

Conducting Python script calculation in Tableau

I am trying to calculate the average daily volume of a stock in Tableau with given stock ticker. I have already connected Python with Tableau; however, the calculation failed and the following error message keeps showing up: enter image description here

The following is my python code in Tableau: enter image description here

I created a parameter in Tableau to enable user select the stock they are interested in. And the parameter's name is [Stock ticker]. Could anyone please help me out this? Thanks

Upvotes: 1

Views: 612

Answers (1)

MonteCarloSims
MonteCarloSims

Reputation: 1771

From the TabPy Github Documentation:

"Tableau expects the SCRIPT to return a single column that has either a single row or the same number of rows as it passed to TabPy."

The ATTR([Stock ticker) parameter that is being sent to Python is actually being sent in an array. If you imagine your data in Tableau as a large crosstab, that parameter is actually repeated for every row.

I would guess that if you dragged [Number of Records] to a sheet you would see that Tableau contains 519733 records - the first number in the error. Your Python script is attempting to return 1612 records to match the 519733 that Tableau expects to receive.

I agree with @cmcau that either all or some of this could be done in Tableau alone. The code could look something like this - using an LOD function:

{Fixed [Symbol]: SUM([Shares])}/85

Note that [Symbol] above is a field and not a parameter

Essentially doing the same thing - aggregating all shares per symbol then dividing by 85.

Otherwise, in your Python script try changing your arg from a parameter (which is the same value repeated for all rows) to a field (which would then be specific to that row in the array sent to Python.) Doing this will cause the Python return to be an array of values that match the array sent to Python. Be cautious though, because as your Python code is written every row read in the array sent to Python will cause a lookup of your entire CSV.

Upvotes: 1

Related Questions