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 |
+------------+-------+-----------+
4

1 回答 1

1

您看到的错误是由 Tableau 计算字段中的 python 注释 # 符号引起的。

Tableau 将 # 符号视为显式声明日期的一种方法。这是导致您看到的“未终止日期”错误的示例(请注意日期后缺少的 # 符号):

在此处输入图像描述

如果您删除 Tableau 计算字段中的注释,它应该可以正确编译。

于 2019-03-05T19:03:36.493 回答