Tuesday, June 27, 2023

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

In Tableau, the "CASE" logical function is used to create conditional statements and perform different calculations or assign different values based on specific conditions. It allows you to define multiple conditions and corresponding actions within a single function.


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


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

Here's an example to illustrate how the "CASE" function works:


Suppose you have a dataset of employees and their salaries, and you want to create a calculated field that assigns a salary category based on the salary value. You define three categories: "Low" for salaries below $30,000, "Medium" for salaries between $30,000 and $50,000, and "High" for salaries above $50,000.


CASE WHEN [Salary] < 30000 THEN 'Low' WHEN [Salary] >= 30000 AND [Salary] <= 50000 THEN 'Medium' ELSE 'High' END

In this example, the "CASE" function is used to check multiple conditions and assign different values based on the salary range. If the salary is less than $30,000, the result will be 'Low'. If the salary is between $30,000 and $50,000 (inclusive), the result will be 'Medium'. For salaries above $50,000, the result will be 'High'.


For instance, if an employee has a salary of $25,000, the "CASE" function will evaluate the first condition and assign the value 'Low' to that employee. If another employee has a salary of $40,000, the function will evaluate the second condition and assign the value 'Medium'. If a third employee has a salary of $60,000, the function will evaluate the last condition and assign the value 'High'.


The "CASE" function is flexible and allows you to handle complex conditions and perform custom calculations based on your data requirements. It is commonly used for data transformation, creating custom categories or segments, and implementing conditional logic 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 ...