2

I am trying to use Tableau calculated field to use my python script. My python script queries a database. I currently use it in Spyder.

Currently I am getting the Unterminated Date Error.

with the following lines underscored in red,

#Remove the list comma
bookList = bookList[:-1]

sql = sql.format ("'" + startDate + "'", "'" + endDate +"'", "'" + nodeNames +"'")

print (sql)

df_Cs01 = pd.read_sql(sql,con)

con.close()

return df_Cs01
)

error msg:

enter image description here

My python script:

import pandas as pd
import pyodbc, os 
import datetime



def GetData (startDate, endDate, nodeNames, server='server'):
    con = pyodbc.connect(r'DSN='+server,autocommit=True)    

    #query removed for simplicity.
    sql = """  e (R.asOfDate >= {0} and R.asOfDate <= {1})
        and R.node = {2}  """

    bookList = ""

    print (nodeNames)
    #loop through the nodeNames
    for nodeName in nodeNames:
        bookList = bookList + "'" + nodeName + "',"


    #Remove the list comma
    bookList = bookList[:-1]

    sql = sql.format ("'" + startDate + "'", "'" + endDate +"'", "'" + nodeNames +"'")

    print (sql)

    df_Cs01 = pd.read_sql(sql,con)

    con.close()

    return df_Cs01

full screen:

enter image description here

Expected results that I would like to display in tableau:

+------------+-------+-----------+
|   Date     | Node  |    sum    |
+------------+-------+-----------+
| 04/02/2019 | Stack | -2.90E+06 |
| 05/02/2019 | Stack | -2.90E+06 |
+------------+-------+-----------+
excelguy
  • 1,574
  • 6
  • 33
  • 67

1 Answers1

1

The error you're seeing is being caused by the python comment # sign within the Tableau Calculated field.

Tableau sees the # sign as a method to explicitly declare a date. Here is an example of something that would cause the 'Unterminated Date' error you are seeing (Take note the missing # sign after the date):

enter image description here

If you remove the comment within the Tableau Calculated field, it should compile correctly.

MonteCarloSims
  • 1,751
  • 1
  • 7
  • 19
  • Thank you. Perfect timing. I just edited my answer to match what I see in your calculated field. – MonteCarloSims Mar 05 '19 at 19:07
  • Nice! Okay that error is solved, but now i got another error lol. `expected closing parenthesis or comma while parsing argument list for script int` on my line `import pandas as pd` . Any idea here? – excelguy Mar 05 '19 at 19:11
  • Indeed, your calculated field needs to be in this format: SCRIPT_INT("_arg1 + _arg2", SUM([A Number]), SUM([Another Number])) - So you'll need quotations around your script, replace fields passed into script with args, a comma, then a list of args from Tableau. Take a look here for formatting/use: https://github.com/tableau/TabPy/blob/master/docs/TableauConfiguration.md – MonteCarloSims Mar 05 '19 at 19:16
  • ahh so complicated :( – excelguy Mar 05 '19 at 19:49
  • Sorry that this one didn't have a simple answer. Once you get the format down and corrections made, though, it is extremely powerful. Good luck! – MonteCarloSims Mar 05 '19 at 19:53
  • one last question daniel, will this script return the data in a dataframe/tabular format in tableau? – excelguy Mar 05 '19 at 20:14
  • From the previous link: "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 example above sends 18 rows of data to TabPy and receives 18 rows back." Answer: depends. :) – MonteCarloSims Mar 05 '19 at 20:17