Forum Discussion

mdm2025's avatar
mdm2025
Helper I
6 months ago
Solved

Using a Measure as a Legend in a Stacked Column Visual

Hello everyone,

 

I’m looking for some guidance on creating a stacked column visual where the Legend is driven by a calculated measure.

My goal is to have:

  • An actual data column on the X‑axis
  • A measure on the Y‑axis
  • And a measure‑based breakdown in the Legend

This would allow me to produce a measure‑driven segmentation similar to the example below.

 

As far as I know, the standard stacked column visual does not allow measures in the Legend field, but I’m hoping someone may know of a workaround or a custom visual that supports this.

I considered creating a calculated column that mirrors the logic of the measure and using that in the Legend. However, due to the complexity of the measure (involving several calculated tables and filtering conditions) and the need for the visual to respect visual filters, this approach feels unreliable.

 

Any insights or recommendations would be greatly appreciated.

 

Thank you,
Marius

  • 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 

     

9 Replies

  • 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 

     

    • mdm2025's avatar
      mdm2025
      Helper I

      Thank you for you help. In the end I manage to use a disconnected table with just the possible values placed in the Legend field and a new dedicated complex measure also using the disconnected table. Not exactly the version you offered but the same ideea. 

      All the best! 

  • Hi mdm2025 

    I have not clearly understood what you want to achieve as there is no legend in the image you provided. Can you please provide more insights into what you are trying to get? We might have a solution using a measure as a filter but the complexity is high so we need to be sure we understood what you need. Also, providing the pbix would be good so we immediately apply the fix in pratice to check the outcome vs what you need. You can share via private message or here with a link to a cloud service of some kind (if for privacy both are not an option please considuer giving us some data we can import in power bi to replicate the thing)

     

    Thanks

     

    If this helped, please consider giving kudos and mark as a solution

    @me in replies or I'll lose your thread

    Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

    Consider voting this Power BI idea

    Francesco Bergamaschi

    MBA, M.Eng, M.Econ, Professor of BI

    • mdm2025's avatar
      mdm2025
      Helper I

      Hi FBergamaschi ,

      I basically want to add a measure in the below field of a stacked column chart which is against PowerBI default design so I am asking if there is another way to have a stacked column broken down by a measure within a visual. 

       

      Thank you,

      Marius

  • v-echaithra's avatar
    v-echaithra
    Community Support

    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.

    • mdm2025's avatar
      mdm2025
      Helper I

      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

  • Hi mdm2025 , you could also try to use Parameter option. If you go to Modeling Tab > New Parameter > Fields, you will be able to add your measurements to one Parameter. Later simply add this Parameter to the stacked bar chart to Y axis.

     

     Kind regards,

    Agata

     

    If this post helps, then please consider to Kudos it and Accept as the solution and Kudos to help the other members find it more quickly

  • Hi,

    Share some data to work with and show the expected result.  Share data in a format that can be pasted in an MS Excel file.

  • v-echaithra's avatar
    v-echaithra
    Community Support

    Hi mdm2025 ,

    We’d like to follow up regarding the recent concern. Kindly confirm whether the issue has been resolved, or if further assistance is still required. We are available to support you and are committed to helping you reach a resolution.

    Best Regards,
    Chaithra E.