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

Prepping for a Fabric certification exam? Join us for a live prep session with exam experts to learn how to pass the exam. Register now.

Reply
manialla
Frequent Visitor

Dynamically Highlight Selected City in Line Graph Using DAX for Conditional Formatting

Hi everyone,

I'm working on a Power BI report using a line graph visual, and I'm using two tables:

  • Table1: Contains Area and City columns — used as slicers.
  • Table2: Contains City, Area, Date, and a measure used in the line chart.

Both tables are connected via the Area column.

My line chart is set up like this:

  • X-axis: Calendar/Date from Table2
  • Y-axis: A measure from Table2
  • Legend: City from Table2

What I'm trying to achieve:

When I select an Area from the slicer (Table1), the line chart correctly shows all cities in that Area — which is what I want, so I can compare their performance over time.

But when I select a specific City from Table1, I want the line representing that city to automatically turn black in the graph, while all other cities from that area stay visible but in Regular colours . This would help visually highlight the selected city without removing the others from view.

Current workaround:

Right now, I’m manually going into the Format pane > Data colors and setting the selected city's color to black — but I’d like to automate this using a DAX measure and conditional formatting.

My goal:

Use a DAX measure that checks if the current city in the chart matches the selected city in the slicer and changes its color accordingly (black if selected, while still showing all cities from the selected Area.

Has anyone done something similar or found a reliable way to implement this? Would really appreciate any suggestions or workarounds!

Thanks so much!

 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @manialla 

 

What you're trying to do requires conditional formatting on legends which currently isn't supported. There is a workaround which may work for a few number of items or more (if you have the patience to manually set the legend color). This requires using two disconnected tables to return the category value/legends.

 

Below is a sample measure that references two disconnected tables.

Value2 = 
VAR _value =
    CALCULATE (
        [Sum of Value],
        TREATAS ( VALUES ( City02[City] ), 'Table'[City] ),
        TREATAS ( VALUES ( City01[Area] ), 'Table'[Area] )
    )
VAR _value2 =
    CALCULATE ( [Sum of Value], TREATAS ( VALUES ( City02[City] ), 'Table'[City] ) )
VAR _areaCheck =
    ISFILTERED ( City01[Area] )
VAR _selected =
    IF (
        SELECTEDVALUE ( City02[Category] ) = "selected",
        IF ( NOT _areaCheck, _value2, _value )
    )
VAR _unselected =
    IF (
        SELECTEDVALUE ( City02[Category] ) = "unselected",
        IF ( NOT _areaCheck, _value2, _value )
    )
RETURN
    IF (
        NOT ( HASONEVALUE ( City01[City] ) ) || NOT ( ISFILTERED ( City01[City] ) ),
        _unselected,
        IF (
            SELECTEDVALUE ( City02[City] ) IN VALUES ( City01[City] ),
            _selected,
            _unselected
        )
    )

danextian_0-1746793515191.gif

 

 Please see the attached sample pbix.

 

 





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

3 REPLIES 3
danextian
Super User
Super User

Hi @manialla 

 

What you're trying to do requires conditional formatting on legends which currently isn't supported. There is a workaround which may work for a few number of items or more (if you have the patience to manually set the legend color). This requires using two disconnected tables to return the category value/legends.

 

Below is a sample measure that references two disconnected tables.

Value2 = 
VAR _value =
    CALCULATE (
        [Sum of Value],
        TREATAS ( VALUES ( City02[City] ), 'Table'[City] ),
        TREATAS ( VALUES ( City01[Area] ), 'Table'[Area] )
    )
VAR _value2 =
    CALCULATE ( [Sum of Value], TREATAS ( VALUES ( City02[City] ), 'Table'[City] ) )
VAR _areaCheck =
    ISFILTERED ( City01[Area] )
VAR _selected =
    IF (
        SELECTEDVALUE ( City02[Category] ) = "selected",
        IF ( NOT _areaCheck, _value2, _value )
    )
VAR _unselected =
    IF (
        SELECTEDVALUE ( City02[Category] ) = "unselected",
        IF ( NOT _areaCheck, _value2, _value )
    )
RETURN
    IF (
        NOT ( HASONEVALUE ( City01[City] ) ) || NOT ( ISFILTERED ( City01[City] ) ),
        _unselected,
        IF (
            SELECTEDVALUE ( City02[City] ) IN VALUES ( City01[City] ),
            _selected,
            _unselected
        )
    )

danextian_0-1746793515191.gif

 

 Please see the attached sample pbix.

 

 





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.
pankajnamekar25
Solution Sage
Solution Sage

Hello @manialla 

 

City Line Color =

VAR SelectedCity = SELECTEDVALUE('Table1'[City])

VAR CurrentCity = SELECTEDVALUE('Table2'[City])

RETURN

IF (

    SelectedCity = CurrentCity,

    "#000000",      -- black

    "#C0C0C0"       -- gray or any default color

)

 

Create column chart and use above measure to CF

Then switch to Line chart

 

Thanks,
 Pankaj Namekar | LinkedIn

If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.

Ashish_Excel
Helper IV
Helper IV

Hi,

Share the download link of the PBI file.

Helpful resources

Announcements
May PBI 25 Carousel

Power BI Monthly Update - May 2025

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

May 2025 Monthly Update

Fabric Community Update - May 2025

Find out what's new and trending in the Fabric community.