0
votes

Problem

I need to use the dynamic conditional formatting built-in in Power BI to change color row-wise. The lowest value in the row must be green, the highest value must be red and all colors in the middle are shades of orange between green and red. The problem is that built-in conditional formatting calculates maximum and minimum over the entire matrix and there is no option to calculate maximum and minimum column-wise or row-wise.

Example

Below a simple example, given dimension_1 (Color), dimension_2 (Year) and a KPI (measure). In reality, the matrix is much bigger than this (e.g. 15 distinct year) but this is a representative example.

+-------------+-------------+------+------+------+
| dimension_1 | dimension_2 | 2017 | 2018 | 2019 |
+-------------+-------------+------+------+------+
| Red         | Measure     |    0 |   50 |  100 |
| Blue        | Measure     | -100 |  -50 |    0 |
| Green       | Measure     | -100 |    0 |  100 |
+-------------+-------------+------+------+------+

Power BI would consider -100 as minimum value and +100 maximum value. Hence, the conditional formatting applied would be like:

+-------------+-------------+--------------+--------------+--------------+
| dimension_1 | dimension_2 |     2017     |     2018     |     2019     |
+-------------+-------------+--------------+--------------+--------------+
| Red         | Measure     | light orange | dark orange  | red          |
| Blue        | Measure     | green        | dark orange  | yellow       |
| Green       | Measure     | green        | yellow       | red          |
+-------------+-------------+--------------+--------------+--------------+

Expected solution

The expected solution apply conditional formatting calculating maximum and minimum row-wise. Therefore, in row 1 the minimum value is 0 and the maximum value is 100. If we replicate this calculation for all rows we obtain the following:

+-------------+-------------+-------+--------+------+
| dimension_1 | dimension_2 | 2017  |  2018  | 2019 |
+-------------+-------------+-------+--------+------+
| Red         | Measure     | green | orange | red  |
| Blue        | Measure     | green | orange | red  |
| Green       | Measure     | green | orange | red  |
+-------------+-------------+-------+--------+------+

Reminder

The conditional formatting must be dynamic given maximum and minimum value row-wise. In other words, I do no not want to use rule to define fixed colors, instead let Power BI define different shades.

1

1 Answers

1
votes

Taken from SQLBI and modified to suit the requirement:

MinMax =
VAR AllList =
    CALCULATETABLE (
        ADDCOLUMNS (
            SUMMARIZE ( Sheet1, Sheet1[Dimension 1], Sheet1[Dimension 2], DimDate[Month] ),
            "@Total", [Total]
        ),
        ALLSELECTED ()
    )
VAR dm1 =
    SELECTEDVALUE ( Sheet1[Dimension 1] )
VAR dm2 =
    SELECTEDVALUE ( Sheet1[Dimension 2] )
VAR CurrList =
    FILTER (
        SELECTCOLUMNS (
            AllList,
            "Dimension1", [Dimension 1],
            "Dimension2", [Dimension 2],
            "DMonth", [Month],
            "TTotal", [@Total]
        ),
        [Dimension1] = dm1
            && [Dimension2] = dm2
    )
VAR MinValue =
    MINX ( CurrList, [TTotal] )
VAR MaxValue =
    MAXX ( CurrList, [TTotal] )
VAR CurrentValue = [Total]
VAR Result =
    SWITCH (
        TRUE,
        CurrentValue = MinValue, 1,
        -- 1 for MIN - green
        CurrentValue = MaxValue, 3,
        -- 3 for MAX - red
        2
    )
RETURN
    Result

enter image description here

Thanks