Forum Discussion

powerbiexpert22's avatar
powerbiexpert22
Icon for Impactful Individual rankImpactful Individual
3 days ago
Solved

Dynamic legends

i want to change the display name for below labels in my chart dynamically based on user selection, example if user select USD currency then labels should show as CY Actuals USD , if EUR then it should display CY Actuals EUR. I am using measure to display in chart which dynamically using SWITCH condition. i do not want to use parameters since it does not allow me to use dynamic format string which is only applicable for measures

 

 

  • Hi powerbiexpert22​,

    You can use a calculation group to dynamically change the measure format, but for the legend labels themselves, Power BI doesn't natively support changing measure names dynamically through DAX.

    One workaround is to use a field parameter, but since you don't want to use parameters, you could try using a disconnected currency table and a SWITCH() measure to control which currency-specific measures are displayed.

    However, this won't dynamically rename the legend entries in a native chart. If changing the legend text is a strict requirement, you'd need to consider a custom visual that supports dynamic series names or redesign the visual to use a dynamic category column for the legend.

3 Replies

  • Hi powerbiexpert22​,

    You can use a calculation group to dynamically change the measure format, but for the legend labels themselves, Power BI doesn't natively support changing measure names dynamically through DAX.

    One workaround is to use a field parameter, but since you don't want to use parameters, you could try using a disconnected currency table and a SWITCH() measure to control which currency-specific measures are displayed.

    However, this won't dynamically rename the legend entries in a native chart. If changing the legend text is a strict requirement, you'd need to consider a custom visual that supports dynamic series names or redesign the visual to use a dynamic category column for the legend.

  • jubinsoni's avatar
    jubinsoni
    Icon for Continued Contributor rankContinued Contributor

    Hi powerbiexpert22​,

    Building on the reply above: legend entries for measures are always the measure (or visual-renamed) name, so DAX can't change them. In practice there are two workarounds that keep your dynamic format string.

    1. Turn the series into a category (works well for line/column charts). The legend then shows column values, which can include the currency:

    Series =
    DATATABLE (
        "Series", STRING, "Metric", STRING, "Currency", STRING, "Order", INTEGER,
        {
            { "CY Actuals USD", "CY",   "USD", 1 }, { "PY Actuals USD", "PY",   "USD", 2 },
            { "CY Plan USD",    "PLAN", "USD", 3 },
            { "CY Actuals EUR", "CY",   "EUR", 1 }, { "PY Actuals EUR", "PY",   "EUR", 2 },
            { "CY Plan EUR",    "PLAN", "EUR", 3 }
        }
    )
    
    Chart Value =
    VAR m   = SELECTEDVALUE ( Series[Metric] )
    VAR cur = SELECTEDVALUE ( Series[Currency] )
    RETURN
        SWITCH ( m & "|" & cur,
            "CY|USD",   [PCD YTD Actuals USD],
            "PY|USD",   [PY YTD Actuals USD],
            "PLAN|USD", [CY Plan USD],
            "CY|EUR",   [PCD YTD Actuals EUR],
            "PY|EUR",   [PY YTD Actuals EUR],
            "PLAN|EUR", [CY Plan EUR]
        )
    currency slicer -> on Series[Currency] (or a Currency dim related to it)
    chart: X = month, Y = [Chart Value], Legend = Series[Series] (sort by Order)
    [Chart Value] keeps your dynamic format string (it's a normal measure)

    Caveat: in a combo chart only the column values take a legend category. Lines need separate measures, so this pattern fits best when all three series use the same chart type (e.g. a line chart).

    2. Keep your combo chart and build the legend yourself. Turn the built-in legend off and place a small card or text box above the chart, driven by a measure:

    Legend Text =
    VAR cur = SELECTEDVALUE ( Currency[Code], "USD" )
    RETURN
        "● CY Actuals " & cur & "     ● PY Actuals " & cur & "     ● CY Plan " & cur

    Colour each "●" in the text box to match the series colours (or use three cards with conditional font colour). It's a bit manual, but the chart stays exactly as it is.

    So my overall approach would be:

    same chart type for all series -> Series category table + one SWITCH measure
    combo chart (bars + lines)     -> hide legend, dynamic legend text measure
    both keep the dynamic format string on the measures

    AI-assisted drafting: AI was used to help structure and phrase this response. I reviewed and validated the technical content before posting.

  • v-aatheeque's avatar
    v-aatheeque
    Icon for Community Support rankCommunity Support

    Hi powerbiexpert22​ 

    Following up to confirm if the earlier responses addressed your query. If not, please share your questions and we’ll assist further.