Writing one column from a dataset tag to a new dataset tag

I’m trying to write one column from a dataset tag to a new dataset tag so I can link the tag to my template I will be creating. The dataset tag I am pulling from currently has 6 columns however I only want information from 1 column. I tried using the system.tag.read(“first dataset tag”).value.getValueAt(1,"Title of the Column) and was able to get the first row of information but I would like to obtain all the rows for this column. I don’t know how to specify that I want all the rows on the dataset tag rather than a specific row. I then plan on writing the information to my new dataset tag and was wondering what the best method would be.

Thanks,
Jo

I wrote this one a while back for a rainy day. This one looks convoluted, but it will work with both BasicDataSets and PyDataSets, and can return either type as its output.

def colToDataSet(dataset, colName, returnPyDataSet = False):
	# Check to see if input really is a dataset, then create data appropriately.
	if str(type(dataset)) == "<type 'com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities$PyDataSet'>":
		data = [[row[colName]] for row in dataset]
	elif str(type(dataset)) == "<type 'com.inductiveautomation.ignition.common.BasicDataset'>":
		colData = dataset.getColumnAsList(dataset.getColumnIndex(colName))
		data = [[item] for item in colData] 
	else:
		raise Exception("Input is not a dataset")

	if returnPyDataSet:
		return system.dataset.toPyDataSet(system.dataset.toDataSet([colName], data))
	else:
		return system.dataset.toDataSet([colName], data)

	
tagIn = '[default]path/to/source/tag'
tagOut = '[default]path/to/destination/tag'
columnName = 'Column Name'

dataIn = system.tag.read(tagIn).value

system.tag.write(tagOut, colToDataSet(dataIn, columnName))

1 Like

To expand a bit on Jordan’s answer, as you found out, getValueAt only returns a single cell. You need a function that will return an entire column.

If you want a simpler one that works with a tag, you can try this:

# read the full dataset from the tag
data = system.tag.read("[default]MyDatasetTag").value

# extract only the column we want
smallData = system.dataset.filterColumns(data, ["Col 2"])

# display it somewhere so we know it worked
event.source.parent.getComponent('Table 1').data = smallData
5 Likes

Thank you Kathy! It worked like a charm and was simple to understand.

1 Like

Hello Kathy,

This seems like what I'm looking for, but is there a way to use this in an expression dataset tag expression?

For example: Tag1 is a dataset with 5 columns. I would like Tag2 to be a dataset containing column1 of Tag1. Can this be done as an expression instead of as an action script?

Thank you