Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in BI by (47.2k points)

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 |

+------------+-------+-----------+ 

1 Answer

0 votes
by (17.6k points)
  • Basically, this kind of error is caused by the python comment # sign within the Tableau Calculated field.

  • In order to explicitly declare a date, Tableau sees the # sign as a method. Look at this example which 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.

Related questions

Browse Categories

...