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:
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:
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 |
+------------+-------+-----------+
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):
If you remove the comment within the Tableau Calculated field, it should compile correctly.