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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
clarkson106
Frequent Visitor

Line graph keep fixed colours for lines as the lines change dynamically

In a line graph, I am showing the top 3 locations filtered based on another variable called 'location_score'. These top 3 locations are dynamic. That is what comes up as the top 3 locations will vary depending on the variable  'location_score'. In this line graph x-axis is Date and y-axis is location_score. 

 

For each location, there are two lines. So for example the figure legend would look like this: 

Melbourne - line1

Melbourne - line2

Sydney - line1

Sydney - line2

New Castle - line1

New Castle - line2

 

What I want to achieve is different line colours for different location but same line colour for 'line1' and 'line2' in the same location. So for example 

Melbourne - line1 should be blue

Melbourne - line2 should be blue

Sydney - line1 should be green

Sydney - line2 should be green

New Castle - line1 should be red

New Castle - line2 should be red

 

How can this be achieved? Currently, as I refresh the data different colours come up and sometimes both Melbourne and Sydney would have the same colour making it difficult to distinguish which location is which.  

1 REPLY 1
123abc
Community Champion
Community Champion

  1. Create Measures:

    • Create a measure that calculates the top 3 locations based on the "location_score" variable. You can use the RANKX function or similar.
    • Create a measure to determine whether the current data point is "line1" or "line2." You can use an IF statement or a SWITCH function for this.
  2. Color Scales:

    • In Power BI, go to the "Visualizations" pane.
    • Find your line graph visualization, and drag the "Location Name" column into the "Legend" or "Axis" section of the visual.
    • Click on the "Color" property under "Data colors."
    • Create a new measure to define the line color based on the location name. For example, you can use the following DAX formula:

Line Color =
VAR LocationName = [Location Name]
VAR LineType = [Line Type]
RETURN
IF(LineType = "line1",
SWITCH(LocationName,
"Melbourne", RGB(0, 0, 255), // Blue
"Sydney", RGB(0, 128, 0), // Green
"New Castle", RGB(255, 0, 0), // Red
RGB(192, 192, 192) // Default color for other locations
),
[Line Color] // Use the color defined for line1
)

 

    • In the formula above, you can adjust the RGB values and add more locations as needed.
  1. Apply Color Measure:

    • Drag the "Line Color" measure you created in step 3 into the "Color Saturation" well under "Data colors" in the "Visualizations" pane. This will set the line color based on your criteria.
  2. Test and Refresh:

    • Now, when you refresh your data, Power BI will use your custom color measure to assign colors to lines, ensuring that "line1" and "line2" have the same color for the same location.

This approach ensures that the line colors are consistent for "line1" and "line2" within the same location, even as the top 3 locations change dynamically based on "location_score." You can easily customize the color logic by adjusting the DAX formula in the "Line Color" measure to fit your specific requirements.

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Kudoed Authors