Exago Logo
Search
Generic filters
Exact matches only

Custom Aggregate Function: AggDistinctAvg()

Review the documentation on Custom Aggregate Functions for full details on developing and implementing this function.

Description Returns the average of only unique values in a series of values.
Arguments
  • dataToAggregate: the value to include the calculation, accepts data fields or cell references. Required.
  • recordLevel: indicates whether to aggregate by recordLevel (true) or entity level (false). Optional, defaults to true if not provided.
Namespaces
  • WebReports.Api.Common
  • WebReports.Api.Custom
  • System.Collections.Generic
References
  • System.Core.dll
Example AggDistinctAvg({OrderDetails.Quantity})

Program Code

public class AggDistinctAvg : ICustomAggregator
{
    HashSet<Decimal> list = new HashSet<Decimal>();
    
	public void AddValue(SessionInfo sessionInfo, object value, params object[] args)
	{
		Decimal curVal;
        
        if (value == null)
            curVal = 0;
        else if (!Decimal.TryParse(value.ToString(), out curVal))
        {
            throw new WrAggregationException(@"Tried to take the average of a set
                                              containing a non-number");
        }
        
        this.list.Add(curVal);
	}

	public object Result(SessionInfo sessionInfo)
	{
        Decimal sum = 0;
        foreach (Decimal val in list)
        {
            sum += val;
        }
        return sum/this.list.Count; // should never be 0
	}
}
Was this article helpful?
0 out of 5 stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
How can we improve this article?
Please submit the reason for your vote so that we can improve the article.
Table of Contents