Multiply dataset columns with number

Hi ,

I have dataset . i want to multiply 2,3,4 column with a number 0003

which method is good approach do this

please anyone help me out

If this dataset is coming from a SQL query, do it in the query itself:

SELECT t_stamp, Running, Starting * 3, Stop * 3, Stopping * 3 FROM...

noo i am getting from system.tag.queryTagHistory

With a script? What triggers it? How often does it populate the table?

its in timer script. timer run for every one minute

That doesn't seem too bad for scripting. Try something like this:

table = event.source.parent.getComponent('Power Table')
#KWdataset = system.tag.queryTagHistory([...])
if isMMBTU:
	KWtoMMBTU_Multiplier = 3
	headers = system.dataset.getColumnHeaders(KWdataset)
	data = [[KWdataset.getValueAt(row, column) if column < 2 else KWdataset.getValueAt(row, column) * KWtoMMBTU_Multiplier 
				for column in range(KWdataset.columnCount)]
			for row in range(KWdataset.rowCount)]
	MMBTUDataset = system.dataset.toDataSet(headers, data)
	table.data = MMBTUDataset
else:
	table.data = KWdataset

Edit: Updated code to reflect the purpose of multiplying columns 2, 3, and 4 by 3

1 Like

Thanks will try and let you know

Also doable with system.dataset.map from my Ignition Extensions module.

Or you could get fancy and use a custom tag history aggregate:
https://docs.inductiveautomation.com/display/DOC81/Custom+Tag+History+Aggregates

It would probably be best to fix this data at the source, though. If these tag values should be 3x higher...why not enable tag scaling on the root tag, so you don't have to manually adjust history down the road?

3 Likes

Purpose of my approach

when user changes the unit from KW to MMBTU -  i am doing this multiplication

I was wondering what that magic number was for. I've updated my code example to reflect this information.

image
for this conversion

Uggh. 0.003412. When I first read the title, there wasn't a decimal. Columns number 2 & 4 look like an integer datatype, so imagine that a float conversion will be needed.

Here is the corrected code:

#define the following variables
'''
table = event.source.parent.getComponent('Power Table')#path to table
isMMBTU = condition that makes this true
KWdataset = system.tag.queryTagHistory([...])#complete query
'''
def setKWtoMMBTU(KWdataset):
	conversionFactor = 0.003412
	headers = system.dataset.getColumnHeaders(KWdataset)
	data = [[KWdataset.getValueAt(row, column) if column < 2 else float(KWdataset.getValueAt(row, column)) * conversionFactor 
				for column in range(KWdataset.columnCount)]
			for row in range(KWdataset.rowCount)]
	MMBTUDataset = system.dataset.toDataSet(headers, data)
	return MMBTUDataset
if isMMBTU:
	table.data = setKWtoMMBTU(KWdataset)
else:
	table.data = KWdataset
1 Like