Author Topic: Pivot Grid - Default Aggregate function for Columns  (Read 3628 times)

mlipham

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 5
    • View Profile
Pivot Grid - Default Aggregate function for Columns
« on: March 20, 2025, 08:50:33 am »
I have a (Pivot) grid with several data types in it – dates, string, and some numbers.


I have been tasked with fixing the behavior where if one drags a date field into the “Aggregates” box (lower right), it automatically assumes “Sum()”.

It can be clicked and changed to a proper aggregate function, like count() or Min/Max, but the default is sum(). As you know, this means the values in the grid appear as NaN or other weird formats.
 

One suggestion I saw was to add summary  type=null to each column definition, which didn't seem to help.


As an alternative, I have some custom .js code that seems to fit the bill – almost. The code below will intercept the ‘drag-in’ and changes it to count() by default – yay! – but the data in the grid still says NaN, as though it ran it as sum(). This leads me to believe I probably need to refresh the grid to register the new change.


So even if I add the refresh, I can’t help wondering – is there a simpler way that I am overlooking? Am I going about this the hard way? Is there a much simpler approach or configuration to achieve the same?


Below please find my code scratchings: (Yes, I also need to add code to handle strings)


( event: pivotCM:  )

Code: [Select]
function (evt, ui) {
   
    //previous code to concat field value to column header

    ...

    //Code to change from sum to count

    ui.CM.forEach(col => {

        if (col.dataType == "date" && col.summary.type == "sum") {

            col.summary.format = "";

            col.summary.type = "count";

        }

        if (col.dataType === "date") {

            if (col.summary.type === "count") {

                col.summary.format = "0";               

            } else {

                col.summary.format = "mm/dd/yy hh:mm:ss";

            }

        }

    });

}


Thanks!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6392
    • View Profile
Re: Pivot Grid - Default Aggregate function for Columns
« Reply #1 on: March 20, 2025, 12:37:59 pm »
I understand that's incorrect behavior of pqgrid pivot to use default aggregate as "sum" for all columns. It would be fixed in upcoming version.

Meanwhile please use this patch to fix it

Code: [Select]
jQuery.paramquery.cToolPanel.prototype.getObj=function(t){var a={},e=this.that;return t.find(".pq-pivot-col").each((function(t,r){var u=r.dataset.di,o=e.getColumn({dataIndx:u}),n=r.getAttribute("type")||o.summaryDefault||e.iGroup.getAggOptions(o.dataType)[0];o.summaryDefault=a[u]=n})),a};

you may remove the custom code in pivotCM event related to it.
« Last Edit: March 20, 2025, 12:39:41 pm by paramvir »

mlipham

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Pivot Grid - Default Aggregate function for Columns
« Reply #2 on: March 21, 2025, 08:08:19 pm »
Awesome - thanks!
I'll keep my eyes peeled for the new version.

mlipham

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Pivot Grid - Default Aggregate function for Columns
« Reply #3 on: April 26, 2025, 01:56:37 am »
The patch works great , but now i have a weird side effect(?).

We have a mix of data types that can be dragged into the aggregate section.
And now with this code - dragging in a string defaults to count() - awesome.
However, if I click on it again to change the aggregate function, 'count' is no longer a choice - it is simply missing.
This means that if I changed to say, 'sum', or 'avg', then wanted to return to 'count', I no longer have that choice in the menu.

Any hints?

thanks again!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6392
    • View Profile
Re: Pivot Grid - Default Aggregate function for Columns
« Reply #4 on: April 27, 2025, 10:18:43 pm »
I don't see the mentioned side effect in this patched example: https://paramquery.com/pro/demos/pivot

Athlete is string datatype column and it defaults to "count" when dropped to aggregates pane, I can still see the count option in the dropdown list of available options for that dataType.

Available options for a dataType in aggregate pane are picked from summaryOptions: https://paramquery.com/pro/api#option-summaryOptions

Have you updated the summaryOptions dynamically somewhere in your code? It could be causing the side effect.

mlipham

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Pivot Grid - Default Aggregate function for Columns
« Reply #5 on: April 28, 2025, 08:09:55 pm »
Yes - you are correct. After further analysis, I understand my ticket a little better - apologies.
And yes, the example behavior seems to match what I am seeing in our development.

Allow me to ask a follow-up.

The example https://paramquery.com/pro/demos/pivot has Gold in the aggregates box (lower right), along with Silver bronze and total.

After re-reading my (internal) request again, I think the question is - why is count not available for the number types?
Is that something I can code to add into the list of aggregate choices for numbers?

Many thanks as always!


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6392
    • View Profile
Re: Pivot Grid - Default Aggregate function for Columns
« Reply #6 on: April 28, 2025, 09:13:52 pm »
count can be added by updating summaryOptions for numbers.

Code: [Select]
summaryOptions: {
number: "avg,max,min,stdev,stdevp,sum,count"
},

mlipham

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Pivot Grid - Default Aggregate function for Columns
« Reply #7 on: April 28, 2025, 09:34:59 pm »
Ah! Lovely - thank you sir!