Another week, another blog! This week, we will cover something very basic. If you are a BI developer and work with Power BI, you will be familiar with DAX functions - Values, Value, and Distinct. This blog will cover all the common pitfalls and practical use cases for each of them.
Before we start, let's start with a small question. Are distinct and unique means the same? For a very long time, I used to think they were just synonyms, but they aren't. Wait what?? How are they different? Okay, let's understand it. Distinct means each value in a specific column appeared once. In other words, you can say to count all values as 1. If you are a fan of SQL, then you already know that distinct is used to remove duplicates. While unique means you only consider the values that aren't repeated in a column. There's a subtle difference between the 2 that gets ignored easily.
Okay, since you are aware of the difference between distinct and unique, we can now start the real action. To do so, I have created a manual table in the query editor that contains order values associated with the different classes. For the demo, we have kept a few rows empty.
Let's start with the Values function in DAX. Values allow us to get the distinct values for a specific column or a table, depending on the argument you have passed, keeping the filter context. Okay, we will be creating another column in the dummy table using Values.
Oh ERROR! But why? To debug this, we need to understand the behaviour of Values. It only returns a table of distinct values and not a single value. What is the correct approach then? We can try this DAX for the calculated column. With this measure, we are making things simpler and asking to populate the Class Values column with Class, and in case of blanks and empty strings, populate it with class D.
We have covered both scenarios by using 2 conditions with IF. ISBlank will tackle the actual blanks, meaning no values are available for that cell, while there are cases like we have of false blanks, meaning nothing is typed in, and gives us a 0-length string.
Now we have learnt a lesson of not using Values in a calculated column. Let's try to use the Values function with a measure.
Oh, not again! Okay, let's figure this out. By using this measure, we are asking to return a table as a scalar value, which is not allowed for a measure. To correct this, we can use the following measure. We are asking to return a list of classes with a comma separator. Coalesce allows us to tackle the blank with an empty string "".
Isn't that amazing?? Let's discuss one more use case that can be tackled with Values. The idea is to get the order value for the selected class. For the demo, we will be visualising in a matrix. First, DAX will give you the correct results, but if you have understood the basics of filter, all, and keep filters, you will definitely go for the optimised version of DAX. If you want to know more about Keepfilters function, here is a quick read.
With the dirty version of DAX, we are using the ALL function unnecessarily, but why? We are removing the filters from Blank dummy table and then reapplying them only for the Class with the use of In Values. Hence, we opt for the optimised version where we use Keepfilters that allow us to keep the current filter, and In Values can help to add a filter condition for the class.
- "N/A",
- Empty String "",
- with currency symbols "$ 2000 and
- last your values have a comma or decimal in between "2,000.00"
Love this blog? Get even more with BI Bits!
I've launched a Power BI newsletter called BI Bits — your go-to for quick, practical tips and tricks to level up your dashboards and DAX skills.
Each edition is:
Short and actionable
Beginner- and intermediate-friendly
Comments
Post a Comment