powerbivisualizationpowerbi-desktopvega-litedeneb

Sorting does not work after adding a new major model and sub-models


My previous question can be found at Deneb (Vega-lite) - Sorting Axis.

The answer from davidebacci helped me sort the sub-models in my dataset with only one major model. After I've added a new major model and sub-models, the sorting does not work. See screenshot below.

The goal here is the major models display the sum of their sub-models, then under each major model, its sub-models are sorted in descending order.

An idea (or potential solution) that I haven't tried yet is either using "facet" or "repeat". However, before I try it, I still would like to receive expert opinions on whether we can still achieve the sorting using layering.

The PBI file can be found at https://github.com/triCNguyen5/Test/blob/main/sortingAxisLabels.pbix

enter image description here


Solution

  • enter image description here

    {
      "data": {
        "name": "dataset"
      },
      "encoding": {
        "y": {
          "field": "ui_label",
          "type": "nominal",
          "sort": {
            "field": "rank",
            "order": "ascending",
            "op": "min"
          }
        },
        "x": {
          "field": "EventRate",
          "type": "quantitative"
        },
        "text": {
          "field": "EventRate",
          "type": "quantitative"
        }
      },
      "transform": [
        {
          "calculate": "datum.model",
          "as": "major_model"
        },
        {
          "calculate": "format(datum.EventRate + 10000, '.5f') + '|' + datum.ui_label",
          "as": "minor_model"
        },
        {
          "sort": [
            {
              "field": "EventRate",
              "order": "descending"
            }
          ],
          "window": [
            {
              "op": "rank",
              "as": "rank1"
            }
          ],
          "groupby": [
            "model"
          ]
        },
        {
          "joinaggregate": [
            {
              "op": "sum",
              "field": "EventRate",
              "as": "TotalEventRate"
            }
          ],
          "groupby": [
            "model"
          ]
        },
        {
          "sort": [
            {
              "field": "TotalEventRate",
              "order": "descending"
            }
          ],
          "window": [
            {
              "op": "dense_rank",
              "as": "rank"
            }
          ]
        },
        {
          "calculate": "(datum.rank1/10)+datum.rank",
          "as": "rank"
        }
      ],
      "layer": [
        {
          "transform": [
            {
              "aggregate": [
                {
                  "op": "sum",
                  "field": "EventRate",
                  "as": "EventRate"
                },
                {
                  "op": "max",
                  "field": "rank1",
                  "as": "maxRank"
                }
              ],
              "groupby": [
                "model"
              ]
            },
            {
              "sort": [
                {
                  "field": "EventRate",
                  "order": "descending"
                }
              ],
              "window": [
                {
                  "op": "rank",
                  "as": "rank"
                }
              ]
            },
            {
              "calculate": "datum.model",
              "as": "ui_label"
            }
          ],
          "layer": [
            {
              "name": "Major Model Bar",
              "mark": {
                "type": "bar",
                "color": "#004D45",
                "height": 20,
                "tooltip": true
              }
            },
            {
              "name": "Major Model Event Rate Label",
              "mark": {
                "type": "text",
                "align": "left",
                "dx": 5,
                "baseline": "middle"
              }
            }
          ]
        },
        {
          "name": "Minor Model Bar",
          "mark": {
            "type": "bar",
            "color": "#8FC319",
            "height": 15,
            "tooltip": true
          }
        },
        {
          "name": "Minor Model Event Rate Label",
          "mark": {
            "type": "text",
            "align": "left",
            "dx": 5,
            "baseline": "middle"
          }
        }
      ]
    }