Skip to main content

Fixing Filtering Issues in Power BI

For Power BI data modeling, star schema is recommended by gurus like Marco Russo, Alberto Ferrari all the time. The other day, I have understood the importance of using star schema one more time. You know what they say: Experience is the best teacher. Better than Marco Russo and Alberto Ferrari. Definitely.


This model looks more like Dalton Brothers than snowflake schema but it's called snowflake schema. Not to be confusing, I removed other dimensions which are not relevant to our subject. As you see there are one to many relationship between all of them and the filtering direction goes from high granularity to low granularity like it's supposed to.


In the first table, the Sub-Category column coming from the Products Sub Category table and the Total Sales measure is the sum of the sales column. With All Sub-Category measure, I remove the filter on the Products Sub Category table by using the ALL function. No problem so far. The problem appears in the second table. My All Sub-Category measure, which removes the filter from the Products Sub Category table also removes the filter from the Category table. It doesn't filter the Category table either, despite the fact that I haven't added the Category table to ALL part of my measure. This is happening because of this: To filter the sales on the Orders table, the Category table filter should pass through the Products Sub Category table and the Products table to reach the sales table. The thing is, it can't pass the filter through the Products Sub Category table, because I removed the filter on that one with my measure. I'd like to emphasize that it's not a filter direction problem. I have tested all different filtering direction combinations in case I might be missing something. That's not it. 
The measure can filter the Products Table as you see because it doesn't have to pass through the filter from a table where the filter is removed. The problem only arises when the filter has to pass through a table where the filter is removed. 
How to solve this? Somehow, I should find a way to tell Power BI to remove the filter only from the Product Sub Category table, and after that, implement the filter on the Product Category table. 


It can be done by adding the FILTER function, as the third part of the parameter. With Filter Category measure, I still keep the filter on the Products Sub Category Table but after that, I implement a filter on the Products Category table based on the Total Sales measure. 

And the moral of this story is not to use the FILTER function after the ALL function. It's using star schema instead of snowflake design. Combining FILTER and ALL solves my problem, but when we look at the big picture, this may not be the best practice, because we may not notice this problem since it doesn't come with an error message or something. Other than fixing issues that may not be noticed, I prefer to focus on not giving any room for this kind of issue from the beginning. And it can be done by converting snowflake design to star schema. You might be worried about repeating data with star schema since with snowflake design we have only unique values, but it's not a problem for Power BI. Because the Vertipaq engine compresses the columns to only its unique rows. Additionally, star schema provides a better user experience than the snowflake design, because it's more difficult to grasp all different hierarchy levels and filtering according to that for users.  

Comments

Popular posts from this blog

Manipulating Grand Totals with ISINSCOPE in Power BI

ISINSCOPE function was introduced in with November 2018 Power BI update. We can say it's a very young player in the game, but it's definitely magic. It can be used to manage lots of issues related to hierarchies, but in this post, I'm going to focus on manipulating the grand total line with it.  This is my budget forecast table. The budget forecast that you see for every month is not the forecast for every month, but it's the forecast for the whole year. The budget forecast for 2020 has changed every month. That's why we have different numbers for every row. Below you see the table visual with the year-month column and a measure that shows the values for every month. The thing that I want you to pay attention to is the total line. I wanted to see the last month's value for the total line and this is the measure for that.  This measure calculates this: If the year-month column has one value then take the average of that value. If it doesn't have one value, li...

How to Add Flags to Power BI Reports

Adding flags to Power BI Reports might be easier said than done. If you have a dataset which contains data of various countries, adding country flags looks cool. The process to do it, on the other hand, might be compelling. Now I am going to share with you the obstacles I  have faced during the process and of course, how I overcame them. First I made an excel list of countries that my dataset has. Then I have downloaded to my computer all the flags I need from Wikipedia and converted them to Base64 link via an image encoder website  base64-image.de  and added those links to my excel file, which looks like this: After I added my excel file to Power BI, I converted the data category of URL column from 'uncategorized' to 'image URL', which might be a small step for you but it's a big step for the success of the flag operation. Then I created one to many relationship with DimLocation table and chose 'both' as filter direction, so that I can filter from both t...

Solving Data Type Conflicts in Power BI and SQL

Whether we like it or not, error messages are part of our lives. The other day, I faced one of those little horrid yellow messages. For me, it's impossible to stay chill when I see that message. For a second, I feel alarmed and panicked. Let me show you what I'm talking about. The first column is the customer code, quite self-explanatory. The Registration Date column shows the date when the customer registered to my shop. The First Sale Date column shows the date when the customer bought something for the first time. And this is what I'm trying to see in the Sales Day column: If Registration Date and First Sale Date are the same, meaning customer bought something at the same day customer registered to my shop, print  "First Day", otherwise show the data of First Sale Date column. But computer says no! It says "Expressions that yield variant data-type cannot be used to define calculated columns." If I need to translate it in simplified English, it means: ...