Code Generator | DAX

DAX Conditional Formatting for Survey Completion

This DAX function applies conditional formatting to the `rca-1` column in the `vw_RNTTP_SurveyCompletion` table by using `rca1enabled` values to determine colors: "Red" for `1`, "Gray" for `0` or `NULL`.


Empty image or helper icon

Prompt

table vw_RNTTP_SurveryCompletion column rca-1 format it to red if value from column rca1enabled is 1 and if it is null  format it to gray if the value is 0

Answer

DAX Function for Conditional Formatting

Overview

This DAX function allows for conditional formatting of the rca-1 column in the vw_RNTTP_SurveryCompletion table. The formatting will be based on the values in the rca1enabled column according to the following rules:

  • Red if the value of rca1enabled is 1
  • Gray if the value of rca1enabled is 0 or NULL

DAX Measure

Formatted_rCA_1_Color = 
    VAR rca1Status = SELECTEDVALUE(vw_RNTTP_SurveryCompletion[rca1enabled])
    RETURN
        SWITCH(
            TRUE(),
            rca1Status = 1, "Red",
            ISBLANK(rca1Status), "Gray",
            rca1Status = 0, "Gray",
            "No Color"  // Default case
        )

Explanation of the Code

  1. Variable Declaration:

    • VAR rca1Status: Retrieves the selected value from the rca1enabled column for the current context, which simplifies the remaining logic.
  2. SWITCH Function:

    • Utilizes the SWITCH function with a condition of TRUE() to allow for multiple conditional checks.
    • Evaluates:
      • If rca1Status equals 1, it returns "Red".
      • If rca1Status is null (checked using ISBLANK), it returns "Gray".
      • If rca1Status equals 0, it also returns "Gray".
      • If none of these conditions are met, it falls back to a default case returning "No Color".

Usage Example

To use this DAX measure for conditional formatting:

  1. Go to the visualization where you want to apply conditional formatting.
  2. In the formatting options, find "Conditional Formatting".
  3. Set the conditional format based on the output of the Formatted_rCA_1_Color measure.
  4. Ensure that any relevant visualizations adjust color based on this output.

Conclusion

This DAX measure is efficient and adheres to best practices, enabling effective conditional formatting in Power BI reports based on the specific criteria outlined.

For further improvement of your DAX skills, consider taking courses offered on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This DAX function applies conditional formatting to the rca-1 column in the vw_RNTTP_SurveyCompletion table by using rca1enabled values to determine colors: "Red" for 1, "Gray" for 0 or NULL.