# Tag Historian Query Aggregation Mode - Range

**URL:** <https://forum.inductiveautomation.com/t/tag-historian-query-aggregation-mode-range/72545>\
**Category:** Ignition\
**Created:** [April 17, 2023, 8:54pm UTC](https://forum.inductiveautomation.com/t/tag-historian-query-aggregation-mode-range/72545 "2023-04-17T20:54:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Allen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_allen/32/47013_2.png) [@Ryan\_Allen](https://forum.inductiveautomation.com/u/Ryan_Allen)\
**Post date:** [April 17, 2023, 8:54pm UTC](https://forum.inductiveautomation.com/t/tag-historian-query-aggregation-mode-range/72545/1 "2023-04-17T20:54:33Z")

</div>

Can anyone help me understand the following behavior when attempting to use the range aggregation mode in the reporting module with a tag historian query? The attached image is a comparison of the same data & time range, one with the aggregation mode set as Closest Value, and the other set as Range. The markups in red is what I would have expected the data to be.

 ![document](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/c/6cceca9d3ca67d7434ecddd530e040e3bdec2d32.png)

---

<div class="post-metadata">

**Author:** ![Ryan\_Allen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_allen/32/47013_2.png) [@Ryan\_Allen](https://forum.inductiveautomation.com/u/Ryan_Allen)\
**Post date:** [April 18, 2023, 7:23pm UTC](https://forum.inductiveautomation.com/t/tag-historian-query-aggregation-mode-range/72545/2 "2023-04-18T19:23:38Z")

</div>

In case this helps anyone else, the solution i totally didn't use chatGPT to help with is as follows:

```auto
	import java
	import jarray
	
	# Get the original dataset
	oldSet = data['tag_history'].getCoreResults()
	ds = system.dataset.toPyDataSet(oldSet)
	column_names = jarray.array(ds.getColumnNames(), java.lang.String)
	
	# Create a list to hold the new data rows
	new_rows = []
	
	# Iterate over the rows of the original dataset
	for i, row in enumerate(ds):
	    # Check if the current row is the first row
	    if i == 0:
	        new_rows.append(row)
	    else:
	        # Create a new list to hold the modified row values
	        new_row = [row[0]]
	        # Iterate over the columns of the current row
	        for j in range(1, len(row)):
	            # Calculate the difference between the current and previous row value
	            diff = row[j] - ds[i-1][j]
	            # Append the modified value to the new row list
	            new_row.append(diff)
	        # Append the modified row to the new data list
	        new_rows.append(new_row)
	
	# Create the new dataset with the modified data
	new_ds = system.dataset.toDataSet(column_names, new_rows)
	
	data['Differential'] = new_ds

```

This then gives the result I was expecting the Range Aggregation Mode to result in

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/2/420f940833fdd5f6b3480891669d94afe774d989.png)
