0

I'm using Vega-lite in PowerBI to make a visual. I want to manually set the axis values, based on the result of a measure ( the measure exists in PowerBI and is added to the Deneb integration ). Using the code below but subbing the field out for an integer works perfectly, but then it's static and I need to dynamically determine it. Is using a field in this way not supported by Vegalite, or have I just used incorrect syntax? (Code snippet below is just an extract of the entire code)

"encoding": { "x": { "field": "made up field", "type": "quantitative", "scale": { "nice": false, "domain": [ 0, "Dynamic field measures" ] } } }

I've also tried using a {"field":"Dynamic field measures"} but it really didn't like that! any advice on the correct syntax, or even just a confirmation vegalite doesn't support this functionality would be great, thanks in advance guys!

Edit: More info!

Example Data

So my data is arranged like this. I want to make a chart for each Category but I want all the axis aligned to the largest value, so every chart is using the same scale. I could fix it statically, but users will be able to interact with filters (this sample data is obviously very simplified, the real model has several different dimensions attached) so the "Dynamic Field Measure" needs to be used to fix the max extent of the axis (Additional context, that measure is made in PowerBI and is working fine)

I could mock up some images if that's helpful but it's basically just a set of barcharts with a common fixed axis. Hope this makes my ask more clear.

Andy
  • 1
  • 2
  • Can you clarify the problem a bit more. Show a full sample dataset ideally and desired output? – Davide Bacci Aug 18 '22 at 14:22
  • Sure, I added some sample data to the original post - let me know if anything requires further elucidation – Andy Aug 19 '22 at 10:33
  • You don't need the dynamic field measure. The extent will be calculated automatically from your data and the axes resolution can be shared or independent as you desire. – Davide Bacci Aug 19 '22 at 11:38
  • Sorry, maybe I'm being unclear, I want the axis to end at the maximal value - if the charts calculate automatically then all the axis will be different, which is what I'm trying to avoid. – Andy Aug 19 '22 at 13:28
  • Having said that, I found a workaround (I suspect vegalite doesn't support using a field to specify a domain) so I just layer a tick and coloured it white, and set it to be my dynamic field so I have achieved the effect I wanted, albeit not in the cleanest way, so if there is a way to make that work I'd still appreciate it! – Andy Aug 19 '22 at 13:29
  • Can you paste a picture of your chart so I can see what your desired output looks like? Ideally matching your sample data. – Davide Bacci Aug 19 '22 at 13:34

1 Answers1

1

I have no idea what exactly you are trying to do, but @David Bacci has a great answer about setting a max value that helped me figure out a cool technique that might apply to your question.

This parameter will grab the value of a field named Max in the first row (0 index).

  "data": {"name": "dataset"},
  "params": [
    {
      "name": "upperLimit",
      "expr": "data('dataset')[0]['Max']"
    }
  ],

I can't find anything that documents accessing data this way, but it appears for the expression "data('dataset')[0]['Max']":

Element Description
data() Standard method to access data
dataset Standard name of input data from Power BI/Deneb ("data": {"name": "dataset"}). For Vega outside of Power BI/Deneb, this needs to match the data element name.
[0] Row number. Zero is the first row, one is second row, etc.
['Max'] The column name enclosed in single quotes.

So for your case, just make sure Max matches your column name.

Then add this code to the axis encoding that you want fixed to this upperLimit parameter:

      "scale": {
        "domain": {"expr": "[0,upperLimit]"}

You could also replace the 0 with a lowerLimit parameter.

Here's an entire Deneb spec with highlighting and cross-filtering:

{
  "data": {"name": "dataset"},
  "params": [
    {
      "name": "upperLimit",
      "expr": "data('dataset')[0]['Max']"
    }
  ],
  "layer": [
    {
      "mark": {
        "type": "bar",
        "clip": true,
        "opacity": 0.3,
        "tooltip": true
      },
      "encoding": {
        "x": {
          "field": "Value"
        }
      }
    },
    {
      "mark": {
        "type": "bar",
        "clip": true,
        "tooltip": true
      },
      "encoding": {
        "x": {
          "field": "Value__highlight"
        },
        "opacity": {
          "condition": {
            "test": {
              "field": "__selected__",
              "equal": "off"
            },
            "value": 0
          },
          "value": 1
        }
      }
    }
  ],
  "encoding": {
    "y": {
      "field": "Category",
      "type": "nominal"
    },
    "x": {
      "type": "quantitative",
      "axis": {"title": "Value"},
      "scale": {
        "domain": {"expr": "[0,upperLimit]"}
      }
    }
  }
}
TheRizza
  • 1,577
  • 1
  • 10
  • 23