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 creating a separate color measure and then apply the conditional formatting to the individual measure being used in the matrix. The matrix has "Period" from Time Intelligence in the column bucket. The values has a number of measures but I'm only trying to get one to work! The YoY calculation item is basically as follows:

VAR CurrentVal = 
CALCULATE( 
   [Flights],
   KEEPFILTERS(DimDate[Date] = TODAY()
   )
VAR PriorVal = 
CALCULATE(
   [Flights],
   KEEPFILTERS(DimDate[Date] = TODAY()-365
   )
RETURN DIVIDE(CurrentVal-PriorVal, Prior)
This renders fine in the matrix, but I I try and apply conditional formating to the font based on a dedicated color measure, everything I've tried fails because of a data conversion error: cannot convert type text to type numeric/date. Here's the color measure. Copilot hasn't worked nor has Claude. I'm hoping people with real brains can help.

VAR _Item = SELECTEDVALUE('Time Intelligence'[Period])
VAR v =
    CALCULATE (
        SWITCH (
            TRUE(),
            _Item = "Yesterday YoY", [Flights],
            _Item = "MTD YoY",       [Flights],
            _Item = "YTD YoY",       [Flights],
            BLANK()
        )
    )

RETURN
IF (
    _Item IN { "Yesterday YoY", "MTD YoY", "YTD YoY" } && NOT ISBLANK(v),
    SWITCH (
        TRUE(),
        v > 0, "#00B050",
        v < 0, "#C00000",
        "#000000"
    )
)





 

  • 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.

7 Replies

  • 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.

    • tomlenzmeier's avatar
      tomlenzmeier
      New Member

      Sorry, but no. The data is confidential. The variance column in the calculation group/matrix I'm concerned about is strictly values like 4.0%, -4.4%, etc..

  • trivedisunita's avatar
    trivedisunita
    Continued Contributor

    Hi tomlenzmeier ,

     

    One possibility is that the calculation group is also being applied to your colour measure. Your colour measure returns text values like #00b050, whereas the YoY calculation item doing numeric calculations. If the calculation group is evaluating the colour measure too, that could be causing the cannot convert type text to type numeric/date error.
     
    If that's the case, you could try excluding the colour measure from the calculation item using ISSELECTEDMEASURE()
    Also , make sure the conditional formatting is configured as Format by-->Field value and that it is using your color measure directly.
     
    Hope this helps!

     

  • v-achippa's avatar
    v-achippa
    Community Support

    Hi tomlenzmeier,

     

    Thank you for reaching out to Microsoft Fabric Community.

     

    Thank you calerofkrishnakanth240trivedisunita and Nasif_Azam for the prompt response.

     

    As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided by the user's for the issue worked?  or let us know if you need any further assistance.

     

    Thanks and regards,

    Anjan Kumar Chippa

    • v-achippa's avatar
      v-achippa
      Community Support

      Hi @tomlenzmeier,

       

      We wanted to kindly follow up to check if the solution provided by the user's for the issue worked?  or let us know if you need any further assistance.

       

      Thanks and regards,

      Anjan Kumar Chippa