Case Statement in Power BI
Case Statement in Power BI, A case statement is a type of statement that evaluates conditions and returns a value when the first condition is met.
In Power BI, the easiest way to implement a case statement is by using the SWITCH function in DAX.
Basic Syntax of SWITCH Function
The basic syntax of the SWITCH function is as follows:
new = SWITCH(
'my_data'[Position],
"G", "Guard",
"F", "Forward",
"C", "Center",
"None"
)
Case Statement in Power BI
In this example, a new column named new
is created. It examines the values in the Position
column of the my_data
table and returns:
- “Guard” if
Position
equals “G” - “Forward” if
Position
equals “F” - “Center” if
Position
equals “C” - “None” if
Position
contains any other value
Practical Example: Using SWITCH in Power BI
Let’s walk through a practical example to see how this function works in Power BI.
Suppose we have a table named my_data
in Power BI that contains information about various basketball players. We want to create a new column that converts the values in the Position
column from “C”, “G”, and “F” to “Center”, “Guard”, and “Forward” respectively.
- Open Power BI Desktop and navigate to the
Table tools
tab. - Click the
New column
icon. - Enter the following formula into the formula bar:
new = SWITCH(
'my_data'[Position],
"G", "Guard",
"F", "Forward",
"C", "Center",
"None"
)
This formula will generate a new column called new
containing the values specified in the SWITCH function.
Result of the SWITCH Function
The new column will display the following values:
- “Guard” if
Position
is “G” - “Forward” if
Position
is “F” - “Center” if
Position
is “C” - “None” if
Position
is any other value
By following these steps, you can efficiently create a case statement in Power BI using the SWITCH function.
This method not only simplifies the process but also enhances the readability and manageability of your data transformation logic.
Enhance Your Power BI Skills
By mastering the SWITCH function and case statements in Power BI, you can streamline your data analysis and reporting tasks.
Continue exploring more Power BI features to become proficient in creating insightful and actionable reports.