Forum Discussion

jaryszek's avatar
jaryszek
Super User
7 months ago
Solved

Power BI conditional formatting using a disconnected table

Hello,

I have a Power BI model with a fact table that contains budget values (both positive and negative) by date, department, and account, and I’m displaying the data in a matrix.

I want to handle conditional formatting using a separate table that stores formatting rules (for example min/max ranges, ratios, and colors), rather than hardcoding thresholds directly in DAX.

 

The formatting table is disconnected from the model and is meant to drive background or font colors based on the calculated value (budget or a ratio).

What I’m looking for is the best DAX pattern to read from this formatting table and return the correct color for each cell in the matrix, including how to handle negatives, zeros, and totals correctly.

Has anyone implemented a similar approach, and what’s the recommended way to structure the DAX so Power BI applies the formatting reliably?

 

My model:

 

Fmt_BudgetRules:

 

sample model:

https://drive.google.com/file/d/1jltIdC256ddBE763yepzgdOfMq4LDSAa/view?usp=sharing

 

 

  • Use this as Field value for background/font color:

    [Budget Color] =
    VAR v = [Budget]
    RETURN
    IF (
        ISBLANK ( v ),
        BLANK(),
        MAXX (
            FILTER (
                Fmt_BudgetRules,
                v >= Fmt_BudgetRules[MinValue]
                    && v < Fmt_BudgetRules[MaxValue]
            ),
            Fmt_BudgetRules[ColorHex]
        )
    )

    Format pane → Conditional formatting → Background color (or Font color) →

    • Format by: Field value

    • Based on field: [Budget Color]

13 Replies

  • Use this as Field value for background/font color:

    [Budget Color] =
    VAR v = [Budget]
    RETURN
    IF (
        ISBLANK ( v ),
        BLANK(),
        MAXX (
            FILTER (
                Fmt_BudgetRules,
                v >= Fmt_BudgetRules[MinValue]
                    && v < Fmt_BudgetRules[MaxValue]
            ),
            Fmt_BudgetRules[ColorHex]
        )
    )

    Format pane → Conditional formatting → Background color (or Font color) →

    • Format by: Field value

    • Based on field: [Budget Color]

    • jaryszek's avatar
      jaryszek
      Super User

      I like the solution, 

      let me try it!

      Best,
      Jacek

  • Hey jaryszek ,

     

    Give User defined functions a shot. UDFs will centralize the logic so you will be able to change it later and you will be sure that it is consistently applied throughout. You can enable UDFs in preview features and add something like this in the DAX query view:

    DEFINE FUNCTION GetColor = ( measurevalueNUMERIC ) => IF(measurevalue > 0"#00B050""#FF0000")
    EVALUATE { GetColor(100) }
    You will be simply writing the coloring measure as follows:
    Sales Color = GetColor([Sales])
    Profit Color = GetColor([Profit])

    Update the function to reference your disconnected table and update the logic as required for handling various conditions.

    Hope it helps!

    • jaryszek's avatar
      jaryszek
      Super User

      If this will work on DirectLake over OneLake? 

      Best,
      Jacek

  • Hi jaryszek 

     

    First, I wish you uploaded a file with numbers typed as numbers and not text so we wouldn't have to figure out why  the measures returned an error. This aside, try the following:

    Color Fill = 
    VAR _Value = [Bud Amount]
    RETURN
        CALCULATE (
            SELECTEDVALUE ( Fmt_BudgetRules[ColorHex] ),
            FILTER (
                Fmt_BudgetRules,
                _Value >= Fmt_BudgetRules[MinValue] && _Value <= Fmt_BudgetRules[MaxValue]
            )
        )
    
    Font Color = 
    VAR _Value = [Bud Amount]
    RETURN
        CALCULATE (
            SELECTEDVALUE ( Fmt_BudgetRules[FontHex] ),
            FILTER (
                Fmt_BudgetRules,
                _Value >= Fmt_BudgetRules[MinValue] && _Value <= Fmt_BudgetRules[MaxValue]
            )
        )
    
    

     

    Note:  Conditional formatting using rules and field value can be applied only to non-blank cells. While you can force a measure to return 0, this causes data points to appear unintentionally.

     

     

     

    • jaryszek's avatar
      jaryszek
      Super User

      Thank you! 

      It would be much easier If I could just upload file here in the forum, now I need to make a workarounds 🙂 

      Great solution.

      Have a question, 
      What if I do not want to show Color Fill measure in my matrix? 

            SELECTEDVALUE ( Fmt_BudgetRules[ColorHex] ),


      Selected value will be not working then...


      Best,
      Jacek


      • danextian's avatar
        danextian
        Super User

        You don't need to. Just use that measure as the field value in conditional formatting options. That should be in the pbix i attached. 

  • For fun only, a more sophisticated solution with simpler format mapping table and UDF,

    fx_Color = (str_Type: string) =>
    		VAR __sum = [SUM BudgetAmount]
    		RETURN
    			MAXX(
    				CALCULATETABLE(
    					TOPN(
    						1,
    						Fmt_BudgetRules,
    						Fmt_BudgetRules[MinValue]
    					),
    					Fmt_BudgetRules[MinValue] <= __sum,
                        Fmt_BudgetRules[Target] = str_Type
    				),
    				Fmt_BudgetRules[Value]
    			)

  • AshokKunwar's avatar
    AshokKunwar
    Continued Contributor

    Hello! jaryszek 

    Using a disconnected table for metadata-driven formatting is the best practice for maintainable models. To make this work, you need a DAX measure that identifies which row of your Fmt_BudgetRules table the current value falls into

    ​You can use the following pattern for your color measure:

    Dynamic_Color_Measure = VAR CurrentValue = [Your_Budget_Measure]

    VAR SelectedColor = CALCULATE(

    SELECTEDVALUE(Fmt_BudgetRules[Color_Hex_Code]),

    FILTER(

    Fmt_BudgetRules,

    CurrentValue >= Fmt_BudgetRules[Min_Range] && CurrentValue < Fmt_BudgetRules[Max_Range]

    )

    )

    RETURN

    IF(ISBLANK(SelectedColor), "#FFFFFF", SelectedColor)

    How to apply this:

    1. ​Go to your Matrix visual.
    2. ​In the Format pane, go to Cell elements.
    3. ​Turn on Background color and click the fx icon.
    4. ​Set 'Format style' to Field value.
    5. ​Select this Dynamic_Color_Measure from your fields.

    Handling Totals and Negatives:

    This pattern handles negatives automatically as long as your Fmt_BudgetRules table has ranges like Min: -999999 to Max: 0. For totals, if you want a different logic, wrap the code in an IF(HASONEVALUE(Account_Column), ...) statement to check if the calculation is at the row level or the total level.

    ​I hope this helps you build a more dynamic report! If this pattern works for your model, please mark this as an Accepted Solution. Happy New Year!

    ​Best regards,

    Vishwanath

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

    Hi jaryszek,

     

    Thank you danextian alish_b cengizhanarslan for your response to the query.

    we haven't heard back from you regarding our last response and wanted to check if your issue has been resolved.

    Should you have any further questions, feel free to reach out.
    Thank you for being a part of the Microsoft Fabric Community Forum!

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

    Hi jaryszek,

     

    I wanted to follow up on our previous suggestions regarding the issue. We would love to hear back from you to ensure we can assist you further.

     

    Thank you.