# Splitting a table based on a column value

**URL:** <https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [February 26, 2024, 5:58pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382 "2024-02-26T17:58:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kraigen94](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kraigen94](https://forum.inductiveautomation.com/u/kraigen94)\
**Post date:** [February 26, 2024, 5:58pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/1 "2024-02-26T17:58:49Z")

</div>

Hey, everybody!

I've got the following table that's getting it's data from a Named Query:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/8/983d70b6730725584425668a5a381506114adf82.png)  
I'd like to split the table by the station value and create a table for each distinct station. Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 26, 2024, 6:03pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/2 "2024-02-26T18:03:52Z")

</div>

Yes, see my Integration Toolkit module's [groupBy](https://www.automation-pros.com/toolkit/doc/iteration.html#groupby) expression function.

---

<div class="post-metadata">

**Author:** ![kraigen94](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kraigen94](https://forum.inductiveautomation.com/u/kraigen94)\
**Post date:** [February 26, 2024, 6:31pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/3 "2024-02-26T18:31:59Z")

</div>

Thank you for answering. Unfortunately, I'm not able to add modules to the project I'm working on right now.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 26, 2024, 6:44pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/4 "2024-02-26T18:44:35Z")

</div>

You'll have to script it, then.

The general approach would be to have a dictionary of station names, each holding a `DatasetBuilder`. As you iterate through the source, you'd retrieve the existing dataset builder for that station value, creating a new dataset builder as needed, and adding the source row to that builder.

After that, you'd loop through the key-value pairs of the dictionary, using the `.build()` method to get the finished inner dataset per station.

---

<div class="post-metadata">

**Author:** ![kraigen94](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kraigen94](https://forum.inductiveautomation.com/u/kraigen94)\
**Post date:** [February 26, 2024, 9:25pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/5 "2024-02-26T21:25:01Z")

</div>

Thank you, I'll give this a try.

---

<div class="post-metadata">

**Author:** ![kraigen94](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kraigen94](https://forum.inductiveautomation.com/u/kraigen94)\
**Post date:** [February 27, 2024, 6:42pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/6 "2024-02-27T18:42:31Z")

</div>

Sorry to bother you again, but do you have an example of this? I'm pretty new to this kind of programming coming from a PLC background and I'm havin difficulty getting this to work.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 27, 2024, 7:00pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/7 "2024-02-27T19:00:33Z")

</div>

So, I just did one of these to break a historian "Tall" result dataset containing multiple tags mixed together into a dictionary (keyed by tag path) of separated datasets:

```auto
def tallHistoryByPath(ds):
	builderMap = {}
	for row in system.dataset.toPyDataSet(ds):
		p, v, ts = [row[c] for c in ['path', 'value', 'timestamp']]
		dsb = builderMap.get(p)
		if dsb is None:
			dsb = builderMap.setdefault(p, DatasetBuilder().colNames(['t_stamp', p]).colTypes([Date, Double]))
		dsb.addRow([ts, v])
	return [(p, dsb.build()) for (p, dsb) in builderMap.items()]

```

You can rework that to split by station.

Be sure to import the `DatasetBuilder` and the column types you need at the top of the library script where you put this.

---

<div class="post-metadata">

**Author:** ![kraigen94](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kraigen94](https://forum.inductiveautomation.com/u/kraigen94)\
**Post date:** [February 27, 2024, 7:57pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/8 "2024-02-27T19:57:09Z")

</div>

Thanks, this was it! I was able to rework it and implement it into my application, really appreciate it.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 27, 2024, 8:21pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/9 "2024-02-27T20:21:26Z")

</div>

I should mention that the change to returning tuples of tagpath and dataset is to make the return value Perspective-friendly. Returning a dictionary with tag paths as keys is handy for a number of algorithms, but tag paths are not valid JSON keys. 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![kraigen94](https://avatars.discourse-cdn.com/v4/letter/k/919ad9/32.png) [@kraigen94](https://forum.inductiveautomation.com/u/kraigen94)\
**Post date:** [February 28, 2024, 2:30pm UTC](https://forum.inductiveautomation.com/t/splitting-a-table-based-on-a-column-value/86382/10 "2024-02-28T14:30:15Z")

</div>

I can see that, I ended up just taking the tables for my application, but it's a very clever script.
