# Appending iterated rows to a dataset

**URL:** <https://forum.inductiveautomation.com/t/appending-iterated-rows-to-a-dataset/25200>\
**Category:** General Discussion\
**Created:** [May 21, 2019, 5:12pm UTC](https://forum.inductiveautomation.com/t/appending-iterated-rows-to-a-dataset/25200 "2019-05-21T17:12:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RForshey](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@RForshey](https://forum.inductiveautomation.com/u/RForshey)\
**Post date:** [May 21, 2019, 5:12pm UTC](https://forum.inductiveautomation.com/t/appending-iterated-rows-to-a-dataset/25200/1 "2019-05-21T17:12:11Z")

</div>

I’ve got a script that works kinda/sorta like it should:

for row in eventDict:  
fworkID = str(row[‘fwork-id’])  
fworkCode = str(row[‘fwork-code’])  
fworkDscr = str(row[‘fwork-dscr’])  
fworkstart = str(row[‘start’])  
fworkend = str(row[‘end’])  
d1 = row[‘start’]  
d2 = row[‘end’]  
FMT = ‘%Y-%m-%d %H:%M:%S’  
tdelta = str(datetime.strptime(d2, FMT) - datetime.strptime(d1, FMT))  
fworkstatus = str(row[‘status’])

headers = [‘OP ID’, ‘Step ID’, ‘Description’, ‘Start’, ‘End’, ‘Elapsed’, ‘Status’]  
rows = [  
[fworkCode, fworkID, fworkDscr, fworkstart, fworkend, tdelta, fworkstatus] ,  
]  
dataset = system.dataset.toDataSet(headers, rows)  
event.source.parent.getComponent(‘Test\_Table’).data = dataset

It iterates through eventDict, and puts the last row into the dataset. Problem is, all the other rows aren’t in the dataset. I’m sure the issue is it isn’t “appending” the rows. I have no idea how to get there from here. I’m sure (hopeful?) the issue is simple, I just don’t know enough about python scripting. Any ideas? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 21, 2019, 5:23pm UTC](https://forum.inductiveautomation.com/t/appending-iterated-rows-to-a-dataset/25200/2 "2019-05-21T17:23:10Z")

</div>

You’re only creating one actual row list in the `rows` 2d array/list of lists. Try something like this:

```auto
# create holder list
rows = []

for row in eventDict:
  # for each row in the source, create a new empty row
  newrow = []
  # append each item, in order
  newrow.append(row['fwork-code'])
  newrow.append(row['fwork-id'])
  newrow.append((row['fwork-dscr'])
  newrow.append((row['start'])
  newrow.append((row['end'])
  d1 = row['start']
  d2 = row['end']
  FMT = '%Y-%m-%d %H:%M:%S'
  newrow.append((datetime.strptime(d2, FMT) - datetime.strptime(d1, FMT))
  newrow.append((row['status'])

  # add our new row (casting each element to a string) to the overall rows list
  rows.append([str(r) for r in newrow])

headers = ['OP ID', 'Step ID', 'Description', 'Start', 'End', 'Elapsed', 'Status']
dataset = system.dataset.toDataSet(headers, rows)
event.source.parent.getComponent('Test_Table').data = dataset

```

---

<div class="post-metadata">

**Author:** ![RForshey](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@RForshey](https://forum.inductiveautomation.com/u/RForshey)\
**Post date:** [May 21, 2019, 5:33pm UTC](https://forum.inductiveautomation.com/t/appending-iterated-rows-to-a-dataset/25200/3 "2019-05-21T17:33:56Z")

</div>

Ok cleaned it up and here’s the script:

```auto
# create holder list
rows = []

for row in eventDict:
# for each row in the source, create a new empty row
	newrow = []
# append each item, in order
newrow.append(row['fwork-code'])
newrow.append(row['fwork-id'])
newrow.append(row['fwork-dscr'])
newrow.append(row['start'])
newrow.append(row['end'])
d1 = row['start']
d2 = row['end']
FMT = '%Y-%m-%d %H:%M:%S'
newrow.append(datetime.strptime(d2, FMT) - datetime.strptime(d1, FMT))
newrow.append(row['status'])

# add our new row (casting each element to a string) to the overall rows list
rows.append([str(r) for r in newrow])

headers = ['OP ID', 'Step ID', 'Description', 'Start', 'End', 'Elapsed', 'Status']
dataset = system.dataset.toDataSet(headers, rows)
event.source.parent.getComponent('Test_Table').data = dataset

```

It still only displays the last row in the table…

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [May 21, 2019, 5:44pm UTC](https://forum.inductiveautomation.com/t/appending-iterated-rows-to-a-dataset/25200/4 "2019-05-21T17:44:39Z")

</div>

The section under “`# append each item, in order`” needs to be indented to make it part of “`for row in eventDict:`”. As shown above, the script sets `newrow = []` for each item in `eventDict`, and then goes onto the next section after `row` is set to the last row, which is why you only get the last row.

---

<div class="post-metadata">

**Author:** ![RForshey](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@RForshey](https://forum.inductiveautomation.com/u/RForshey)\
**Post date:** [May 21, 2019, 5:49pm UTC](https://forum.inductiveautomation.com/t/appending-iterated-rows-to-a-dataset/25200/5 "2019-05-21T17:49:28Z")

</div>

Bingo! I now have multiple rows. Outstanding and thanks for the help!
