Forum Discussion
Create colors based on value, not category
- 10 months ago
Hi jaryszek ,
Fix: branch around the whole RANKX, not around the table variable-- Works at Category and Subcategory levels, blanks elsewhere Rank (Current Axis) := VAR Result = IF ( ISINSCOPE ( Dim_Meter[MeterSubCategory] ), RANKX ( ALLSELECTED ( Dim_Meter[MeterSubCategory] ), CALCULATE ( [Total Amortized Cost] ), , DESC, DENSE ), IF ( ISINSCOPE ( Dim_Meter[MeterCategory] ), RANKX ( ALLSELECTED ( Dim_Meter[MeterCategory] ), CALCULATE ( [Total Amortized Cost] ), , DESC, DENSE ), BLANK () ) ) RETURN ResultIf you don’t have a Dim_Meter table, replace with your columns (but a proper dimension is recommended to avoid duplicate categories).
Hi jaryszek ,
You can follow these steps to implement it
1. Model MeterCategory and MeterSubCategory in a proper dimension (unique keys) and use those on visuals instead of the fact columns.
2.Create Rank Measure
DAX
Rank (Current Axis) :=
VAR RankTable =
SWITCH (
TRUE(),
ISINSCOPE ( Dim_Meter[Subcategory] ),
ALLSELECTED ( Dim_Meter[Subcategory] ),
ISINSCOPE ( Dim_Meter[Category] ),
ALLSELECTED ( Dim_Meter[Category] ),
-- Fallback when neither is on the visual
ALLSELECTED ( Dim_Meter[Category] )
)
RETURN
IF (
ISEMPTY ( RankTable ),
BLANK(),
RANKX (
RankTable,
CALCULATE ( [Total Amortized Cost] ), -- ensure row→filter context
,
DESC,
DENSE
)
)
3. Create Color Measure
DAX
Color by Rank :=
VAR r = [Rank (Current Axis)]
RETURN
SWITCH (
TRUE(),
ISBLANK ( r ) || r > 5, "#B3B3B3",
r = 1, "#E81123",
r = 2, "#107C10",
r = 3, "#0078D4",
r = 4, "#FFB900",
r = 5, "#8E8CD8"
)
4. Fact columns on the axis → duplicates can break ranks. Prefer a dimension.
• Top N visual filter → still works because we rank on ALLSELECTED; the Top N is applied after ranking, but the color measure is evaluated per visible item.
• Totals → ISINSCOPE prevents totals from getting a misleading rank; they’ll render as grey.
• Row-level security → honored automatically because we keep ALLSELECTED.
⭐Hope this solution helps you make the most of Power BI! If it did, click 'Mark as Solution' to help others find the right answers.
💡Found it helpful? Show some love with kudos 👍 as your support keeps our community thriving!
🚀Let’s keep building smarter, data-driven solutions together!🚀 [Explore More]
- jaryszek10 months agoSuper User
Hello,
thanks, it is not working at all:Best,
Jacek- GrowthNatives10 months agoSuper User
Hi jaryszek ,
Fix: branch around the whole RANKX, not around the table variable-- Works at Category and Subcategory levels, blanks elsewhere Rank (Current Axis) := VAR Result = IF ( ISINSCOPE ( Dim_Meter[MeterSubCategory] ), RANKX ( ALLSELECTED ( Dim_Meter[MeterSubCategory] ), CALCULATE ( [Total Amortized Cost] ), , DESC, DENSE ), IF ( ISINSCOPE ( Dim_Meter[MeterCategory] ), RANKX ( ALLSELECTED ( Dim_Meter[MeterCategory] ), CALCULATE ( [Total Amortized Cost] ), , DESC, DENSE ), BLANK () ) ) RETURN ResultIf you don’t have a Dim_Meter table, replace with your columns (but a proper dimension is recommended to avoid duplicate categories).