Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to calculate MAX from a dataset column in original Vega

New member
Joined
Feb 3, 2023
Messages
4
I'm working on a Power BI Deneb visual, using original (not Lite) Vega syntax. I'm trying to calculate a maximum value from a column of my dataset, containing only integers. I could do this on Power BI side and pass max as a repeated argument in dataset, but for performance reasons I would like to calculate it on Vega side.

Could someone give me a tip on how to do it? Feels like transform of a formula or aggregate type, but I can't figure it out.

Thats:

That was my first idea, but obviously it sees only row context so it finds max of one value instead of entire column:

Code:
  "data": [
    {
      "name": "dataset",
      "transform": [
        {
          "type": "formula",
          "as": "maxValue",
          "expr": "max(datum['ColumnWhereILookForMax'])"
        }
      ]
    }
 
New member
Joined
Feb 3, 2023
Messages
8
You can either use the joinaggregate transform (https://vega.github.io/vega/docs/transforms/joinaggregate/) or derive a new dataset and use the aggregate transform. e.g.

Code:
{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "A basic bar chart example, with value labels shown upon mouse hover.",
  "width": 400,
  "height": 200,
  "padding": 5,
  "data": [
    {
      "name": "table",
      "values": [
        {"category": "A", "amount": 28},
        {"category": "B", "amount": 55},
        {"category": "C", "amount": 43},
        {"category": "D", "amount": 91},
        {"category": "E", "amount": 81},
        {"category": "F", "amount": 53},
        {"category": "G", "amount": 19},
        {"category": "H", "amount": 87}
      ],
      "transform": [
        {
          "type": "joinaggregate",
          "fields": ["amount"],
          "ops": ["max"],
          "as": ["max"]
        }
      ]
    }
  ],
  "signals": [
    {
      "name": "tooltip",
      "value": {},
      "on": [
        {"events": "rect:mouseover", "update": "datum"},
        {"events": "rect:mouseout", "update": "{}"}
      ]
    }
  ],
  "scales": [
    {
      "name": "xscale",
      "type": "band",
      "domain": {"data": "table", "field": "category"},
      "range": "width",
      "padding": 0.05,
      "round": true
    },
    {
      "name": "yscale",
      "domain": {"data": "table", "field": "amount"},
      "nice": true,
      "range": "height"
    }
  ],
  "axes": [
    {"orient": "bottom", "scale": "xscale"},
    {"orient": "left", "scale": "yscale"}
  ],
  "marks": [
    {
      "type": "rect",
      "from": {"data": "table"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "field": "category"},
          "width": {"scale": "xscale", "band": 1},
          "y": {"scale": "yscale", "field": "amount"},
          "y2": {"scale": "yscale", "value": 0}
        },
        "update": {"fill": {"value": "steelblue"}},
        "hover": {"fill": {"value": "red"}}
      }
    },
    {
      "type": "text",
      "encode": {
        "enter": {
          "align": {"value": "center"},
          "baseline": {"value": "bottom"},
          "fill": {"value": "#333"}
        },
        "update": {
          "x": {"scale": "xscale", "signal": "tooltip.category", "band": 0.5},
          "y": {"scale": "yscale", "signal": "tooltip.amount", "offset": -2},
          "text": {"signal": "tooltip.amount"},
          "fillOpacity": [
            {"test": "datum === tooltip", "value": 0},
            {"value": 1}
          ]
        }
      }
    }
  ]
}

enter image description here
 
Top