Code Generator | DAX

DAX Measure for Decimal Formatting and Conditional Coloring

This guide explains how to create a DAX measure in Power BI that formats decimal values and changes text color based on conditions. It includes code examples and practical usage instructions for enhancing data visualization.


Empty image or helper icon

Prompt

change the decimals and color of text in power bi

Answer

DAX Function for Formatting Decimals and Text Color in Power BI

Introduction

This guide demonstrates how to create a DAX measure in Power BI that formats the decimal places of a value and changes its color based on certain conditions using the DAX language.

Function: FormatValueWithColor

/**
 * FormatValueWithColor
 *
 * This measure formats the decimal places of a numeric value and changes its color based on specified conditions. 
 * It returns the value as a formatted HTML string with appropriate color.
 *
 * @param ValueColumn The column containing the numeric values to be formatted.
 * @param DecimalPlaces The number of decimal places to format the value to.
 * @param PositiveColor The color for positive values (default: "green").
 * @param NegativeColor The color for negative values (default: "red").
 * @param ZeroColor The color for zero values (default: "black").
 *
 * @return A formatted HTML string representing the value with the specified decimal places and color.
 */
VAR DecimalPlaces = 2
VAR PositiveColor = "green"
VAR NegativeColor = "red"
VAR ZeroColor = "black"

VAR Value = SUM('YourTable'[ValueColumn])  -- Change 'YourTable' and 'ValueColumn' to your actual table and column.
VAR FormattedValue = 
    SWITCH(
        TRUE,
        Value > 0, FORMAT(Value, "#,##0." & REPT("0", DecimalPlaces)),
        Value < 0, FORMAT(Value, "#,##0." & REPT("0", DecimalPlaces)),
        Value = 0, FORMAT(Value, "#,##0." & REPT("0", DecimalPlaces)),
        FORMAT(Value, "#,##0." & REPT("0", DecimalPlaces))
    )

RETURN 
SWITCH(
    TRUE,
    Value > 0, "" & FormattedValue & "",
    Value < 0, "" & FormattedValue & "",
    Value = 0, "" & FormattedValue & ""
)

Explanation of the Code

  1. Comment Block: The code starts with a comprehensive docstring explaining the purpose, parameters, and return type.
  2. Parameters: Variables for decimal places and colors for positive, negative, and zero values.
  3. Summing Values: Summing the column's values.
  4. Formatting Values: Using the FORMAT function to set the number of decimal places.
  5. Switch Logic for Color: The SWITCH function determines the color of the output text based on the value.
  6. Return Statement: Returns the formatted value encapsulated in HTML <span> tags to style the text color.

Input Validation

DAX does not have native error handling or input validation mechanisms similar to other programming languages. However, best practices suggest ensuring that data types are handled correctly.

Usage Example

Assume you have a table named Sales with a column Revenue.

  1. Create a new measure:
    • Go to the "Modeling" tab.
    • Click on "New Measure".
    • Paste the FormatValueWithColor code into the formula bar.
  2. Replace YourTable and ValueColumn with Sales and Revenue.
  3. Use the measure in a table or a card visual.

Practical Example

Formatted Revenue = 
VAR DecimalPlaces = 2
VAR PositiveColor = "green"
VAR NegativeColor = "red"
VAR ZeroColor = "black"

VAR Value = SUM('Sales'[Revenue])
VAR FormattedValue = 
    SWITCH(
        TRUE,
        Value > 0, FORMAT(Value, "#,##0." & REPT("0", DecimalPlaces)),
        Value < 0, FORMAT(Value, "#,##0." & REPT("0", DecimalPlaces)),
        Value = 0, FORMAT(Value, "#,##0." & REPT("0", DecimalPlaces)),
        FORMAT(Value, "#,##0." & REPT("0", DecimalPlaces))
    )

RETURN 
SWITCH(
    TRUE,
    Value > 0, "" & FormattedValue & "",
    Value < 0, "" & FormattedValue & "",
    Value = 0, "" & FormattedValue & ""
)

Conclusion

With this measure, you can easily format decimal values and apply conditional color formatting for better data visualization in Power BI. For further learning on DAX functions and Power BI, consider courses available 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 guide explains how to create a DAX measure in Power BI that formats decimal values and changes text color based on conditions. It includes code examples and practical usage instructions for enhancing data visualization.