How can I show the legend and data labels together…


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:

Arul_2-1685354198037.png

 

For the below example, we are going to use the sample data available in Power BI Desktop.

Arul_1-1685353393924.png

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

Arul_3-1685354810238.png

 

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,

 

Arul_4-1685355946341.png

 

Under the values option we can see custom label,

 

Arul_5-1685355985100.png

 

And the turn on the custom label option, now we can see the option for add data will be enabled. 

 

Arul_6-1685356036808.png

 

Then you can add the measure that we have already created for the labels. 

 

Arul_7-1685356296132.png

 

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.

 

Arul_8-1685356508212.png

 

Now can see the legends and data labels togehter in the visual.

 

Arul_9-1685356554666.png

Hope this article will be helpful. Please let me know your comments and suggestions.

Regards,

Arul

Legends and Data Labels Together.pbix



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*