Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Meagan
Super User
Super User

Deneb Vega-Lite use expression for scale domain?

I have a Deneb visual using Vega-Lite. Right now, the domain is hard coded to 3 values. We need to remove the third value ("Target") based upon another input value called include Target. We need to customize it to hide a value from a visual based upon a condition. There are 4 fields from the dataset used in the visual: Current, Prior Period, Target, Include Target. Include Target returns a 1 if the target should be included or a 0 if the target should be excluded. How do I use an expression to populate the domain shown below? 

"color": {
          "field": "legend_label",
          "type": "nominal",
          "scale": {
            "domain": [
              "Actual",
              "Prior Period",
              "Target"
            ],
            "range": [
              { "expr": "pbiColor(4,.1)" },
              { "expr": "pbiColor(3,.4)" },
              {"expr": "pbiColor(3)"}
            ]
          },
1 ACCEPTED SOLUTION
Meagan
Super User
Super User

Ok, I solved this by using a second inline dataset. Then did a lookup from it to the PBI dataset and then filtered based upon my Include Target value. Then I used legend_label in the domain. 

View solution in original post

6 REPLIES 6
Meagan
Super User
Super User

Ok, I solved this by using a second inline dataset. Then did a lookup from it to the PBI dataset and then filtered based upon my Include Target value. Then I used legend_label in the domain. 

Anonymous
Not applicable

Thank you lbendlin 

Hi, @Meagan 

With support for ternary expressions in vega-lite, I think you can effectively use ternary expressions to complete your judgment, such as this:

datum['Include Target'] == 1 ? 'green' : 'red'

This will determine if datum['Include Target'] is equal to 1, and if green is returned, red is returned. Of course, for further filtering, you can also nest one or more ternary expressions within the ternary expression.

Of course, vega-lite also supports the use of transform calculate, for example:

"transform": [
    {
      "calculate": "datum['Include Target'] == 1 ?'green':'red'",
      "as": "color"
    }
  ]

Pre-filter the data that meets the criteria in the filter:

"transform": [
    {
      "filter": "datum['Include Target'] == 1 || datum['legend_label'] != 'Target'"
    }
  ]

 

 

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Thanks for your suggestion, but it is not helpful. I understand the ternary logic. What I'm asking for is the syntax to return an array instead of a single expression. The filter transform is also not appropriate here. 

lbendlin
Super User
Super User

You should be able to do this via "condition"

 

Condition | Vega-Lite

Yeah, I've read that, but I can't figure out how to pass it an array of two or three objects rather than a single field. Do you have suggested syntax? 

You may be able to achieve this by creating a param. Assuming your column/field called "legend_label" has 4 values, you can use the "data" function to return an array, then when you have this array can "slice" or "pluck" it, then you can write another expression in your scale > domain, for example:

"scale": {"domain": {"expr": "the name of your param after plucking the filed"}}

 

See screenshot below for an example, it's my own dataset.

you can use the "data" function to return an array. You can look at the name of your dataset under the "Data" tab.

Also check out Expressions | Vega, the language reference has a lot of functions that may help you acheive what you need.

 

ponyo_pankun_1-1734722747769.png

 

 

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.