RadPivotGrid: calculated field exclude incomplete records

2 Answers 24 Views
PivotGrid
Oscar
Top achievements
Rank 1
Iron
Oscar asked on 06 Mar 2025, 04:22 PM

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:

  1. Specify I want to use the AVERAGE function instead of sum.
  2. 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);
    }
}

2 Answers, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 11 Mar 2025, 02:41 PM

Hello Oscar,

If I correctly understand these two requirements, you would like to change the aggregation function to average instead of sum, as well as ignore values when they are, for example, null (incomplete record)?

If the above scenario is indeed the case on your side, about the aggregate function, you can set the AggregateFunction property to Average on the description, on which the CalculatedField will be used. For the part about ignoring incomplete records, you could set the IgnoreNullValues property to True on the description, on which the CalculatedField will be used.

The following code snippet shows these suggestion's implementation:

<pivot:LocalDataSourceProvider.AggregateDescriptions>
    <pivot:PropertyAggregateDescription PropertyName="ExtendedPrice" AggregateFunction="Average" IgnoreNullValues="True"  />    <pivot:CalculatedAggregateDescription CalculatedFieldName="Commission" />
</pivot:LocalDataSourceProvider.AggregateDescriptions>

The calculated field will use the value of the ExtendedPrice property.

If these suggestions do not target the desired behavior, could you share a bit more information about these requirements?

Regards,
Stenly
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

0
Oscar
Top achievements
Rank 1
Iron
answered on 11 Mar 2025, 04:27 PM

Hello Stenly! It seems I can't get this to work.

What I'm looking for is this: given the following data, I would like to get the calculated speed field to ignore the third record (since it does not have Time property).

I would also like to set this from c# code, if possible.


            <x:Array x:Key="SampleData" Type="{x:Type local:myClass}">
                <local:myClass
                    Id="1"
                    Time="10"
                    Distance="100" />
                <local:myClass
                    Id="2"
                    Time="20"
                    Distance="200" />
                <local:myClass
                    Id="3"
                    Distance="1000" />
            </x:Array>

Stenly
Telerik team
commented on 12 Mar 2025, 03:28 PM

Hello Oscar,

I am not sure that I fully understand the setup on your end, so, before continuing, may I ask if it would be possible to modify the attached sample project to showcase a base version of your setup? This way I can review it on my end and see if I can provide suggestions for your requirements.

Tags
PivotGrid
Asked by
Oscar
Top achievements
Rank 1
Iron
Answers by
Stenly
Telerik team
Oscar
Top achievements
Rank 1
Iron
Share this question
or