Power BI is well-known for its powerful visualization capabilities, but when you need more flexibility and control, you might want to explore custom visuals. One such tool that offers a ton of customization options is Deneb, a visual that allows you to write Vega-Lite JSON code directly in Power BI.
In this guide, we'll walk you through how to create multiple interactive scatter plots using Deneb and Vega-Lite JSON within Power BI. This approach offers flexibility for more complex scenarios and customization than the out-of-the-box visuals.
To begin, you'll need to install the Deneb custom visual:
Once installed, drag the Deneb visual onto your report canvas.
For this example, let's assume we have a dataset with fields like Sales, Profit, and Product Category. This dataset will be used to create scatter plots showing the relationship between Sales and Profit across different categories.
Make sure your data is clean and ready for visualization. Ideally, you'd have numerical fields for the X and Y axes and a categorical field to color the scatter plot points.
Deneb lets you write Vega-Lite JSON code directly, so we'll start with the basics:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": []
},
"mark": "point",
"encoding": {
"x": {"field": "Sales", "type": "quantitative"},
"y": {"field": "Profit", "type": "quantitative"},
"color": {"field": "Product Category", "type": "nominal"}
}
}
In the JSON above:
x
and y
represent the Sales and Profit fields.color
encoding applies a different color to each Product Category.To link your Power BI data to the Vega-Lite JSON, follow these steps:
"data": {"values": []}
part in the JSON code with "data": {"name": "data"}
to use Power BI data dynamically.Your Vega-Lite code now looks like this:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"name": "data"
},
"mark": "point",
"encoding": {
"x": {"field": "Sales", "type": "quantitative"},
"y": {"field": "Profit", "type": "quantitative"},
"color": {"field": "Product Category", "type": "nominal"}
}
}
To create multiple scatter plots, we’ll use faceting in Vega-Lite. Faceting allows you to split the dataset into multiple subplots based on a field.
In this case, let's facet the data by Region:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"name": "data"
},
"facet": {
"column": {"field": "Region", "type": "nominal"}
},
"spec": {
"mark": "point",
"encoding": {
"x": {"field": "Sales", "type": "quantitative"},
"y": {"field": "Profit", "type": "quantitative"},
"color": {"field": "Product Category", "type": "nominal"}
}
}
}
Here’s what’s happening:
facet
property, with column
specifying that we want to split the data by the Region field, creating a column of scatter plots for each region.One of the key advantages of using Vega-Lite is the ability to add interactions to your visuals. For example, you can enable tooltips and make points clickable:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"name": "data"
},
"facet": {
"column": {"field": "Region", "type": "nominal"}
},
"spec": {
"mark": "point",
"encoding": {
"x": {"field": "Sales", "type": "quantitative"},
"y": {"field": "Profit", "type": "quantitative"},
"color": {"field": "Product Category", "type": "nominal"},
"tooltip": [
{"field": "Sales", "type": "quantitative"},
{"field": "Profit", "type": "quantitative"},
{"field": "Product Category", "type": "nominal"}
]
},
"selection": {
"click": {
"type": "single",
"on": "click",
"encodings": ["color"]
}
}
}
}
Now, when users hover over the points, they’ll see a tooltip with the sales, profit, and product category data. They can also click to select points based on color, allowing for more interactivity.
To ensure that your scatter plots are well-organized, you can adjust the layout to fit your needs. For instance, you can specify how many columns or rows of plots you want to display at a time:
"facet": {
"column": {"field": "Region", "type": "nominal", "spacing": 15}
}
Here, the spacing
property adds some padding between the individual scatter plots to ensure they don’t appear too cluttered.
Once you’ve fine-tuned your Vega-Lite code, save your Power BI report. You can now publish it and share it with others. Users can interact with your scatter plots, explore relationships between variables, and dive deeper into the data.
Using Deneb with Vega-Lite JSON in Power BI gives you unparalleled control over your visualizations. By leveraging these tools, you can create multiple scatter plots that are interactive, informative, and customized to meet specific reporting needs. Whether you’re working with complex data sets or need advanced visual interactions, this method will enhance your Power BI capabilities.
Try it out in your next report and take your visualizations to the next level!