0 votes
1 view
in BI by (43.4k 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 (16.3k 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

0 votes
1 answer
0 votes
1 answer
asked Jul 23, 2019 in BI by Vaibhav Ameta (16.3k points)
0 votes
1 answer
asked Dec 29, 2019 in BI by Vaibhav Ameta (16.3k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...