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
bernate
Helper III
Helper III

Conditionally Format a Card with Variable Hex Codes

Hello, I am trying to conditionally format a card visual to display a color associated with certain rules. I have 2 tables: Location and Training Complete.

 

Location Table:

Location
Alabama
Georgia
Mississippi
Florida

 

Training Complete Table- the table is actually a parameter from the DAX below, but I included example values of what the measures would come out to in column "% Training Complete"

% to Complete = {
    ("% to Complete", NAMEOF('Alabama Complete'[Alabama Training Complete %]), 0),
    ("% to Complete", NAMEOF('Georgia Complete'[Georgia Training Complete %]), 1),
    ("% to Complete", NAMEOF('Mississippi Complete'[Mississippi Training Complete 
    %]),2),
    ("% to Complete", NAMEOF('Florida Complete'[Florida Training Complete %]),3)
% to TrainingTraining FieldsTraining OrderLocation
% to TrainingAlabama Training'[Alabama MTD Training %]0Alabama
% to TrainingGeorgia Training'[Georgia MTD Training %]1Georgia
% to TrainingMississippi Training'[Mississippi MTD Training %]2Mississippi
% to TrainingFlorida Training'[Florida MTD Training %]3

Florida

 

Current Measure Values

Location% Training Complete
Alabama98.2
Georgia99.7
Mississippi95.4
Florida103.6

 

I also have some Conditional Formatting rules for each location based on the % Training Complete that I put into a table along with the HEX codes I need to use.

AlabamaGeorgiaMississippiFlorida

<97% Red #E4080A

<97% Red #E4080A

<95% Orange #FE9900

<97% Red #E4080A

97-103% Green #7DDA5897%-99.9% Yellow #FFDE5995%-98% Light Yellow #FFECA197%-99% Yellow #FFDE59
>103% Blue #5DE2E7>100% Green #7DDA5898-102% Green #7DDA58100-103% Green #7DDA58
  102%-105% Light Blue #98F5F9>103% Blue #5DE2E7
  >105% Dark Blue #060270 

 

Right now, I have a card visual that correctly shows the % Training Complete by Location, so that when I switch a location with the slicer the % also changes. But I would like to add formatting to color code according to the rules above. My difficulty is that each location has different rules and also different colors they wish to use. Is there any way that I can change this card to Dark Blue for Mississippi when it is selected in the slicer, and to change to a different color according to the rules when another location is selected from the slicer?

bernate_0-1753826592400.png

 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @bernate 

Each measure in a field variable needs to be conditionally formatted individually as you select a measure in the slicer. Alternatively, you can write a measure that detects what measure is being selected and return hexadecimal values based on that. Try:

Training MTD % Color =
VAR _TrainingOrder =
    SELECTEDVALUE ( Training[Order] ) -- 0 = Alabama, 1 = Georgia, 2 = Mississippi, 3 = Florida
VAR _MTDMeasure =
    SWITCH (
        _TrainingOrder,
        0, [Alabama MTD Training %],
        1, [Georgia MTD Training %],
        2, [Mississippi MTD Training %],
        3, [Florida MTD Training %]
    )
RETURN
    SWITCH (
        _TrainingOrder,
        0,
            // --- Alabama ---
            SWITCH (
                TRUE (),
                _MTDMeasure < 97, "#E4080A",
                // Red
                _MTDMeasure <= 103, "#7DDA58",
                // Green
                _MTDMeasure > 103, "#5DE2E7" // Blue
            ),
        1,
            // --- Georgia ---
            SWITCH (
                TRUE (),
                _MTDMeasure < 97, "#E4080A",
                // Red
                _MTDMeasure <= 99.9, "#7DDA58",
                // Green
                _MTDMeasure <= 100, "#FFDE59",
                // Yellow
                _MTDMeasure > 100, "#7DDA58" // Green again
            ),
        2,
            // --- Mississippi ---
            SWITCH (
                TRUE (),
                _MTDMeasure < 95, "#FE9900",
                // Orange
                _MTDMeasure < 98, "#FFDE59",
                // Yellow
                _MTDMeasure <= 102, "#7DDA58",
                // Green
                _MTDMeasure <= 105, "#98F5F9",
                // Light Blue
                _MTDMeasure > 105, "#060270" // Dark Blue
            ),
        3,
            // --- Florida ---
            SWITCH (
                TRUE (),
                _MTDMeasure < 97, "#E4080A",
                // Red
                _MTDMeasure <= 99, "#FFDE59",
                // Yellow
                _MTDMeasure <= 103, "#7DDA58",
                // Green
                _MTDMeasure > 103, "#5DE2E7" // Blue
            )
    )




Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

5 REPLIES 5
Khushidesai0109
Super User
Super User

Hello @bernate 

Try this Measure

TrainingColorHex =
VAR SelectedLocation = SELECTEDVALUE('Location Table'[Location])
VAR TrainingComplete = [Training Complete %] -- Your dynamic % measure
RETURN
SWITCH(TRUE(),
SelectedLocation = "Alabama" && TrainingComplete < 97, "#E4080A",
SelectedLocation = "Alabama" && TrainingComplete >= 97 && TrainingComplete <= 103, "#7DDA58",
SelectedLocation = "Alabama" && TrainingComplete > 103, "#5DE2E7",

SelectedLocation = "Georgia" && TrainingComplete < 97, "#E4080A",
SelectedLocation = "Georgia" && TrainingComplete >= 97 && TrainingComplete <= 99.9, "#FFDE59",
SelectedLocation = "Georgia" && TrainingComplete > 99.9, "#7DDA58",

SelectedLocation = "Mississippi" && TrainingComplete < 95, "#FE9900",
SelectedLocation = "Mississippi" && TrainingComplete >= 95 && TrainingComplete < 98, "#FFECA1",
SelectedLocation = "Mississippi" && TrainingComplete >= 98 && TrainingComplete <= 102, "#7DDA58",
SelectedLocation = "Mississippi" && TrainingComplete > 102 && TrainingComplete <= 105, "#98F5F9",
SelectedLocation = "Mississippi" && TrainingComplete > 105, "#060270",

SelectedLocation = "Florida" && TrainingComplete < 97, "#E4080A",
SelectedLocation = "Florida" && TrainingComplete >= 97 && TrainingComplete <= 99, "#FFDE59",
SelectedLocation = "Florida" && TrainingComplete > 99 && TrainingComplete <= 103, "#7DDA58",
SelectedLocation = "Florida" && TrainingComplete > 103, "#5DE2E7",

"#FFFFFF" -- default white color if no match
)
Apply This Measure to the Card's Background or Data Label Color

Proud to be a Super User!!

Thank you for this option, it worked with a normal measure but not with a parameter.

danextian
Super User
Super User

Hi @bernate 

Each measure in a field variable needs to be conditionally formatted individually as you select a measure in the slicer. Alternatively, you can write a measure that detects what measure is being selected and return hexadecimal values based on that. Try:

Training MTD % Color =
VAR _TrainingOrder =
    SELECTEDVALUE ( Training[Order] ) -- 0 = Alabama, 1 = Georgia, 2 = Mississippi, 3 = Florida
VAR _MTDMeasure =
    SWITCH (
        _TrainingOrder,
        0, [Alabama MTD Training %],
        1, [Georgia MTD Training %],
        2, [Mississippi MTD Training %],
        3, [Florida MTD Training %]
    )
RETURN
    SWITCH (
        _TrainingOrder,
        0,
            // --- Alabama ---
            SWITCH (
                TRUE (),
                _MTDMeasure < 97, "#E4080A",
                // Red
                _MTDMeasure <= 103, "#7DDA58",
                // Green
                _MTDMeasure > 103, "#5DE2E7" // Blue
            ),
        1,
            // --- Georgia ---
            SWITCH (
                TRUE (),
                _MTDMeasure < 97, "#E4080A",
                // Red
                _MTDMeasure <= 99.9, "#7DDA58",
                // Green
                _MTDMeasure <= 100, "#FFDE59",
                // Yellow
                _MTDMeasure > 100, "#7DDA58" // Green again
            ),
        2,
            // --- Mississippi ---
            SWITCH (
                TRUE (),
                _MTDMeasure < 95, "#FE9900",
                // Orange
                _MTDMeasure < 98, "#FFDE59",
                // Yellow
                _MTDMeasure <= 102, "#7DDA58",
                // Green
                _MTDMeasure <= 105, "#98F5F9",
                // Light Blue
                _MTDMeasure > 105, "#060270" // Dark Blue
            ),
        3,
            // --- Florida ---
            SWITCH (
                TRUE (),
                _MTDMeasure < 97, "#E4080A",
                // Red
                _MTDMeasure <= 99, "#FFDE59",
                // Yellow
                _MTDMeasure <= 103, "#7DDA58",
                // Green
                _MTDMeasure > 103, "#5DE2E7" // Blue
            )
    )




Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

This worked perfectly, thank you!

amitchandak
Super User
Super User

@bernate , In case you want field parameter to work with conditional formatting use 

Field Parameters- Conditional Formatting: https://amitchandak.medium.com/field-parameters-conditional-formatting-517aacc23fdf

Also, you can use a looped Switch and if 

 

Also note percent is displayed as *100. Measn .25 is displayed as 25% (when you choose format as %) 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

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

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.