Scenario:
Let's suppose we have a Power BI Star Schema Data model like below:
From the Data Model, we observed that dim_Products and dim_Country do not have the direct relationship.
However, they are having the (1:M) relationship with the fact table fct_Orders_Sample.There are total 6 Products available in the dim_Products:
From the above Star Schema Data model, we need to Calculate the Count of Products sold from the dimension table dim_Products at a Country Level.
In this Scenario we are Passing the Filters from the dim_Country dimension to dim_Products.
Now we will calculate the 2 Measures, one from Dimension Table (dim_Products) and another form the fact table(fct_Orders_Sample) to see the Filter behaviour.
Products Count (fact) = DISTINCTCOUNT(fct_Orders_Sample[Product_ID])
Products Count (dim) = DISTINCTCOUNT(dim_Products[Product_ID])
The result from fact table is Correct but not from Dimension table as shown below:
Now we can fix the measure based on dimension table(dim_Products), by adjusting the filter Context via passing the fact table into CALCULATE Function.
Products Count Adj (dim) =
CALCULATE(DISTINCTCOUNT(dim_Products[Product_ID]), fct_Orders_Sample)The above measure can be written using the below CROSS Fiter approach as well, which is not a ideal method:
Products Count CrossFilter (dim) =
CALCULATE(DISTINCTCOUNT(dim_Products[Product_ID]),
CROSSFILTER(dim_Products[Product_ID], fct_Orders_Sample[Product_ID], Both)
)
Products Count Adj Zero (dim) =
VAR _ZeroSales = IF ( NOT ISBLANK(SELECTEDVALUE(dim_Country[Country_ID])),0)
VAR _ProdCount =
CALCULATE(
DISTINCTCOUNT(dim_Products[Product_ID]), fct_Orders_Sample
)
RETURN
_ProdCount + _ZeroSales
To enable the Filtering between Slicers from dim_Country to dim_Products, we need to pass a visual level filter for dim_Products slicer as "Products Count Adj Zero (dim) > 0" :
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. If you wish, please share your genuine Feedback or comments only related to this Blog Posts. It is my humble request that, please do not post any Spam comments or Advertising kind of comments, which will be Ignored.