Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Dynamic Line Color

Hi,

 

I want to show the data points in green if meet target, but the below measure shows all green.

Can anyone help on this?

 

Thanks.

 

Line Color = 
VAR currProduct =
SELECTEDVALUE ( TableB[Product] )
VAR currValue =
CALCULATE (SUMX(TableA, TableA[Rate]),ALLSELECTED(TableB),values(TableB[Product] ) )
RETURN
IF (
currProduct = "A",
IF ( currValue > 0.8, "Green", "Red" ),
IF ( currValue > 0.7, "Green", "Red" )
)

 

 

  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi Anonymous ,

     

    You can follow my method below. I modified your measure.

     

    Line Color = 
    VAR currProduct =
    SELECTEDVALUE ( TableA[Product] )
    VAR currValue =
    CALCULATE (SUMX(TableA, TableA[Rate]),ALLSELECTED(TableA[Product]),values(TableA[Product] ) )
    RETURN
    IF (
    currProduct = "A",
    IF ( currValue > 0.8, "Green", "Red" ),
    IF ( currValue > 0.7, "Green", "Red" )
    )

     

    1. Here is my sample data.

    TableA:

    Product

    Date

    Rate

    A

    8/15/2019

    0.9

    A

    9/15/2019

    0.75

    A

    10/15/2019

    0.45

    B

    11/15/2019

    0.95

    B

    12/15/2019

    0.8

    B

    1/15/2020

    0.7

    A

    2/15/2020

    0.6

    A

    3/15/2020

    0.94

    A

    4/15/2020

    0.91

    B

    5/15/2020

    0.65

    B

    6/15/2020

    0.44

    B

    7/15/2020

    0.75

     

     

    2. First I create a simple column chart:

     

    3. Then under Data colors, set up conditional formatting on the bars:

     

    4. The result is this.

     

    5. Now switch the visual to a line chart.

     

     

    You can check more details from here.

     

     

    Best regards,

    Stephen Tao

     

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

4 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    Anonymous - I am not aware of a way to make line colors in a Line chart visualization dynamic in terms of their color. However, the Power BI team has been adding more and more conditional or formula based formatting to things so it may be coming.

  • Anonymous ,  Assuming you measure is correct. You have to use the same under conditional formatting with field options. But you have to do it on Bar and then make it line.

     

    https://radacad.com/dax-and-conditional-formatting-better-together-find-the-biggest-and-smallest-numbers-in-the-column
    https://docs.microsoft.com/en-us/power-bi/desktop-conditional-table-formatting#color-by-color-values

     

    steps

    example of color measure

    Colour = 
    SWITCH(TRUE(),
    Table[Date] < TODAY(), "red",
    Table[Date] = TODAY(), "orange",
    "green")
    
    Color sales = if(AVERAGE(Sales[Sales Amount])<170,"green","red")
    Color Year = if(FIRSTNONBLANK(Table[Year],2014) <=2016,"lightgreen",if(FIRSTNONBLANK(Table[Year],2014)>2018,"red","yellow"))
    
    Color  = if(FIRSTNONBLANK(Table[Year],2014) <=2016 && AVERAGE(Sales[Sales Amount])<170 
    ,"lightgreen",if(FIRSTNONBLANK(Table[Year],2014)>2018,"red","yellow"))
    Color sales = if([Sales Today] -[sales yesterday]>0,"green","red")
    
    color =
    switch ( true(),
    FIRSTNONBLANK(Table[commodity],"NA") ="commodity1" && sum(Table[Value]) >500,"lightgreen",
    FIRSTNONBLANK(Table[commodity],"NA") ="commodity2" && sum(Table[Value]) >1000,"lightgreen",
    /// Add more conditions
    "red"
    )
    calc

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous ,

       

      You can follow my method below. I modified your measure.

       

      Line Color = 
      VAR currProduct =
      SELECTEDVALUE ( TableA[Product] )
      VAR currValue =
      CALCULATE (SUMX(TableA, TableA[Rate]),ALLSELECTED(TableA[Product]),values(TableA[Product] ) )
      RETURN
      IF (
      currProduct = "A",
      IF ( currValue > 0.8, "Green", "Red" ),
      IF ( currValue > 0.7, "Green", "Red" )
      )

       

      1. Here is my sample data.

      TableA:

      Product

      Date

      Rate

      A

      8/15/2019

      0.9

      A

      9/15/2019

      0.75

      A

      10/15/2019

      0.45

      B

      11/15/2019

      0.95

      B

      12/15/2019

      0.8

      B

      1/15/2020

      0.7

      A

      2/15/2020

      0.6

      A

      3/15/2020

      0.94

      A

      4/15/2020

      0.91

      B

      5/15/2020

      0.65

      B

      6/15/2020

      0.44

      B

      7/15/2020

      0.75

       

       

      2. First I create a simple column chart:

       

      3. Then under Data colors, set up conditional formatting on the bars:

       

      4. The result is this.

       

      5. Now switch the visual to a line chart.

       

       

      You can check more details from here.

       

       

      Best regards,

      Stephen Tao

       

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

      • Drunkmonkey's avatar
        Drunkmonkey
        Frequent Visitor

        First, this doesn't color the line, which was the original request. But more importantly, WHY IN THE NAME OF ALL THAT'S HOLY should it be necessary to go through this dog and pony show just to color a series of data points?