SWITCH() – age breakdown

Now that you have the customer's age, it's time to put each customer into an age bucket. For this example, there will be four separate age buckets:

  • 18-34
  • 35-44
  • 45-54
  • 55 +

The SWITCH function is preferable to the IF function when performing multiple logical tests in a single DAX formula. This is because the SWITCH function is easier to read and makes debugging code much easier.

With the Customer table selected, click New Column from the modeling ribbon. Type in the completed DAX formula for the following example:

Figure 5-Completed DAX formula

The preceding formula is very readable and understandable. There are three logical tests, and if a customer age does not evaluate to true on any of those logical tests, then that customer is automatically put into the 18-34 age bucket.

The astute reader may have noticed that the second and third logical tests do not have an upper range assigned. For example, the second test simply checks whether the customer's age is 45 or greater. Naturally, you may assume that a customer whose age is 75 would be incorrectly assigned to the 45–54 age bucket. However, once a row evaluates to true, it is no longer available for subsequent logical tests. Someone who is 75 would have evaluated to true on the first logical test (55 +) and would no longer be available for any further tests.

If you would like a better understanding of using the SWITCH statement instead of nesting multiple IF statements, then you can check out a blog post by Rob Collie at https://tinyurl.com/pbiqs-switch. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.70.185