Search code examples
powerbivisualizationpowerbi-desktopvega-litedeneb

Setting X-Axis format dynamically from columnvalue


I´m using the deneb visual for a bar chart. The datastream comes from a switch-measure (the data provided can be percentage oder whole numbers).

Since the I m using a switch measure, PBI formats dont work, so I m tying to format the barchart X-axis dynamically by a provided column (Format_Verteilung), which contains the format.

columnname: "Format_Verteilung" value: ".0%"

I have tried different approaches, but they don't work:

1. "axis":{"format":{"expr":"data('dataset')[0].Format_Verteilung"}}

2. "axis":{"format":{"field":"Format_Verteilung"}}

3. "axis":{"format":"datum.Format_Verteilung"}

samplecode, formating y-axis:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "A simple bar chart with embedded data.",
  "data": {
    "values": [
      {"a": "A", "b": 0.1, "myformat":".0%"}, {"a": "B", "b": 0.3, "myformat":".0%"}, {"a": "C", "b": 0.2, "myformat":".0%"},
      {"a": "D", "b": 0.1, "myformat":".0%"}, {"a": "E", "b": 0.4, "myformat":".0%"}, {"a": "F", "b": 0.9, "myformat":".0%"},
      {"a": "G", "b": 0.2, "myformat":".0%"}, {"a": "H", "b": 0.7, "myformat":".0%"}, {"a": "I", "b": 0.7, "myformat":".0%"}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
    "y": {"field": "b", "type": "quantitative","axis":{"format":{"field":"myformat"}}}
  }
}

Solution

  • enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "description": "A simple bar chart with embedded data.",
      "data": {
        "values": [
          {"a": "A", "b": 0.1, "myformat": ".0%"},
          {"a": "B", "b": 0.3, "myformat": ".0%"},
          {"a": "C", "b": 0.2, "myformat": ".0%"},
          {"a": "D", "b": 0.1, "myformat": ".0%"},
          {"a": "E", "b": 0.4, "myformat": ".0%"},
          {"a": "F", "b": 0.9, "myformat": ".0%"},
          {"a": "G", "b": 0.2, "myformat": ".0%"},
          {"a": "H", "b": 0.7, "myformat": ".0%"},
          {"a": "I", "b": 0.7, "myformat": ".0%"}
        ]
      },
      "mark": "bar",
      "encoding": {
        "x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
        "y": {
          "field": "b",
          "type": "quantitative",
          "axis": {"labelExpr": "format(datum.label,data('source_0')[0]['myformat'])"}
        }
      }
    }