March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Earlier, when we had a requirement to bring legend and data labels together in a bar chart, we used to look for some custom visuals, but with Measure driven data labels in the May 2023 update, we can achieve this in standard bar charts and line charts.
Here we are going to see how can we show the legend and data labels together in a stacked bar chart but in general
we can follow the same approach for other bar and line charts.
Requirement:
For the below example, we are going to use the sample data available in Power BI Desktop.
To achieve the requirement, first we need to drag and drop the required column to the visual fields pane, here the requirement is to show the sales values for countries split by product. I put all the required fields from the field list to fields pane.
Y-axis : Country
X-axis : Sales
Legend : Product
Now we need to write a measure like below to concatenate the legends and sales together in the data lables (It may vary depending on the requirement),
Sales Label =
--Summing up the sales and format it to millions
VAR _salesMillions =
FORMAT ( SUM ( financials[ Sales] ), "#,,.0" ) & "M"
--concatenating product and sales for the current context
VAR _result = SELECTEDVALUE(financials[Product]) &"("& _salesMillions&")"
RETURN _result
Next naviagate to the data labels option of the visuals and turn it on,
Under the values option we can see custom label,
And the turn on the custom label option, now we can see the option for add data will be enabled.
Then you can add the measure that we have already created for the labels.
If you don't see any labels, try to adjust (decrease/increase) the font size of the data labels and also turn on the overflow text option.
And to see the Totals we can turn on total labels.
Now can see the legends and data labels togehter in the visual.
Hope this article will be helpful. Please let me know your comments and suggestions.
Regards,
Arul
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.