Forum Discussion

tomlenzmeier's avatar
tomlenzmeier
New Member
1 month ago
Solved

Conditionally Format a measure in a calculation group

I have a calculation group called "Time Intelligence." One of the calculation items is a YoY percentage variance. I want to conditionally format it with green for >0 and red for <0. I've tried creati...
  • Nasif_Azam's avatar
    1 month ago

    Hey  tomlenzmeier ,

    This is a real gotcha in how calculation groups behave.

     

    Why it fails

    A calculation item applies to every measure evaluated in that filter context, not just the ones you had in mind. Your matrix has 'Time Intelligence'[Period] on columns, so in the Yesterday YoY column every measure the visual touches gets wrapped by that item, including the measure for conditional formatting.

    The engine effectively runs:

    VAR CurrentVal = CALCULATE ( [YoY Colour], KEEPFILTERS ( DimDate[Date] = TODAY () ) ) 
    VAR PriorVal   = CALCULATE ( [YoY Colour], KEEPFILTERS ( DimDate[Date] = TODAY () - 365 ) )
    RETURN DIVIDE ( CurrentVal - PriorVal, PriorVal )

    SELECTEDMEASURE() is now your text colour measure, so CurrentVal - PriorVal is arithmetic on "#00B050". Hence the error.

     

     

    Nothing to do with SELECTEDVALUE, KEEPFILTERS or your SWITCH. Any measure returning a string will blow up the moment a numeric calculation item is applied to it.

     

    The fix: stop returning text

    Switch conditional formatting from Field value to Rules. The measure it's based on can then stay numeric, which means the calculation item is free to apply to it — and that's exactly what produces the YoY number you want to test.

    Flights YoY Rule Value =
    IF (
        SELECTEDVALUE ( 'Time Intelligence'[Period] )
            IN { "Yesterday YoY", "MTD YoY", "YTD YoY" },
        [Flights]
    )

    That's the whole measure. [Flights] is numeric, so the Yesterday YoY item wraps it and returns the variance.

    Then on the measure in the matrix: Conditional formatting → Font colour

     

     

    Yesterday YoY green, MTD YoY red, YTD YoY green, and Actual / MTD / YTD still black. I left a second measure (Cancellations) unformatted in the same visual — it shows the colour is scoped to the measure, not to the column.

     

    If you specifically need hex codes

    Then you are in for more work than it is worth, and here is why:

    The Field value picker only lists text-typed fields. A numeric measure is greyed out and unselectable.

     

     

    To satisfy both you need two changes:

    1. Guard each YoY calculation item so it hands the colour measure straight through:

    IF (
        ISSELECTEDMEASURE ( [Flights YoY Colour] ),
        SELECTEDMEASURE (),
        VAR CurrentVal = CALCULATE ( SELECTEDMEASURE (), KEEPFILTERS ( DimDate[Date] = TODAY () - 1 ) )
        VAR PriorVal   = CALCULATE ( SELECTEDMEASURE (), KEEPFILTERS ( DimDate[Date] = TODAY () - 366 ) )
        RETURN DIVIDE ( CurrentVal - PriorVal, PriorVal )
    )

    2. Have the colour measure compute its own variance, deriving the windows from the selected item:

    Flights YoY Colour =
    VAR _Item = SELECTEDVALUE ( 'Time Intelligence'[Period] )
    VAR _CurFrom = SWITCH ( _Item, "Yesterday YoY", TODAY () - 1, "MTD YoY", EOMONTH ( TODAY (), -1 ) + 1, "YTD YoY", DATE ( YEAR ( TODAY () ), 1, 1 ) )
    VAR _CurTo   = SWITCH ( _Item, "Yesterday YoY", TODAY () - 1, "MTD YoY", TODAY (), "YTD YoY", TODAY () )
    VAR _PriFrom = SWITCH ( _Item, "Yesterday YoY", TODAY () - 366, "MTD YoY", EOMONTH ( TODAY (), -13 ) + 1, "YTD YoY", DATE ( YEAR ( TODAY () ) - 1, 1, 1 ) )
    VAR _PriTo   = SWITCH ( _Item, "Yesterday YoY", TODAY () - 366, "MTD YoY", TODAY () - 365, "YTD YoY", TODAY () - 365 )
    VAR _Cur = CALCULATE ( [Flights], KEEPFILTERS ( DATESBETWEEN ( DimDate[Date], _CurFrom, _CurTo ) ) )
    VAR _Pri = CALCULATE ( [Flights], KEEPFILTERS ( DATESBETWEEN ( DimDate[Date], _PriFrom, _PriTo ) ) )
    VAR _YoY = DIVIDE ( _Cur - _Pri, _Pri )
    RETURN
        IF (
            _Item IN { "Yesterday YoY", "MTD YoY", "YTD YoY" },
            SWITCH ( TRUE (), ISBLANK ( _YoY ), BLANK (), _YoY > 0, "#00B050", _YoY < 0, "#C00000", "#000000" )
        )

     

     

     

     

    Also attached the PBIX file.