Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: How to dynamicaly calculate data Averagevalue(AVG Function can't work!)  (Read 3342 times)

showmesky

  • Newbie
  • *
  • Posts: 6
    • View Profile
I know the function AVG.
I used it like that: AVG($D{data....}.
If it worked well like this: (1+2+3+4+5)/5=3,that's what I want.
but sometimes ,the data is like :0,0,0,0,5,so the function will get the result of (0+...+5)/5=1,this is not the correct result I wanted, the correct result is 5/1=5.
Anyone get good idea? thanks very much.

Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: How to dynamicaly calculate data Averagevalue(AVG Function can't work!)
« Reply #1 on: October 30, 2019, 12:16:28 pm »
If we are speaking about the mathematically true average value it is:
 
considering this when data is 0,0,0,0,5 the correct result is 1 :)

Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: How to dynamicaly calculate data Averagevalue(AVG Function can't work!)
« Reply #2 on: October 30, 2019, 01:00:54 pm »
I thought about your task and found the following solution

If you want to calculate data by yourself you can do this trick:
In the init script define the value, for example, var sumVal = 0; var itemCount = 0
In the item (it is doesn't matter which only one thing is important it must be placed on the band that bound with calculated value)
put next script: $S{if ($D{data.value}!=0) {sumVal += $D{data.value}; itemCount +=1}
then in the footer place item with next script $S{if (itemCount>0) {sumVal / itemCount} else 0}

I hope it will help you :)
« Last Edit: October 30, 2019, 01:05:29 pm by Arin Alex »

showmesky

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How to dynamicaly calculate data Averagevalue(AVG Function can't work!)
« Reply #3 on: October 31, 2019, 10:21:31 am »
Ok!Your method works.  :) Thank you very much

I also agree with your opinion of math AVG 8)

« Last Edit: October 31, 2019, 10:24:43 am by showmesky »