Forum Discussion
Using a Measure as a Legend in a Stacked Column Visual
- 6 months ago
Hi mdm2025
Measures can’t be used as a legend because they don’t exist per row—they only calculate in the filter context. If a chart needs a legend, it needs real, row-level values. So the fix is to materialize the measure’s result in a physical column. That way, each row has a value the chart can actually use. For example:
Sales by Grouping = VAR __TBL = ADDCOLUMNS ( SUMMARIZE ( Sales, Sales[Fruit], Sales[State] ), "@Amt", CALCULATE ( SUM ( Sales[Sales] ) ), "@Grouping", IF ( CALCULATE ( SUM ( Sales[Sales] ) ) >= [Parameter Value], "Yes", "No" ) ) VAR __FILTERED_TBL = FILTER ( __TBL, [@Grouping] IN VALUES ( 'Grouping'[Grouping] ) ) RETURN SUMX ( __FILTERED_TBL, [@Amt] )The @Grouping column in the virtual table contains the “legend” logic but to use it in a visual, it must be materialized via a disconnected table that holds the expected grouping values. The virtual table is then filtered so that @Grouping matches the values in the disconnected table, allowing the measure to behave like it has row-level categories for the legend.
Link to the sample pbix files are in the video description on YouTube - https://www.youtube.com/watch?v=dEuDlcSzk7k
Hi mdm2025 ,
Power BI does not support using a measure in the Legend field of a stacked column chart.
The Legend must contain a column (categorical field) because the visual needs to determine the grouping before the query runs, whereas a measure is calculated after the filter context is applied. This is a limitation of how the semantic model and DAX query engine operate.
Workaround: Use a Disconnected “Legend” Table
Instead of trying to place a measure directly in the Legend, you can create a small helper table that defines the categories you want to display. The measure will then return values based on the selected category.
Step 1 - Create a Static Segmentation Table
Create a disconnected table to act as the Legend:
Segmentation =
DATATABLE(
"Segment", STRING,
{
{"High"},
{"Medium"},
{"Low"}
})
Do not create relationships to other tables. This table is only used to drive the Legend categories.
Step 2 - Make the Measure Respond to the Selected Segment
Rewrite your measure so it evaluates differently depending on which segment is being rendered:
Value by Segment =
VAR _segment = SELECTEDVALUE(Segmentation[Segment])
RETURN
SWITCH(
_segment,
"High",
CALCULATE(
[Your Base Measure],
FILTER(ALLSELECTED(Data), [Your Complex Logic] > 0.8)),
"Medium",
CALCULATE(
[Your Base Measure],
FILTER(ALLSELECTED(Data),
[Your Complex Logic] > 0.5 &&
[Your Complex Logic] <= 0.8)),
"Low",
CALCULATE(
[Your Base Measure],
FILTER(ALLSELECTED(Data), [Your Complex Logic] <= 0.5)
))
This allows the same measure to return different results for each legend category.
Step 3 - Configure the Visual
X-Axis > Your actual dimension column
Legend > Segmentation[Segment]
Values > Value by Segment
Hope this helps.
Warm Regards.
Hi v-echaithra,
Thank you very much for the detailed solution. Will try to use this approach.
But of the top of my head I am thinking that once the Segmentation Column(From the disconnected Static Segmentation Table) will be placed in the Legend field won't it brake the vizual? (since the tables don't have any relathionship).
Also can you elaborate on what the "[Your Base Measure]," should?
Thanks again,
Marius