I'm using Vega Lite via Deneb in PowerBI. See attached example that follows the general format/definition of my chart
{
"data": {"name": "dataset"},
"width": 500,
"height": {"step": 15},
"layer":[
{
"name": "BAR",
"mark": {"type": "bar", "size":14},
"transform": [
{
"fold": [
"Good %",
"Bad %"
],
"as": [
"key",
"value"
]
},
{
"calculate": "indexof(['Good %', 'Bad %'], datum.key)",
"as": "order"
}
],
"encoding": {
"y": {
"field": "Question",
"type": "nominal",
"axis": {
"labelAlign": "right",
"labelLimit": 400,
"labelFont": "DIN",
"labelFontSize": 10,
"orient" : "left",
"title": null,
"labelColor": "white",
"minExtent": 400
}
},
"x": {
"field": "value",
"type": "quantitative",
"title": null,
"axis": {"grid": false, "ticks": false, "labels": false}
},
"color":{
"field": "key",
"type" : "nominal",
"legend": null,
"scale": {
"domain": ["Bad %", "Good %"],
"range": ["#DE3E33", "#1AA14D"]
}
},
"order": {"field": "order", "type": "ordinal"}
}
},
{"mark": {"type": "text", "align": "right", "x":-10},
"encoding": {
"y": {"field": "Question", "type": "nominal"},
"color": {
"condition": {"test": "datum['Good %'] >= 0.5", "value": "green"},
"value": "red"
},
"text": {"field": "Question", "type": "nominal"}}
},
{"mark": {"type": "text", "align": "left", "x":2},
"encoding": {
"y": {"field": "Question", "type": "nominal"},
"color": {"value": "white"},
"text": {"aggregate": "sum", "field": "Good %", "type": "quantitative", "format": ".0%"}}
}
]
}
I have two calculated measures that I combine using "fold" to produced a stacked bar. In the enclosed example I want to sort the questions by the "Good %" score. I've tried the "Sort" guidance but the order doesn't change - I suspect it's because the total x value is 100% in all cases.
Is it possible to set the sort order to one of the folded values ("Good %")?
Many thanks in advance!
Does this do what you want? I renamed your measures to Good and Bad to remove the % sign.
{
"data": {
"name": "dataset"
},
"width": 500,
"height": {
"step": 15
},
"encoding": {
"y": {
"field": "Question",
"type": "nominal",
"sort": {
"field": "Good",
"op": "max",
"order": "ascending"
},
"axis": {
"labelAlign": "right",
"labelLimit": 400,
"labelFont": "DIN",
"labelFontSize": 10,
"orient": "left",
"title": null,
"labelColor": "white",
"minExtent": 400
}
}
},
"layer": [
{
"name": "BAR",
"mark": {
"type": "bar",
"size": 14
},
"transform": [
{
"fold": [
"Bad",
"Good"
],
"as": [
"key",
"value"
]
},
{
"calculate": "indexof(['Good %', 'Bad %'], datum.key)",
"as": "order"
}
],
"encoding": {
"x": {
"field": "value",
"type": "quantitative",
"title": null,
"axis": {
"grid": false,
"ticks": false,
"labels": false
}
},
"color": {
"field": "key",
"type": "nominal",
"legend": null,
"scale": {
"domain": [
"Good",
"Bad"
],
"range": [
"#DE3E33",
"#1AA14D"
]
}
},
"order": {
"field": "Bad",
"type": "quantitative",
"sort": "descending"
}
}
},
{
"mark": {
"type": "text",
"align": "right",
"x": -10
},
"encoding": {
"color": {
"condition": {
"test": "datum['Bad'] >= 0.5",
"value": "green"
},
"value": "red"
},
"text": {
"field": "Question",
"type": "nominal"
}
}
},
{
"mark": {
"type": "text",
"align": "left",
"x": 2
},
"encoding": {
"color": {
"value": "white"
},
"text": {
"aggregate": "sum",
"field": "Bad",
"type": "quantitative",
"format": ".0%"
}
}
}
]
}