# Combine result of two Data sources into a table Report

**URL:** <https://forum.inductiveautomation.com/t/combine-result-of-two-data-sources-into-a-table-report/26980>\
**Category:** Ignition\
**Created:** [July 30, 2019, 7:29am UTC](https://forum.inductiveautomation.com/t/combine-result-of-two-data-sources-into-a-table-report/26980 "2019-07-30T07:29:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nader.chinichian](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nader.chinichian/32/9261_2.png) [@nader.chinichian](https://forum.inductiveautomation.com/u/nader.chinichian)\
**Post date:** [July 30, 2019, 7:29am UTC](https://forum.inductiveautomation.com/t/combine-result-of-two-data-sources-into-a-table-report/26980/1 "2019-07-30T07:29:04Z")

</div>

Hi  
I define two data sources in report with the same time range:  
1 - History Tag Query with ‘Average’ aggregation mode  
2 - History Tag Query with ‘Range’ aggregation mode

How can I use the result in only one table in report? The table component only accept one Data Key.  
I want some column show average and some of them show Range for the same interval.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 30, 2019, 11:39am UTC](https://forum.inductiveautomation.com/t/combine-result-of-two-data-sources-into-a-table-report/26980/2 "2019-07-30T11:39:40Z")

</div>

You’ll need to use a script data source to combine them into one dataset, then use the key created by the script in your table.

---

<div class="post-metadata">

**Author:** ![nader.chinichian](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nader.chinichian/32/9261_2.png) [@nader.chinichian](https://forum.inductiveautomation.com/u/nader.chinichian)\
**Post date:** [July 30, 2019, 6:55pm UTC](https://forum.inductiveautomation.com/t/combine-result-of-two-data-sources-into-a-table-report/26980/3 "2019-07-30T18:55:31Z")

</div>

Could you please show me some examples code

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 30, 2019, 8:05pm UTC](https://forum.inductiveautomation.com/t/combine-result-of-two-data-sources-into-a-table-report/26980/4 "2019-07-30T20:05:20Z")

</div>

Since I have no idea what you have, I’ll assume a few things:

- Two exiting datasources: _averageQuery_ and _rangeQuery_
- Each dataset has two columns: _t\_stamp_ and _value_

There are a couple of different ways to combine datasets.

If the datasets have different time stamps and you want to keep them all. This is great for graphs and charts, but perhaps not so much for tables:

```python
def updateData(data, sample):
	"""
	This function has the opportunity to add additional data into the report's
	data map.

	Arguments:
		data: This is a map, whose keys are report data keys and values should
		      be sequences, maps, scalar values, or datasets to provide information to
		      the report.
		sample: A flag that will be True if the report data being gathered is
		        for a preview of the report. Use to avoid slow queries and calculations
		        to keep previews quick.
	"""
	averageDataset = system.dataset.toPyDataSet(data['averageQuery'])
	rangeDataset = system.dataset.toPyDataSet(data['rangeQuery'])
	
	headers = ['t_stamp', 'average', 'range']
	dataOut = []
	for row in averageDataset:
		dataOut.append(row[0],row[1], None)
	for row in rangeDataset:
		dataOut.append([row[0], None, row[1]])
		
	combinedDataSet = system.dataset.toDataSet(headers, dataOut)
	data['combinedData'] = system.dataset.sort(combinedDataSet, 0)

```

Or you just keep the timestamps from one dataset, which makes for a cleaner looking table. (this example uses the timestamps from the average data):

```python
def updateData(data, sample):
	"""
	This function has the opportunity to add additional data into the report's
	data map.

	Arguments:
		data: This is a map, whose keys are report data keys and values should
		      be sequences, maps, scalar values, or datasets to provide information to
		      the report.
		sample: A flag that will be True if the report data being gathered is
		        for a preview of the report. Use to avoid slow queries and calculations
		        to keep previews quick.
	"""
	averageDataset = system.dataset.toPyDataSet(data['averageQuery'])
	rangeDataset = system.dataset.toPyDataSet(data['rangeQuery'])
	
	headers = ['t_stamp', 'average', 'range']
	dataOut = []
	for avgRow, rangeRow in zip(averageDataset, rangeDataset):
		dataOut.append([avgRow[0],avgRow[1], rangeRow[1]])
	
	data['combinedData'] = system.dataset.toDataSet(headers, dataOut)

```

---

<div class="post-metadata">

**Author:** ![KA4HRE](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ka4hre/32/42145_2.png) [@KA4HRE](https://forum.inductiveautomation.com/u/KA4HRE)\
**Post date:** [January 13, 2021, 10:05pm UTC](https://forum.inductiveautomation.com/t/combine-result-of-two-data-sources-into-a-table-report/26980/5 "2021-01-13T22:05:57Z")

</div>

```auto
dataOut.append(avgRow[0],avgRow[1], rangeRow[1])

i think needs to be 

dataOut.append([avgRow[0],avgRow[1], rangeRow[1]])

```

---

<div class="post-metadata">

**Author:** ![alainia.spencer](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@alainia.spencer](https://forum.inductiveautomation.com/u/alainia.spencer)\
**Post date:** [January 25, 2022, 7:09pm UTC](https://forum.inductiveautomation.com/t/combine-result-of-two-data-sources-into-a-table-report/26980/6 "2022-01-25T19:09:54Z")

</div>

What if you are trying to combine 2 datasets that do not contain the same amount of rows. For an example, I have two queries where I’m gathering historical data from. Sometimes, depending on the timing, we might have more daily records in one table then the other. How does that work?

---

<div class="post-metadata">

**Author:** ![jsteele](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jsteele/32/40285_2.png) [@jsteele](https://forum.inductiveautomation.com/u/jsteele)\
**Post date:** [June 1, 2022, 5:28pm UTC](https://forum.inductiveautomation.com/t/combine-result-of-two-data-sources-into-a-table-report/26980/7 "2022-06-01T17:28:34Z")

</div>

> [@JordanCClark](#):
>
> ```python
> averageDataset = system.dataset.toPyDataSet(data['averageQuery'])
> rangeDataset = system.dataset.toPyDataSet(data['rangeQuery'])
> 	
> headers = ['t_stamp', 'average', 'range']
> dataOut = []
> for avgRow, rangeRow in zip(averageDataset, rangeDataset):
> dataOut.append([avgRow[0],avgRow[1], rangeRow[1]])
> 	
> data['combinedData'] = system.dataset.toDataSet(headers, dataOut)
> 
> ```

I had to add .getCoreResults()

averageDataset = system.dataset.toPyDataSet(data['averageQuery'].getCoreResults())
