LimeReport Forum

General Category | Основное => Discussion | Обсуждение => Topic started by: showmesky on October 30, 2019, 11:35:23 AM

Title: How to dynamicaly calculate data Averagevalue(AVG Function can't work!)
Post by: showmesky on October 30, 2019, 11:35:23 AM
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.
Title: Re: How to dynamicaly calculate data Averagevalue(AVG Function can't work!)
Post by: Arin Alex on October 30, 2019, 12:16:28 PM
If we are speaking about the mathematically true average value it is:
 (https://wikimedia.org/api/rest_v1/media/math/render/svg/52a4490ec47192d3f77fbdf9bc8eddacdf2631f2)
considering this when data is 0,0,0,0,5 the correct result is 1 :)
Title: Re: How to dynamicaly calculate data Averagevalue(AVG Function can't work!)
Post by: Arin Alex 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 :)
Title: Re: How to dynamicaly calculate data Averagevalue(AVG Function can't work!)
Post by: showmesky 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)