Using the SWITCH function
The SWITCH
function is very useful when you need to evaluate an expression and return a result from a list of possible values. In this recipe, you will create a column that can be used to determine if the road was paved or unpaved. This will allow your users to filter the results easily by choosing a label versus a value. The SWITCH
function has a required syntax of SWITCH(<expression>,<value>,<result>[, <value>,<result>])
.
How to do it...
Open the Chapter_9_DAX solution, select the CRASH_DATA_T table, and make sure you are in the data Grid view.
Scroll to the right until you find the Add Column. Then in the expression box, add the formula to evaluate each value and return the corresponding label. Then press Enter to create the calculation. You will then see a label added to each row:
=SWITCH([PAVED], 1, "Paved", 2, "Unpaved", 99, "Unknown")
On the Properties window, change the Column Name from Add Column to Fatality Group.