Author Topic: Custom Aggregations  (Read 6156 times)

mercury85

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 58
  • ASP.NET, VB.NET, MVC, JS, C#, VBA, SQL, MDX, DAX
    • View Profile
Custom Aggregations
« on: April 25, 2018, 10:29:23 pm »
Hi everyone,

I was wondering if there was a little more guidance on custom aggregations.

For example lets say we had a 3 tier group, but the lowest group had a couple of children.  I dont want to see all the children summing up to the top, but rather see only 1 of the children totals.  I have included a picture of my thought on this.  Where at the lowest level, I am looking at dirty and clean clothes, but when I roll it up, I only want to see my clothes summing up, not the dirty ones.

Thanks!!!!




paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Custom Aggregations
« Reply #1 on: April 25, 2018, 10:49:27 pm »
Aggregates ( custom and built in ) receive all the children item values as array, they don't carry row information with them, hence not possible to discriminate based on row's other cell values.

https://paramquery.com/pro/api#method-aggregate

mercury85

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 58
  • ASP.NET, VB.NET, MVC, JS, C#, VBA, SQL, MDX, DAX
    • View Profile
Re: Custom Aggregations
« Reply #2 on: April 25, 2018, 11:01:36 pm »
Just to be clear, we are limited to a one dimensional array of numbers associated with the column as the array identifier...  There is no way we can manipulate to a 2 dimensional array, or maybe a concatenated array to somehow throw conditions on the aggregations?

Or is there possibilities of nested grouping and details and pull values up from there?
« Last Edit: April 25, 2018, 11:29:53 pm by mgregory85 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Custom Aggregations
« Reply #3 on: April 26, 2018, 12:14:25 am »
yes aggregates work on 1 dim arrays, but your message reminds me that js formulas combined with aggregates could be used for your purpose.

https://paramquery.com/pro/demos/grid_formula

I'm looking into it and would get back to you.

mercury85

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 58
  • ASP.NET, VB.NET, MVC, JS, C#, VBA, SQL, MDX, DAX
    • View Profile
Re: Custom Aggregations
« Reply #4 on: April 26, 2018, 12:21:47 am »
Thanks for the quick reply, does JS formulas work on hidden columns by any chance, if that were the case, bringing in some more fields wouldn't be too bad.  I am going to look into the nested groupings, curious if we can extract numbers from the nested items.

mercury85

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 58
  • ASP.NET, VB.NET, MVC, JS, C#, VBA, SQL, MDX, DAX
    • View Profile
Re: Custom Aggregations
« Reply #5 on: April 26, 2018, 05:50:01 am »
Hey there,  I was looking at ways to look at values, not sure if this would be similar.  But maybe we can inject these into rows if possible...




var data = grid.options.dataModel.data;

var TargetColumn = "Type",
TargetSummary = "[Total Wow Likes]",
SummaryList = [];

for(var i=0, len = data.length; i<len; i++){
       var rowData = data;         
           if(rowData[TargetColumn] == TargetSummary){         
               
               Object.keys(rowData).forEach(function (key) {
               if (key.match(/C.*/))
               {            
               debugger
               SummaryList.push({"Name": TargetSummary, ID: key, key: rowData[key]});   
               }        
        });
}}   
« Last Edit: April 26, 2018, 06:01:38 am by mgregory85 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Custom Aggregations
« Reply #6 on: April 26, 2018, 10:27:14 am »
JS formulas work on hidden columns too. In this plnkr example I've skipped the freight values from aggregates for customer 'Yvonne Moncada'

https://plnkr.co/edit/2P5W0d3zHfm2ytI9PL0C?p=preview

Code for actual and formula computed column.

Code: [Select]
{
    //actual column.
    hidden: true,
    dataType: "float",
    dataIndx: "Freight"
  }, {
    //computed column.
    title: "Freight",
    summary: {
      type: "sum"
    },
    width: 120,
    format: '$##,###.00',
    dataType: "float",
    dataIndx: "freight"
  },

Code for formula.

Code: [Select]
    formulas: [
      ['freight', function(rd) {
        //conditional.
        return rd.ContactName ==  'Yvonne Moncada'? undefined: rd.Freight;
      }]
    ],

mercury85

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 58
  • ASP.NET, VB.NET, MVC, JS, C#, VBA, SQL, MDX, DAX
    • View Profile
Re: Custom Aggregations
« Reply #7 on: April 26, 2018, 11:05:47 am »
 :'(

I redid what you did and noticed the behavior occurring.  At the child level, the values are removed.  It appears that this solution works but it prevents the values down to the children.  My attempt is to keep the children, and only have the subtotals impacted for totals.


One way I could see this working is where subtotals appear when collapsed, so more so a trigger effect when the rows are collapsed that the children are removed from this method.

Going to fiddle around and create a function on the formula if pq_hidden = true or undefined.  I think that may work here.

Thanks!
« Last Edit: April 26, 2018, 11:17:17 am by mgregory85 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Custom Aggregations
« Reply #8 on: April 26, 2018, 12:25:36 pm »
Please check this updated plnkr. column.render has been added to the computed column to show values ( taken from original column ) at the child level.

https://plnkr.co/edit/2P5W0d3zHfm2ytI9PL0C?p=preview

Code: [Select]
{
    //computed column.
    title: "Freight",
    summary: {
      type: "sum"
    },
    width: 120,
    format: '$##,###.00',
    dataType: "float",
    dataIndx: "freight",
    render: function(ui){
      //debugger;
      var rd = ui.rowData;
      return (rd.ContactName ==  'Yvonne Moncada'?
        pq.formatNumber(rd.Freight, ui.column.format):
        ui.formatVal);
    }
  }

mercury85

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 58
  • ASP.NET, VB.NET, MVC, JS, C#, VBA, SQL, MDX, DAX
    • View Profile
Re: Custom Aggregations
« Reply #9 on: April 26, 2018, 11:47:10 pm »
Thanks for the quick reply there, playing around in plunker, much nicer than fiddle.  I changed things over, and am now playing with the editing portion.  Hopefully that functionality is not lost.

mercury85

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 58
  • ASP.NET, VB.NET, MVC, JS, C#, VBA, SQL, MDX, DAX
    • View Profile
Re: Custom Aggregations
« Reply #10 on: April 27, 2018, 02:49:57 am »
Since we are using a calculated column:  would we be able to edit it just by using the same keys and modifying them..

Following this guide:  https://paramquery.com/demos/crud

Thinking something similar to the bottom:

change: function (evt, ui) {       
       var newKeys = ui.updateList[0].newRow;
       var index = ui.updateList[0].rowIndx;
       changeKeysToUpper(newKeys);
       
      pq.grid("updateRow", {rowIndx: index, data: newKeys});
}

After a couple of hours of failing, I threw the actual column in and hid it.  Once the column was in place, updates are working as needed.

I will post an example of this in a bit.
« Last Edit: April 27, 2018, 04:29:49 am by mgregory85 »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Custom Aggregations
« Reply #11 on: October 18, 2019, 10:00:35 pm »
This is an old post, but just wanted to add that rows information is also available in aggregates since version 6, that helps to simplify the solution to the use case of this thread.

https://paramquery.com/pro/api#method-aggregate
« Last Edit: October 18, 2019, 10:04:20 pm by paramvir »