Perspective 8.1.47
I am trying to create a new datakey to use in a report that is one day prior to the t_stamp from another tag historian query data source. I tried following the manual, but am getting some errors and the manual doesn’t have the exact case I am looking for. I’m pretty sure I’m using the dateArithmetic function incorrectly. I hope I pasted the code correctly. If I remove the dateArithmetic function from line 13 (it overflows in this pasted code to the next line), then the FlowsLogDate data source is created with the FlowsLogDate datakey. How should I format this to do the date math on the source data? Thank you! Also, the data source tag historian query is wide return format if that matters.
def updateData(data, sample):
#data['myKey']='Example'
#build a header and initialize a pydataset
header = ['FlowsLogDate']
filteredDataset = []
#get the results form the FLOWS data source
rawDataset = data['FLOWS'].getCoreResults()
#build the new pydataset out of only some of the FLOWS data keys
for row in range(rawDataset.rowCount):
valFlowsLogDate = dateArithmetic(rawDataset.getValueAt(row,'t_stamp'),-1,"day")
filteredDataset.append([valFlowsLogDate])
#convert the pydataset to a standard dataset
filteredDataset = system.dataset.toDataSet(header, filteredDataset)
#create a new data source with the filtered results
data['FlowsLogDate'] = filteredDataset
Sorry, should have also reported that the Data Collection Errors warn message is: Error invoking script.Traceback (most recent call last): File “<function:updateData.” line 13 in updateDataNameError: global name ‘dateArithmetic’ is not defined.
And a slider bar appears on the published post where line 13 does not overflow to the next line.
You're in python scripting. system.date.addDays() is what you're going to want.
2 Likes
Thank you @tgarrison, that fixed the error. Do you know how I get this script to work for all rows? It now just returns the same date for all rows of my report.
If I “show calculations” in the key browser and select datakey “count” for the FlowsLogData source, it does give the correct count of rows.
Are you sure your t_stamp column has varying dates?
Yes, if I put t_stamp back in the report details cell, it populates sequential dates (one day off) for each row.
It’s encouraging that it is the first date I would expect, each row is just not incremented by one calendar day.
Nevermind, if I create a new table by dragging the new data source onto my report from the Key Browser, and drop datakey ‘FlowsLogDate’ onto a cell in the Details row, it populates the dates I would expect. I just need to figure out how to relate the data into my existing object. Or reformat my report with an added table component. Sigh, I don’t like the interface to manipulate report objects on the designer.
Odd. Your script looks like it should be good. Maybe step it thru (remove the date math and see if the .get(row,t_stamp) does return the sequential dates..) Then go from there. Or add the row number to your output dataset so you can verify it's actually incrementing the row number.
I tried adding the the data source to the table and that didn’t seem to correct it. Instead, I just deleted the datakey out of the existing problem cell for t_stamp, and drug the new data source onto the report to create a table, formatted details row to be one column wide with same dimensions as the problem cell in the other component and it works perfectly. Thanks again for the help!
1 Like