powerbivisualizationpowerbi-desktopvega-litedeneb

Deneb (Vega-lite) - Sorting Axis


I am attempting to sort my Y axis, see screenshot below. Basically, I want to sort the Y Axis based on the Even Rate in descending order. The structure of the data is as follows: major model > minor model, major model displays the sum of the Event Rate from the minor models.

I sort of got it to sort, however, the major model label just disappears. the PBI file can be found at https://github.com/triCNguyen5/Test/blob/main/sortingAxisLabels.pbix

Any help or idea is appreciated. This is just a subset of the data, full dataset has different major models and their own minor models.

enter image description here


Solution

  • enter image description here

    {
      "data": {
        "name": "dataset"
      },
      "transform": [
        {
          "sort": [
            {
              "field": "EventRate",
              "order": "descending"
            }
          ],
          "window": [
            {
              "op": "rank",
              "as": "EventRateRank"
            }
          ],
          "groupby": [
            "model"
          ]
        },
        {
          "calculate": "format(datum.EventRateRank, '0') + '|' + datum.ui_label",
          "as": "minor_model"
        }
      ],
      "layer": [
        {
          "name": "Major Model Bar",
          "mark": {
            "type": "bar",
            "color": "#004D45",
            "height": 20,
            "tooltip": true
          },
          "encoding": {
            "y": {
              "field": "model",
              "type": "nominal",
              "title": "Fleet"
            },
            "x": {
              "field": "EventRate",
              "type": "quantitative",
              "aggregate": "sum"
            }
          }
        },
        {
          "name": "Major Model Event Rate Label",
          "mark": {
            "type": "text",
            "align": "left",
            "dx": 5,
            "baseline": "middle"
          },
          "encoding": {
            "y": {
              "field": "model",
              "type": "nominal"
            },
            "x": {
              "field": "EventRate",
              "type": "quantitative",
              "aggregate": "sum"
            },
            "text": {
              "field": "EventRate",
              "type": "quantitative",
              "aggregate": "sum"
            }
          }
        },
        {
          "name": "Minor Model Bar",
          "mark": {
            "type": "bar",
            "color": "#8FC319",
            "height": 15,
            "tooltip": true
          },
          "encoding": {
            "y": {
              "field": "minor_model",
              "type": "ordinal",
              "axis": {
                "labelExpr": "split(datum.label, '|')[1]==null?split(datum.label, '|')[0]:split(datum.label, '|')[1]"
              }
            },
            "x": {
              "field": "EventRate",
              "type": "quantitative",
              "title": "Event Rate"
            }
          }
        },
        {
          "name": "Minor Model Event Rate Label",
          "mark": {
            "type": "text",
            "align": "left",
            "dx": 5,
            "baseline": "middle"
          },
          "encoding": {
            "y": {
              "field": "minor_model",
              "type": "ordinal"
            },
            "x": {
              "field": "EventRate",
              "type": "quantitative"
            },
            "text": {
              "field": "EventRate",
              "type": "quantitative"
            }
          }
        }
      ]
    }