Author Topic: Group summary that is a formula  (Read 3407 times)

TeeJT

  • Pro Ultimate
  • Jr. Member
  • *
  • Posts: 88
    • View Profile
Group summary that is a formula
« on: July 10, 2018, 09:23:05 am »
I need to have a group summary that is not an aggregate function (which is based on data in that column) but a formula based on data in other columns e.g. val = rd.Freight / rd.Deposit

How can I acheive this functionality?
I am using ParamQuery Pro 3.4

TeeJT

  • Pro Ultimate
  • Jr. Member
  • *
  • Posts: 88
    • View Profile
Re: Group summary that is a formula
« Reply #1 on: July 10, 2018, 12:27:54 pm »
Using this discussion on formula for Grand Summary, I was able to do it from Grand Summary at the bottom of the grid. However I need it for the Group Summary too.
https://paramquery.com/forum/index.php?topic=2122.msg8407

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Group summary that is a formula
« Reply #2 on: July 10, 2018, 07:40:58 pm »
Summary can be applied the same way to formula column as applied to normal column.

Code: [Select]
   {
      dataIndx: 'product',
      formula: function(ui) {
        return ui.rowData.Freight * 3;
      },
      summary: {
        type: 'sum'
      }
   },
« Last Edit: July 10, 2018, 07:43:17 pm by paramquery »

TeeJT

  • Pro Ultimate
  • Jr. Member
  • *
  • Posts: 88
    • View Profile
Re: Group summary that is a formula
« Reply #3 on: July 11, 2018, 05:21:32 am »
What I need in the summary is not a sum but a formula involving the sum of other columns e.g.  sum(ui.rowData.Freight) / sum(ui.rowData.Deposit)

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Group summary that is a formula
« Reply #4 on: July 11, 2018, 09:50:55 am »
In that case column.render can be used.

Code: [Select]
   {
      dataIndx: 'product',
      render: function(ui){
        var rd = ui.rowData;
        if( rd.pq_gsummary )
          return rd.Freight / rd.Deposit
      }
   },

where Freight and Deposit columns have sum summary defined on them.

TeeJT

  • Pro Ultimate
  • Jr. Member
  • *
  • Posts: 88
    • View Profile
Re: Group summary that is a formula
« Reply #5 on: July 11, 2018, 12:20:23 pm »
I have added a column to your demo at https://paramquery.com/pro/demos33/export
I added a column below but the result is empty because I did not define summary for that column. The summary function kicks in for the summary row and overwrites whatever was rendered. If summary is empty list then that cell is empty. If it is a sum, then it will be sum of the column.

         {
            title: "Product",
            width: 120,
            dataIndx: 'Product',
            format: "##,###.00",
            dataType: "float",
            render: function(ui){
               var rd = ui.rowData;
               if( rd.pq_gsummary )
                  return rd.Freight * 2;
            }
         },         

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Group summary that is a formula
« Reply #6 on: July 11, 2018, 01:10:06 pm »
In that demo, summary is displayed in title rows, please try the below changes in column.render.

Code: [Select]
render: function(ui){
   var rd = ui.rowData;
   if( rd.pq_gsummary || rd.pq_gtitle)
  return pq.formatNumber(rd.Freight * 2, ui.column.format);
}

TeeJT

  • Pro Ultimate
  • Jr. Member
  • *
  • Posts: 88
    • View Profile
Re: Group summary that is a formula
« Reply #7 on: July 11, 2018, 03:18:56 pm »
Thank you very much! This was exactly what I needed.