SWITCH DAX Function (Logical)

Returns different results depending on the value of an expression.

Syntax

SWITCH ( <Expression>, <Value>, <Result> [, <Value>, <Result> [, … ] ] [, <Else>] )
Parameter Attributes Description
Expression

The expression to be evaluated.

Value Repeatable

If expression has this value the corresponding result will be returned.

Result Repeatable

The result to be returned if Expression has corresponding value.

Else Optional

If there are no matching values the Else value is returned.

Return values

Scalar A single value of any type.

A scalar value coming from one of the Result expressions, if there was a match with Value, or from the Else expression, if there was no match with any Value.

Remarks

All result expressions and the else expression must be of the same data type.

» 2 related articles
» 3 related functions

Examples

A common use of SWITCH is to match the result of an expression with constant value:

SWITCH (
    [A],
    0, "Zero",
    1, "One",
    2, "Two",
    "Other numbers"
)

However, the argument can be an expression and the initial can be a constant.
By using TRUE as a first argument, SWITCH can replace a list of cascading IF statements.
The following code:

IF (
    [A] > [B], 
    "First case",
    IF ( 
        [A] = [B],
        "Second case",
        IF (
            [A] = 0,
            "Third case",
            "Fourth case"
        )
    )
)

can be written as:

SWITCH (
    TRUE,
    [A] > [B], "First case",
    [A] = [B], "Second case",
    [A] = 0, "Third case",
    "Fourth case"
)
--  SWITCH evaluates its first argument (value) and then uses the next
--  remaining parameters in pair: the first element is used to match the value
--  the second as the result if there is a match.
--  The last argument, alone, provides the no-match value and it defaults 
--  to blank.
DEFINE MEASURE Sales[Discounted Sales] = 
    SUMX ( 
        VALUES ( 'Product'[Category] ),
        VAR DiscountPct = 
            SWITCH ( 
                'Product'[Category],
                "Audio", 0.15,
                "Computers", 0.2,
                "Cell phones", 0.13,
                0
            ) 
        RETURN
            [Sales Amount] * (1 - DiscountPct )
    )
EVALUATE
SUMMARIZECOLUMNS ( 
    'Product'[Category],
    "Sales Amount", [Sales Amount],
    "Discounted sales", [Discounted Sales]
)
ORDER BY [Category]
Category Sales Amount Discounted sales
Audio 384,518.16 326,840.44
Cameras and camcorders 7,192,581.95 7,192,581.95
Cell phones 1,604,610.26 1,396,010.93
Computers 6,741,548.73 5,393,238.98
Games and Toys 360,652.81 360,652.81
Home Appliances 9,600,457.04 9,600,457.04
Music, Movies and Audio Books 314,206.74 314,206.74
TV and Video 4,392,768.29 4,392,768.29
--  A common usage of SWITCH is to use a constant as the value argument
--  and expressions in the pairs. This technique allows more flexibility
--  even though it somewhat lacks in readability.
DEFINE MEASURE Sales[Discounted Sales] = 
    SUMX ( 
        SUMMARIZE ( Sales, Sales[Net Price], Product[Category] ),
        VAR DiscountPct = 
            SWITCH ( 
                TRUE,
                Sales[Net Price] <= 150, 0.15,
                Sales[Net Price] <= 1000, 0.2,
                Product[Category] = "Audio", 0.13,
                0
            ) 
        RETURN
            [Sales Amount] * (1 - DiscountPct )
    )
EVALUATE
SUMMARIZECOLUMNS ( 
    'Product'[Category],
    "Sales Amount", [Sales Amount],
    "Discounted sales", [Discounted Sales]
)
ORDER BY [Category]
Category Sales Amount Discounted sales
Audio 384,518.16 319,255.67
Cameras and camcorders 7,192,581.95 5,975,444.25
Cell phones 1,604,610.26 1,302,864.23
Computers 6,741,548.73 5,806,084.78
Games and Toys 360,652.81 306,554.89
Home Appliances 9,600,457.04 8,540,512.07
Music, Movies and Audio Books 314,206.74 255,356.90
TV and Video 4,392,768.29 3,561,271.37
--  Using SWITCH the first condition met defines the result.
--  In the following example, the second condition (<= 150) will never be
--  met, because the first one is less restrictive.
DEFINE MEASURE Sales[Discounted Sales] = 
    SUMX ( 
        SUMMARIZE ( Sales, Sales[Net Price], Product[Category] ),
        VAR DiscountPct = 
            SWITCH ( 
                TRUE,
                Sales[Net Price] <= 1000, 0.2,
                Sales[Net Price] <= 150, 0.15,
                Product[Category] = "Audio", 0.13,
                0
            ) 
        RETURN
            [Sales Amount] * (1 - DiscountPct )
    )
EVALUATE
SUMMARIZECOLUMNS ( 
    'Product'[Category],
    "Sales Amount", [Sales Amount],
    "Discounted sales", [Discounted Sales]
)
ORDER BY [Category]
Category Sales Amount Discounted sales
Audio 384,518.16 307,614.53
Cameras and camcorders 7,192,581.95 5,961,597.72
Cell phones 1,604,610.26 1,283,688.21
Computers 6,741,548.73 5,765,868.70
Games and Toys 360,652.81 288,522.25
Home Appliances 9,600,457.04 8,489,439.38
Music, Movies and Audio Books 314,206.74 251,365.39
TV and Video 4,392,768.29 3,537,902.55

Related articles

Learn more about SWITCH in the following articles:

Related functions

Other related functions are:

Last update: Jul 30, 2021   » Contribute   » Show contributors

Contributors: Alberto Ferrari, Marco Russo

Microsoft documentation: https://docs.microsoft.com/en-us/dax/switch-function-dax

2018-2021 © SQLBI. All rights are reserved. Information coming from Microsoft documentation is property of Microsoft Corp. » Contact us   » Privacy Policy & Cookies

Context Transition

This function performs a Context Transition if called in a Row Context. Click to read more.

Row Context

This expression is executed in a Row Context. Click to read more.

Iterator

Not recommended

The use of this function is not recommended. See Remarks and Related functions for alternatives.

Not recommended

The use of this parameter is not recommended.

Deprecated

This function is deprecated. Jump to the Alternatives section to see the function to use.

Deprecated

This parameter is deprecated and its use is not recommended.

DirectQuery compatibility

Limitations are placed on DAX expressions allowed in measures and calculated columns.
The state below shows the DirectQuery compatibility of the DAX function.

Contribute

Want to improve the content of SWITCH? Did you find any issue?
Please, report it us! All submissions will be evaluated for possible updates of the content.


This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.