Software Development

Can you dynamically build axis labels using values from other columns in a Deneb Vega-Lite chart?

RO Asked by Robert Miller · 14-04-2024
0 upvotes 4,137 views 0 comments
The question

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

0
NA
Answered on 16-04-2024

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.

0
JA
Answered on 19-04-2024

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

ST 22-04-2024

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.

0
DA
Answered on 25-04-2024

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.

NA 28-04-2024

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.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session