Tuesday, June 27, 2023

Explain the Logical Function ' WHEN' in Tableau with Example?

In Tableau, the "WHEN" keyword is used in conjunction with the "CASE" statement to define specific conditions and corresponding results. The "CASE" statement is a logical function that allows you to perform conditional evaluations and return different values based on multiple conditions.

The syntax of the "CASE" statement in Tableau is as follows:

CASE WHEN condition1 THEN result1
WHEN condition2 THEN result2
 ...
     ELSE result_else 
END

Here's an example to illustrate how the "WHEN" keyword is used with the "CASE" statement in Tableau:

Suppose you have a dataset of customers and their purchase amounts. You want to create a calculated field that categorizes customers into different spending groups based on their purchase amounts.

CASE 
 WHEN [Purchase Amount] < 100 THEN 'Low Spend'
 WHEN [Purchase Amount] >= 100 AND [Purchase Amount] < 500 THEN 'Medium Spend' WHEN [Purchase Amount] >= 500 THEN 'High Spend' ELSE 'Unknown'
 END

In this example, the "WHEN" keyword is used to define the conditions and the corresponding results. The first condition is [Purchase Amount] < 100, and if this condition evaluates to true, the result will be 'Low Spend'. The second condition is [Purchase Amount] >= 100 AND [Purchase Amount] < 500, and if this condition evaluates to true, the result will be 'Medium Spend'. The third condition is [Purchase Amount] >= 500, and if this condition evaluates to true, the result will be 'High Spend'. Finally, the "ELSE" keyword is used to specify the result 'Unknown' if none of the conditions evaluate to true.

The "WHEN" keyword, within the context of the "CASE" statement, allows you to define multiple conditions and their corresponding results. It is useful for performing complex conditional evaluations and data categorization in Tableau. 

No comments:

Post a Comment

If you have any doubts. Please let me know

How can you create an alias for a table in a SQL query?

In SQL, you can create an alias for a table in a query to give the table a temporary, alternative name that you can use in the query. Table ...