# Min,Max,Avg Tag Calculation Query

**URL:** <https://forum.inductiveautomation.com/t/min-max-avg-tag-calculation-query/39598>\
**Category:** Ignition\
**Tags:** ignition80, scripting, perspective\
**Created:** [October 12, 2020, 4:45pm UTC](https://forum.inductiveautomation.com/t/min-max-avg-tag-calculation-query/39598 "2020-10-12T16:45:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![miyanalu](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@miyanalu](https://forum.inductiveautomation.com/u/miyanalu)\
**Post date:** [October 12, 2020, 4:45pm UTC](https://forum.inductiveautomation.com/t/min-max-avg-tag-calculation-query/39598/1 "2020-10-12T16:45:33Z")

</div>

Hey, I am trying to use the system.tag.queryTagCalculation() to pull the minimum, maximum and average of a tag and have ran into some inconsistency.

1. The Maximum Value is not matching the historical data (Station 9 State/Station 9 Valve)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/3/439332ead6a0140422fd41fd4218e587249ee258.png)  
Script Implementation:  
Tag Binding

```auto
def transform(self, value, quality, timestamp):
	"""
	Transform the incoming value and return a result.

	Arguments:
		self: A reference to the component this binding is configured on.
		value: The incoming value from the binding or the previous transform.
		quality: The quality code of the incoming value.
		timestamp: The timestamp of the incoming value as a java.util.Date
	"""
	tag = [value["tag"]]
	startDate = ""
	endDate = ""
	timeDiff = 0
	if value["historicalMode"]:
		try:
			endDate = system.date.parse(value["endDate"])
			startDate = system.date.parse(value["startDate"])
			timeDiff = system.date.secondsBetween(startDate, endDate)
		except:
			endDate = ""
			startDate = ""
	if timeDiff>0 and value["historicalMode"]:
		result = ScriptLib.DashBoards.analytics.getMinMaxAvg(tag, returnSize = 10,interval = 1,intervalUnit = "hr" ,jsonFormat = True, endDate = endDate,startDate=startDate)
		type = 1
	elif timeDiff <= 0 and value["historicalMode"]:
		result = [{"Minimum":0,"Maximum":0,"Average":0}]
		type = 2
	else:
		result = ScriptLib.DashBoards.analytics.getMinMaxAvg(tag, returnSize = 500,interval = 5,intervalUnit = "min" ,jsonFormat = True)
		type = 3
	return result

```

Script Library

```auto
def getMinMaxAvg(tags, returnSize = 100,interval = 1,intervalUnit = "hr" ,jsonFormat = True, endDate ="",startDate = ""):
	#Set the interval to all lower case
	log = system.util.getLogger("MinMaxAVG")
	info = tags[0]
	intervalUnit = intervalUnit.lower()
	if endDate == "":
		endDate = system.date.now()
	if interval < 1:
		interval = 1
	if startDate == "":
		if intervalUnit in ["hr","h","hours","hour"]:
			startDate = system.date.addHours(endDate,-interval)
		elif intervalUnit in ["sec","s","seconds","second"]:
			startDate = system.date.addSeconds(endDate,-interval)
		elif intervalUnit in ["min","m","minutes","minute"]:
				startDate = system.date.addMinutes(endDate,-interval)
		else:
			startDate = system.date.addDays(endDate,-interval)
	info = info+", Time - StartDate: " + str(startDate) + ", EndDate: "+ str(endDate)
	data = system.tag.queryTagCalculations(tags,["Minimum","Maximum","Average"],startDate,endDate)
	if jsonFormat:
		data = ScriptLib.Framework.utility.datasetToJSON(data)
	info= info +", Minimum: "+str(data[0]["Minimum"])
	info=info +", Maximum: "+str(data[0]["Maximum"])
	log.info(info)
	return data

```

1. When start and end dates are equal, perspective fails to load data and freezes the screens, I made a workaround for this issue.

Maybe I need to get the data and apply secondary min max average

---

<div class="post-metadata">

**Author:** ![miyanalu](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@miyanalu](https://forum.inductiveautomation.com/u/miyanalu)\
**Post date:** [October 14, 2020, 6:57pm UTC](https://forum.inductiveautomation.com/t/min-max-avg-tag-calculation-query/39598/2 "2020-10-14T18:57:49Z")

</div>

Can anyone please help?
