I’m trying to create a highly customized bar chart using the Deneb visual where the Y-axis labels aren't just a single field, but a concatenation of multiple columns (e.g., 'Product Name' + 'Product Code'). I know how to do this in DAX, but I’d prefer to handle it within the Vega-Lite specification to keep the data model clean. Is there a way to use a calculation transform to merge these fields and then bind that new "virtual" column to the axis?
3 answers
In Deneb, the axis itself doesn't have direct "lookup" access to other columns during the rendering phase, but you can solve this easily using a transform. You should use the calculate expression to create a new field. For example: "transform": [{"calculate": "datum['Product Name'] + ' (' + datum['Product Code'] + ')'", "as": "DynamicLabel"}]. Once defined, you simply set the field property of your Y-axis encoding to "DynamicLabel". This creates a seamless, concatenated label that lives only within the visual. It’s an SEO-friendly way to keep your Power BI field list lean while still delivering a data-rich UI for your end users.
That works perfectly for text, but what if I want to format a number from another column into that label string as well? Does Vega-Lite support D3 formatting inside that same calculation string, or do I need to format the number in DAX first
Using calculate is the standard way. Just be careful with sorting; when you create a new "DynamicLabel" field, Power BI might not know the original sort order of your products, so you may need to add a sort property to your encoding.
Great point, Robert. I usually include the original ID or Sort column in the Deneb "Values" bucket even if I don't display it. Then, in the encoding for the axis, I use "sort": {"field": "SortOrderColumn", "order": "ascending"}. This ensures that while the label looks dynamic and custom, the bars still follow the logical business order defined in your data science model.
You can actually do it all in the spec, James! You’ll want to use the format() function within your calculate transform. For instance, datum['Product Name'] + ' - ' + format(datum['Sales'], '$,.0f'). This uses D3-format syntax to turn your sales numbers into a currency string before joining it to the text. Just remember that if you're using the "PBI Format" features in Deneb, those usually apply to the whole axis at once, so sticking to the standard Vega-Lite format() function is better for manual string concatenation like this.