Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
LillyLegenda
Helper III
Helper III

Coloring Bar chart dynamically by value of x-axis

Hello, I would like to color the bars in my bar chart accoring to the year on the x-axis:

The largest year (Max Year) should be red, second largest blue (Max Year -1) , third largest yellow (Max Year -2).

LillyLegenda_1-1675849134329.png

I am using a measure to calculate the colors which is working when I write down the years in the code but I would like to do it dynamically.
This is working: 

 

 

Year Colour = 
VAR MaxYear = MAX('Date'[Year])

VAR Color =
SWITCH (
    True(),
        MaxYear= 2023, "red",
        MaxYear=2022, "blue", 
        MaxYear=2021, "yellow"
)
Return
color

 

 

But when I change it work dynamically all bars have the same color:

 

 

Year Colour = 
VAR MaxYear = MAX('Date'[Year])
VAR MaxYearColour = MAX('Date'[Year])

VAR Color =
SWITCH (
    True(),
        MaxYear= MaxYearColour, "red",
        MaxYear=2022, "blue", 
        MaxYear=2021, "yellow"
)
Return
color

 

 

LillyLegenda_2-1675849362921.png

Thank you very much for your help 🙂

 

1 ACCEPTED SOLUTION
PaulDBrown
Community Champion
Community Champion

Try:

 

Bar Colour =
VAR _YR =
    MAXX ( ALLSELECTED ( 'Date'[Year] ), 'Date'[Year] )
VAR _YR1 =
    MAXX ( EXCEPT ( ALLSELECTED ( 'Date'[Year] ), { _YR } ), 'Date'[Year] )
VAR _YR2 =
    MAXX ( EXCEPT ( ALLSELECTED ( 'Date'[Year] ), { _YR, _YR1 } ), 'Date'[Year] )
RETURN
    SWITCH (
        SELECTEDVALUE ( 'Date'[Year] ),
        _YR, "Red",
        _YR1, "Blue",
        _YR2, "Yellow"
    )

colour.png

 

 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

2 REPLIES 2
PaulDBrown
Community Champion
Community Champion

Try:

 

Bar Colour =
VAR _YR =
    MAXX ( ALLSELECTED ( 'Date'[Year] ), 'Date'[Year] )
VAR _YR1 =
    MAXX ( EXCEPT ( ALLSELECTED ( 'Date'[Year] ), { _YR } ), 'Date'[Year] )
VAR _YR2 =
    MAXX ( EXCEPT ( ALLSELECTED ( 'Date'[Year] ), { _YR, _YR1 } ), 'Date'[Year] )
RETURN
    SWITCH (
        SELECTEDVALUE ( 'Date'[Year] ),
        _YR, "Red",
        _YR1, "Blue",
        _YR2, "Yellow"
    )

colour.png

 

 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Amazing it is working! Thank you very much!

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors