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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
MatsSchrader
Regular Visitor

I want to set a color per value in a measure

Hi,
I am building a custom visual and want to be able to set a color for each unique value in a given measure.
Since this visual is using the matrix dataview mapping, I can't use conditional formatting.
Then I created a ColorPicker for each unique value, but I can't find these colors in the dataView.


This is how I build the formattingSlices for these ColorPickers:

for (const [transitionKey, transition] of transitionsColorMap) {
edgeColorSlices.push({
uid: `edgeTransitionColor_${transitionKey}_uid`,
displayName: transitionKey,
control: {
type: powerbi.visuals.FormattingComponent.ColorPicker,
properties: {
descriptor: {
objectName: 'edgeSettings',
propertyName: `categoricalColors.${transitionKey}`,
selector: transition.identity.getSelector(),
},
value: { value: transition.color },
},
},
});
}

My capabilities:

"edgeSettings": {
"properties": {
"categoricalColors": {
"type": {
"fill": {
"solid": {
"color": true
}
}
}
},
...

I realize this setup might be flawed — in the capabilities I’m defining categoricalColors as a color value, but in my code, I'm treating it like an object with color values for each key.
I’ve tried a few variations but haven’t found a working structure yet.

To clarify: the ColorPickers show up for each unique value as I intend, but in the code I can't find the values I set in the settings with those ColorPickers.

Next steps I'm considering:

  •  I will have a look if I can first create this categoricalColors object with visualHost.persistProperties and then let the ColorPickers write their values in there.
  • I will read up on dataview mappings to see, If it is possible to stay with the matrix structure I have for now but add one categorical field for conditional formatting.


Any help is greatly appreciated!
Best regards
Mats

1 ACCEPTED SOLUTION

Hi @v-csrikanth ,
If I understand correctly I would need to edit this new DAX measure everytime I want to change a color by going in there and rewriting the hex codes instead of using a colorPicker in the settings, right?
I am sorry, I don't know where to find that dropdown arrow to get to "Conditional formatting > Font color or Background color". I built a custom visual and am not using the pre defined matrix or table.
If I was able to set that option I still don't know where to find those values in the code after, like in the dataView object. In my original post I said that I was able to activate conditional formatting, at least in the way that the conditional formatting pane opened and I could define rules etc., but I had no way of accessing those chosen colors because they were not in the dataView.

In the meantime we built a workaround where we don't use the PowerBI settings anymore, but built our own settings panel inside the visual. With this we are able to get the desired functionality.


Best regards
Mats

View solution in original post

5 REPLIES 5
v-csrikanth
Community Support
Community Support

Hi @MatsSchrader 
I really appreciate your contibution and workaround shared in the community. Could please accept as solution so that It would be helpful to others facing a similar situation.

Best Regards,
Community Support Team _ C Srikanth.

v-csrikanth
Community Support
Community Support

Hi @MatsSchrader   
To apply color formatting per value in a measure within Power BI, you can create a DAX measure that returns color codes based on specific conditions. Here's how you can achieve this:

  1. Define a new measure that returns a color code (e.g., hex code) based on the value of your target measure. For example: ColorMeasure =
    SWITCH(
    TRUE(),
    [YourMeasure] > 1000, "#FF0000", // Red
    [YourMeasure] > 500, "#FFFF00", // Yellow
    "#00FF00" // Green
    )

    Replace [YourMeasure]with the actual measure you're evaluating.

  2. Apply Conditional Formatting:

    • Select the visual (e.g., table or matrix) where you want to apply the formatting.

    • Click on the dropdown arrow next to the field you want to format and choose Conditional formatting > Font color or Background color.

    • In the formatting dialog: Set Format by to Field value. Set Based on field to the ColorMeasure you created.

This approach allows you to dynamically assign colors to values in your visuals based on the logic defined in your ColorMeasure.


If the above information helps you, please give us a Kudos and marked the Accept as a solution.
Best Regards,
Community Support Team _ C Srikanth.

 

 

Hi @v-csrikanth ,
If I understand correctly I would need to edit this new DAX measure everytime I want to change a color by going in there and rewriting the hex codes instead of using a colorPicker in the settings, right?
I am sorry, I don't know where to find that dropdown arrow to get to "Conditional formatting > Font color or Background color". I built a custom visual and am not using the pre defined matrix or table.
If I was able to set that option I still don't know where to find those values in the code after, like in the dataView object. In my original post I said that I was able to activate conditional formatting, at least in the way that the conditional formatting pane opened and I could define rules etc., but I had no way of accessing those chosen colors because they were not in the dataView.

In the meantime we built a workaround where we don't use the PowerBI settings anymore, but built our own settings panel inside the visual. With this we are able to get the desired functionality.


Best regards
Mats

v-csrikanth
Community Support
Community Support

Hi @MatsSchrader 
In your capabilities JSON, you're defining categoricalColors as a single fill property, but you’re trying to dynamically treat it like a map of values per key. This causes a mismatch, and Power BI doesn't persist or expose the colors correctly in dataView.objects.
******************************************************

"objects": {
"edgeSettings": {
"displayName": "Edge Settings",
"properties": {
"categoricalColors": {
"type": {
"fill": {
"solid": {
"color": true
}
}
},
"suppressDisplayName": true
}
}
}
}

******************************************************
In visual update logic, use visualHost.persistProperties() to store the color mapping manually.
this.host.persistProperties({
merge: [
{
objectName: 'edgeSettings',
properties: {
[`categoricalColors.${transitionKey}`]: { solid: { color: colorValue } }
},
selector: transition.identity.getSelector()
}
]
});
This ensures that Power BI will store each key’s color as a distinct entry unde categorialColors.

Then during rendering, you can read the saved color values like this:
const objects = dataView.metadata.objects;
const colorForKey = DataViewObjects.getFillColor(
objects,
`edgeSettings`,
`categoricalColors.${transitionKey}`,
defaultColor
);

If the above information helps you, please give us a Kudos and marked the Accept as a solution.
Best Regards,
Community Support Team _ C Srikanth.

Hi @v-csrikanth ,
thank you for the answer but it does not seem to work.
With your suggested solution a user, with no access to the code, can use my visual, go to the formatting pane, select colors (via ColorPickers) for each transitionValue and it will be accessable via that "DataViewObjects.getFillColor" call?
To me it looks like the "DataViewObjects.getFillColor" call just accesses what I set with ".persistProperties".
And even that does not work on my end.

If I am mistaken I will try again to implement your solution or provide a minimal working example.

Best regards
Mats

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

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.