☝️Small business or a startup? See if you qualify for our special offer.
+

Issue with Custom Aggregation ("Double Sum") Reverting to Default "Sum"

Answered
Sajjad Ramezani asked 2 days ago

We are currently integrating Flexmonster as our datagrid view component for the Observations module in our application, where pivoting and charting features are essential. While setting up a custom aggregation function, we encountered an issue that we hope you can help us resolve.

Context: We have added a custom aggregation named "double sum" and configured it to be available for number fields. The custom aggregation appears correctly in the list of available aggregations in the UI.

However, when we select "double sum" from the aggregation dropdown:

The selected aggregation automatically switches back to "sum".

No changes are applied, and the custom logic.

 
"use client";
import dynamic from "next/dynamic";

// Wrapper must be imported dynamically, since it contains Flexmonster pivot
const FlexmonsterPivot = dynamic(() => import("@/providers/PivotWrapper"), {
  ssr: false,
  loading: () => <h1>Loading Flexmonster...</h1>,
});

export default function PivotTableDemo() {
  const customAggregations = [
    {
      id: "doubleSum",
      label: "Double Sum",

      measure: (values) => {
        const sum = values.reduce((a, b) => a + b, 0);
        return sum * 2;
      },
      isNumeric: true,
    },
  ];

  return (
    <>
      <div>
        <FlexmonsterPivot
          toolbar={true}
          customAggregations={customAggregations}
          report={{
            dataSource: {
              type: "json",
              data: [
                {
                  id: 1,
                  trial__date: "2024-06-11",
                  trial__country: "West Africa",
                  trial__type: "Screening",
                  trial__status: "Planted",
                  trial__crop_name: "BASIL",
                  trial__protocol: "default",
                  volume_of_seeds: 10,
                  unit: "kg",
                  replication: 1,
                },
                {
                  id: 2,
                  trial__date: "2024-06-11",
                  trial__country: "West Africa",
                  trial__type: "Screening",
                  trial__status: "Planted",
                  trial__crop_name: "BASIL",
                  trial__protocol: "default",
                  volume_of_seeds: 10,
                  unit: "gr",
                  replication: 2,
                },
                {
                  id: 3,
                  trial__date: "2024-06-11",
                  trial__country: "West Africa",
                  trial__type: "Screening",
                  trial__status: "Planted",
                  trial__crop_name: "BASIL",
                  trial__protocol: "default",
                  volume_of_seeds: 10,
                  unit: "gr",
                  replication: 3,
                },
              ],

              mapping: {
                aggregations: {
                  number: ["sum", "average", "doubleSum"],
                  string: ["count"],
                },
              },
            },

            slice: {
              rows: [
                {
                  uniqueName: "trial__crop_name",
                },
              ],
              columns: [
                {
                  uniqueName: "unit",
                },
                {
                  uniqueName: "[Measures]",
                },
              ],
              measures: [
                {
                  uniqueName: "replication",
                },
              ],
            },
          }}
          //licenseKey="XXXX-XXXX-XXXX-XXXX-XXXX"
        />
      </div>
    </>
  );
}

3 answers

Public
Nadia Khodakivska Nadia Khodakivska Flexmonster 1 day ago

Hello,

Thank you for contacting us.

Flexmonster does not provide the customAggregations property. Please check the list of available objectsmethods, and events in our documentation: https://www.flexmonster.com/api/.

To add a "Double sum" field, we recommend using the calculated values feature. For example:

 report: {
// other properties
slice: {
// other properties
measures: [
{
uniqueName: "Double Sum",
caption: "Double Sum",
formula: "sum('Price') * 2"
}
]
}
}

You can find more details about the calculated values at the following link: https://www.flexmonster.com/doc/calculated-values/. Here's an example illustrating the suggested approach: https://jsfiddle.net/flexmonster/uoy9xpea/

Please let us know if it works for you. Looking forward to hearing from you.

Kind regards,
Nadia

Public
Sajjad Ramezani 24 hours ago

Hi Nadia,

Thank you very much for your helpful and prompt response to our ticket — we really appreciate your support.

Could you please clarify or point us more specifically to the part of your website or documentation that explains how to configure aggregations when working with dynamic files? A direct link or example would be greatly appreciated.

https://www.flexmonster.com/doc/support-more-aggregations/#custom-aggs

Thanks again for your time and assistance.

Best regards, Sajjad

Public
Nadia Khodakivska Nadia Khodakivska Flexmonster 4 hours ago

Hi Sajjad,

Thank you for reaching out to us.

Please provide us with more details on your use case:

  1. Do you want to calculate a double sum for different data sources (files)?
  2. Which aggregations do you want to add?
  3. Which data source do you use?

It would greatly help us.

Looking forward to hearing from you.

Kind regards,
Nadia

Please login or Register to Submit Answer