Forum Discussion

tarunbisht20001's avatar
tarunbisht20001
Frequent Visitor
1 year ago

Need help!!!!!

I have this measure as below.

StackedBarChart =
VAR BarHeight = 80  
VAR MaxWidth = 600
VAR BarY = 50
VAR FontSize = 43
VAR FontWeight = "bold"  
VAR MinWidth = MaxWidth * 0.15  
VAR Color1 = "#FF0000"  
VAR Color2 = "#FFC107"  
VAR Color3 = "#4CAF50"
VAR TextColor = "#000000"
VAR TotalValue = [Red Count] + [Amber Count] + [Green Count]
VAR Width1 = MAX(DIVIDE([Red Count], TotalValue, 0) * MaxWidth, MinWidth)
VAR Width2 = MAX(DIVIDE([Amber Count], TotalValue, 0) * MaxWidth, MinWidth)
VAR Width3 = MAX(DIVIDE([Green Count], TotalValue, 0) * MaxWidth, MinWidth)
VAR CountStr1 = IF([Red Count] = 0, "", FORMAT([Red Count], "#,0"))
VAR CountStr2 = IF([Amber Count] = 0, "", FORMAT([Amber Count], "#,0"))
VAR CountStr3 = IF([Green Count] = 0, "", FORMAT([Green Count], "#,0"))
VAR XPos1 = 0
VAR XPos2 = IF([Red Count] > 0, XPos1 + Width1, 0)
VAR XPos3 = IF([Amber Count] > 0, XPos2 + Width2, IF([Red Count] > 0, XPos1 + Width1, 0))
VAR SVG =
    "data&colon;image/svg+xml;utf8,<svg width='700' height='200' xmlns='http://www.w3.org/2000/svg'>" &
    IF([Red Count] > 0,
        "<rect width='" & Width1 & "' height='" & BarHeight & "' x='" & XPos1 & "' y='" & BarY & "' fill='" & Color1 & "' />" &
        "<text x='" & (XPos1 + Width1 / 2) & "' y='" & (BarY + BarHeight / 2 + FontSize / 10) & "' font-family='Arial' font-size='" & FontSize & "' font-weight='" & FontWeight & "' fill='" & TextColor & "' text-anchor='middle'>" & CountStr1 & "</text>",
        ""
    ) &
    IF([Amber Count] > 0,
        "<rect width='" & Width2 & "' height='" & BarHeight & "' x='" & XPos2 & "' y='" & BarY & "' fill='" & Color2 & "' />" &
        "<text x='" & (XPos2 + Width2 / 2) & "' y='" & (BarY + BarHeight / 2 + FontSize / 10) & "' font-family='Arial' font-size='" & FontSize & "' font-weight='" & FontWeight & "' fill='" & TextColor & "' text-anchor='middle'>" & CountStr2 & "</text>",
        ""
    ) &
    IF([Green Count] > 0,
        "<rect width='" & Width3 & "' height='" & BarHeight & "' x='" & XPos3 & "' y='" & BarY & "' fill='" & Color3 & "' />" &
        "<text x='" & (XPos3 + Width3 / 2) & "' y='" & (BarY + BarHeight / 2 + FontSize / 10) & "' font-family='Arial' font-size='" & FontSize & "' font-weight='" & FontWeight & "' fill='" & TextColor & "' text-anchor='middle'>" & CountStr3 & "</text>",
        ""
    ) &
    "</svg>"
RETURN
    SVG

I am using this major in matrix visual with other column, problem i am facing is this measure is causing bypasing of the selection in slicer, there is no error or issue in the red, amber and green count measure as i return either of them instead of svg measure repspects the selection in slicers.

I tried making a calculated column but that is not giving me required image, even tho it resolved the bypassing issue.
 

2 Replies

  • I have a doubt that your measure is not respecting the filtering context and  accidentally resetting or overriding it.

     

    For example, when calculating Red Count, Amber Count, and Green Count, ensure that they properly respect the filter context:

    VAR RedCount = CALCULATE([Red Count], KEEPFILTERS(YourTable))
    
    VAR AmberCount = CALCULATE([Amber Count], KEEPFILTERS(YourTable))
    
    VAR GreenCount = CALCULATE([Green Count], KEEPFILTERS(YourTable))

     

     

    Since the measure is being used in a matrix visual, try explicitly defining how the matrix should interact with other visuals. 

    You can manage the filtering behavior under the "Edit Interactions" options in Power BI. 

    Since you mentioned that using a calculated column resolves the bypassing issue but doesn't display the image correctly, this suggests the dynamic nature of the measure might be part of the problem. While calculated columns lack flexibility in filtering, a workaround could be to precalculate values or store them in a way that still allows for dynamic rendering without bypassing.

     

    Alternatively, you can create calculated columns for Red Count, Amber Count, and Green Count and a measure for the SVG part, keeping it separate from the counts themselves.

     

    • tarunbisht20001's avatar
      tarunbisht20001
      Frequent Visitor

      Red Count, Amber Count, and Green Count are not causing the issue actually, when i return total count or either of them instead of SVG it do not causes the issue but when i return any static valu like max width,SVG and all it causes the bypassing.