Skip to content

aggregate columns #2951

@vera-atomic

Description

@vera-atomic

Trying to make sense of how I could use expressions to create a custom aggregate cumulative sum column for each of my split_by pivot columns.
I've seen the issue opened here: #2624
And I tried with an example from the library that doesn't have live data:
https://perspective.finos.org/block/?example=editable

This is the starting config object:

{
  "version": "3.4.0",
  "plugin": "Datagrid",
  "plugin_config": {
    "columns": {},
    "edit_mode": "EDIT",
    "scroll_lock": false
  },
  "columns_config": {},
  "settings": true,
  "theme": "Pro Light",
  "title": null,
  "group_by": [
    "Segment"
  ],
  "split_by": [
    "Ship Date"
  ],
  "columns": [
    "Quantity"
  ],
  "filter": [],
  "sort": [],
  "expressions": {},
  "aggregates": {}
}

Then I tried adding the code you've suggested in issue #2624
But adapting it to this example

var cum_quantity := 0;
for (var idx := 0; idx < index(); idx += 1) {
    cum_quantity += vlookup('Quantity', idx);
};

cum_quantity

here's the config post setting up the new column:

{
  "version": "3.4.0",
  "plugin": "Datagrid",
  "plugin_config": {
    "columns": {},
    "edit_mode": "EDIT",
    "scroll_lock": false
  },
  "columns_config": {},
  "settings": true,
  "theme": "Pro Light",
  "title": null,
  "group_by": [
    "Segment"
  ],
  "split_by": [
    "Ship Date"
  ],
  "columns": [
    "Quantity",
    "New Column 1"
  ],
  "filter": [],
  "sort": [],
  "expressions": {
    "New Column 1": "var cum_quantity := 0;\nfor (var idx := 0; idx < index(); idx += 1) {\n    cum_quantity += vlookup('Quantity', idx);\n};\n\ncum_quantity"
  },
  "aggregates": {}
}

But the new column is showing 0 everywhere instead of cumulative sum.
I would have expected the totals row, for first split by and new column 1 to show 5, then for second split by column and new column 1 to show 5 + 11 = 16 etc for subrows too

What am I doing wrong here please?
TIA!

Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions