Hello everyone!
I want to let my users display a Speed field: this will be a custom field, of course Speed = Distance / Time.
The problem I have is that some of my records are incomplete (have only either distance or time populated), but those records are still being used.
From what I understand, when I get an aggregate value the aggregation function is always set to sum, and incomplete records are still being put into the sum function (even if the other value is null).
I would like to be able to do both of these:
- Specify I want to use the AVERAGE function instead of sum.
- Exclude incomplete records from the calculation of my calculated field.
Thanks for your input!
public class CalculatedSpeedCalculatedField : CalculatedField
{
private RequiredField distance;
private RequiredField time;
public CalculatedSpeedCalculatedField()
{
this.Name = "CustomCalculatedSpeed";
this.DisplayName = "Custom Calculated Speed";
this.distance = RequiredField.ForProperty("distance");
this.time = RequiredField.ForProperty("time");
}
protected override IEnumerable<RequiredField> RequiredFields()
{
return new List<RequiredField>
{
this.distance,
this.time,
};
}
protected override AggregateValue CalculateValue(IAggregateValues aggregateValues)
{
var aggregateDistance = aggregateValues.GetAggregateValue(this.distance);
var aggregateTime = aggregateValues.GetAggregateValue(this.time);
if (aggregateDistance.IsError())
{
return aggregateDistance;
}
else if (aggregateTime.IsError())
{
return aggregateTime;
}
double dist = aggregateDistance.ConvertOrDefault<double>();
double tim = aggregateTime.ConvertOrDefault<double>();
return new DoubleAggregateValue(dist / tim);
}
}