I’m trying to enforce a specific x-axis sort order in a vega-lite stacked bar chart. I’ve tried a ton of different specifications, but, apparently, I’m missing something each time as the sort order is always alphabetical.
Some of the complexity is due to sourcing x-axis labels from a measure—something that can’t be done in core visuals, and the reason for that is that in real life my x-axis labels are derived by concatenating multiple measure values in a string. I’ve skipped that step in the attached example.
Vega-Lite does not always respect an axis sort order and uses its default of alphabetical. When this impacts me, the workaround I’ve applied successfully is to:
add a [calculate] transform that creates a new field by adding a padded prefix to the field in question
change the axis encoding to use new field
added [axis/labelExpr] to display the new field with the prefix removed
Note that Power BI doesn’t supply a sort order value for any columns that use another field for sorting, so this needs to be manually added to the dataset and this can be used in one of the options above to respect that particular order.