Deneb/Vega-Lite can use nested facets to easily compare multiple categories of data. Whereas the small multiples available in native Power BI visuals concatenate charts in a filled grid, nested facets in Deneb/Vega-Lite use a true matrix approach and permits a “ragged-right” rendering which can ease visualization and comparison.
(NOTE: I didn’t quickly find a way to implement a “ragged-right” rendering with native Power BI visuals; I’d be happy to be proven wrong.)
The example presented herein uses the facet view composition technique in Vega-Lite twice (nested) to present column charts of the 2021 Canadian population with provinces and territories on columns and regions on rows. A screen widget was added to effect interactivity and allow the user to select the comparison sex.
This example illustrates a number of Deneb/Vega-Lite features, including:
0 - General:
- a “title” block with subtitle
- a shared “transform” block with:
- a “calculate” transform to determine the display count based on the selected comparison sex
- a “calculate” transform to determine the composite age group label (id:name)
- a shared “params” block with:
- radio buttons to select the comparison sex
- a colour value set based on the selected comparison sex
- a vertical concatentation (vconcat) for the column chart (above) and data source (below)
1 - Column Chart:
- a vertical facet (rows) by region with:
- region sorted by ID
- a “spec” block with:
- a horizontal facet (columns) by geography (province/territory) with:
- geography sorted by ID (provinces east-to-west, territories west-to-east)
- a “spec” block with:
- a “bar” mark using the colour parameter set above, 50% opacity, custom tooltip, and
- an “encoding” block with:
- for the X axis, a label expression to extract the “name” only from the composite bucket label
- a horizontal facet (columns) by geography (province/territory) with:
2 - Data Source:
- a “text” mark with:
- a “calculate” transform to compose an array for the multi-line data source information
Deneb/Vega-Lite JSON Code:
{
"title": {
"anchor": "start",
"align": "left",
"offset": 20,
"text": "Power BI Nested Facets using Deneb",
"font": "Verdana",
"fontSize": 24,
"fontWeight": "bold",
"fontStyle": "normal",
"subtitle": "Canada 2021 Population by Age Group by Province by Region",
"subtitleFont": "Verdana",
"subtitleFontSize": 16,
"subtitleFontWeight": "normal",
"subtitleFontStyle": "italic"
},
"data": {
"name": "dataset"
},
"transform": [
{
"calculate": "_sex == 'Men' ? datum['Population Men'] : _sex == 'Women' ? datum['Population Women'] : 0",
"as": "_population_count"
},
{
"calculate": "toString( datum['Age Group ID'] ) + ':' + datum['Age Group Short']",
"as": "_age_group_id_name"
}
],
"params": [
{
"name": "_sex",
"value": "Men",
"bind": {
"input": "radio",
"options": [
"Men",
"Women"
],
"name": "Sex: "
}
},
{
"name": "_column_colour",
"expr": "_sex == 'Men' ? '#35558A' : _sex == 'Women' ? '#BB2649' : '#FFFFFF'"
}
],
"vconcat": [
{
"name": "COLUMN_CHART",
"facet": {
"row": {
"field": "Region",
"header": {
"title": null,
"labelFont": "Segoe UI",
"labelFontSize": 14,
"labelFontWeight": "bolder"
},
"sort": {
"field": "Region ID",
"order": "ascending"
}
}
},
"spec": {
"facet": {
"column": {
"field": "Geography",
"header": {
"title": null,
"labelFont": "Segoe UI",
"labelFontSize": 14,
"labelFontWeight": "bolder",
"labelPadding": 0
},
"sort": {
"field": "Geography ID",
"order": "ascending"
}
}
},
"spec": {
"width": 340,
"height": 104,
"mark": {
"type": "bar",
"color": {
"expr": "_column_colour"
},
"opacity": 0.5,
"tooltip": true
},
"encoding": {
"x": {
"field": "_age_group_id_name",
"type": "nominal",
"axis": {
"title": null,
"labelExpr": "slice( datum.value, 3, 100 )"
}
},
"y": {
"field": "_population_count",
"type": "quantitative",
"axis": {
"title": null
}
},
"tooltip": [
{
"field": "Age Group",
"type": "nominal"
},
{
"field": "_population_count",
"type": "quantitative",
"formatType": "pbiFormat",
"format": "#,0;#,0",
"title": "Count"
}
]
}
}
}
},
{
"name": "DATA_SOURCE",
"mark": {
"type": "text",
"align": "left",
"xOffset": -74,
"font": "Segoe UI",
"fontSize": 12,
"fontWeight": "lighter",
"fontStyle": "italic",
"color": "#969696"
},
"transform": [
{
"calculate": "['Data Source: Statistics Canada', 'Populations: 2021 Census; https://open.canada.ca/data/en/dataset/750e6035-adf8-4426-966f-4c25b12a999e', 'Regions: https://www12.statcan.gc.ca/census-recensement/2021/ref/dict/tab/index-eng.cfm?ID=t1_8 (modified for illustative purposes)']",
"as": "_text"
}
],
"encoding": {
"text": {
"field": "_text",
"type": "nominal"
}
}
}
]
}
Also included is the development sample PBIX using data from the Canadian population data from the 2021 census conducted by Statistics Canada.
The intent of this example was not to provide a finished visual, but rather to explore the use of the Deneb custom visual and the Vega-Lite language within Power BI and to serve as a starting point for further development.
This example is provided as-is for information purposes only, and its use is solely at the discretion of the end user; no responsibility is assumed by the author.
Greg
Deneb Example - Nested Facets - V4.pbix (4.6 MB)