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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

Dynamically changing the colors of lines in line chart

Hello,
I have the following data:

Index allChannelIndex By SensorSensorNumValue
0Detector_10Sensor_118
1Detector_11Sensor_197
2Detector_12Sensor_117
3Detector_13Sensor_114
4Detector_14Sensor_167
5Detector_10Sensor_254
6Detector_11Sensor_23
7Detector_12Sensor_243
8Detector_13Sensor_236
9Detector_14Sensor_20
10Detector_10Sensor_357
11Detector_11Sensor_346
12Detector_12Sensor_36
13Detector_13Sensor_336
14Detector_14Sensor_35
0Detector_20Sensor_111
1Detector_21Sensor_146
2Detector_22Sensor_151
3Detector_23Sensor_196
4Detector_24Sensor_151
5Detector_20Sensor_238
6Detector_21Sensor_280
7Detector_22Sensor_214
8Detector_23Sensor_250
9Detector_24Sensor_252
10Detector_20Sensor_333
11Detector_21Sensor_39
12Detector_22Sensor_359
13Detector_23Sensor_389
14Detector_24Sensor_317



The data above is dummy data and will not result in the same graph below. I will plot a line graph that filter by channel and also filter by SensorNum. In my graph below, the x axis an Index By Sensor and y axis is a measure based on the Value column and legend is SensorNum.

sarah931_0-1702653300371.png


My goal is to change the color of the lines based on which group they belong to. For exmaple lines for Sensor1&2&3 will be all in blue (any color as long as it's the same color) and Sensors 4&5 will be in red and so on.

What could be the way to acheive this? Thank you.

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

This is a complex question because you cannot have legend and more than one measure with conditional formatting.

 

Believe that the solution goes by adding a new table with the combination of sensors and groups something similar to this:

MFelix_0-1703245381583.png

 

Now add the following measure:

Values Sensor Group = CALCULATE(SUM('Readings'[Value]), 'Readings'[Group] in VALUES('Sensor_Group'[Group]) && 'Readings'[SensorNum] in VALUES('Sensor_Group'[SensorNum]), VALUES('Readings'[Group]))

Now do you chart with the following format:

X-Axis: Index By Sensor

Y-Axis: Values Sensor (measure created previously)

Legend: GroupSensor (column from the new table)

 

Now manually change the color for each group, and probably add the series label:

 

MFelix_1-1703245608631.png

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

5 REPLIES 5
MFelix
Super User
Super User

Hi @Anonymous ,

 

If you have a group that allows you to have the sensor aggregated create a measure similar to this one:

Color values =
SWITCH(SELECTEDVALUE(Table[Group]),
"Group 1", "Blue",
"Group 2", "Green",
"Group 3", "Yellow",
...
"Group N", "Red")

 

Then use this measure to colour your lines condittionaly


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Anonymous
Not applicable

Thank you for your reply. Do you mean I should be using the measure as a legend? Can you provide an example please?

Hi @Anonymous ,

 

Sorry for missleading you, since you have a legend this is not as direct as I referenced, I apologize.

 

If you are using the Sensor as legend then you aren't abble to have different colour per group since the context of each line will be based on the value of the sensor so if you have sensor 1 for two groups whitin the line chart the value consider is only of the sensor not of the sensor + group.

 

Confirm if I'm correct you want to have for example

Sensor 1 (Group1)- Blue 

Sensor 1 (Group2) - Green

 

However the Group is not a part of your chart directly?

 

How many sensors do you have and are they going to change, meaning increase the number of sensors?

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Anonymous
Not applicable

Hey @MFelix , thank you. No need for an apology. So I can add the grouping, meaning I can add a table that says to which group each sensor belong instead of the filters if that would makes it easiter to solve.

Sensor 1Group 1
Sensor 2Group 1
Sensor 3Group 2


My goal now will be to make lines of the sensors of the same group to have the same color in the graph, so sensor 1& 2 will be red and sensor 2 will be yellow.

To answer you question, yes sensor numbers could vary.

Hi @Anonymous ,

 

This is a complex question because you cannot have legend and more than one measure with conditional formatting.

 

Believe that the solution goes by adding a new table with the combination of sensors and groups something similar to this:

MFelix_0-1703245381583.png

 

Now add the following measure:

Values Sensor Group = CALCULATE(SUM('Readings'[Value]), 'Readings'[Group] in VALUES('Sensor_Group'[Group]) && 'Readings'[SensorNum] in VALUES('Sensor_Group'[SensorNum]), VALUES('Readings'[Group]))

Now do you chart with the following format:

X-Axis: Index By Sensor

Y-Axis: Values Sensor (measure created previously)

Legend: GroupSensor (column from the new table)

 

Now manually change the color for each group, and probably add the series label:

 

MFelix_1-1703245608631.png

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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