Forum Discussion

Sergii24's avatar
Sergii24
Super User
2 years ago

Countrows: selected VS other

Hi all, I have an issue with calcualtion of selected items vs other when multiple filters are applied (i.e. countrows).

My semantic model:

 

  • d_Product - dimensional table with attrbutes of products
  • f_Sales - fact table with sales of products
  • t_Attribute - technical table with 1 column and 2 rows "Selected" and "Other" (to be used in pie chart's legend)
  • Parameter - Field parameter to select atrribute (color or size) and then corresponding values (consider it out of scope for now)

 

My test case: Color = Red and Blue, Product = E

On canvas I apply filters and verify that there are actually 4 products using table which is disconnected from "Product" filter.

 

As you can see, I obtain 2 for others, while the expected result is 3. Below you can find my DAX for "Selected vs Other" measure (from debugging I've found that VAR _AllProducts_ProductLevel returns 3 rows while I expect them to be 4):

Selected vs Other = 
//----TEST CASE: Color: Red and Blue are selected, Product E is selected-----

VAR _SelectedAttribute = SELECTEDVALUE( t_Attribute[Attribute] )            //when used in pie chart with attribute in legend, I'll always have only 1 value selected: "selected" or "other"
VAR _SelectedProducts = VALUES( d_Product[Product] )                        //1 column table with currently selected products, in my case only 1 row with product E

VAR _AllProducts_ProductLevel =
    CALCULATETABLE(                                                         //calculatetable is used to overwrite existing filters 
        d_Product,                                                          //I suppose that before evaluation this table contains 1 row (product E only). However, before evaluation it will be modified by filters below following execution sequence of calculate()
        ALL( d_Product[Product] )                                           //I'm removing any filter from Product, so I expect the only active filter to remain is Color: Red and Blue, so the expected output is 4 but the result is 3... Why?
    )

VAR _OtherProducts_ProductLevel =                                           //once we removed any filter from product, so I have 4 rows in _AllProducts_ProductLevel I'm excluding SelectedProducts to obtain the rest
    FILTER(
        _AllProducts_ProductLevel,                                          //expected 4 rows (all products with Color: Red and Blue)
        NOT [Product] IN _SelectedProducts                                  //exclude the selected product E, expected output 3
    )

RETURN
    SWITCH(
        TRUE(),
        _SelectedAttribute = "Selected",                                    //when we're at "Selected" slice of pie chart 
            COUNTROWS( _SelectedProducts ),                                 //count rows of selected items
        _SelectedAttribute = "Other",                                       //when we're at "Other" slice of pie chart 
            COUNTROWS( _OtherProducts_ProductLevel )                        //count rows of other items of corresponding level
    )

 

The pbix is attached.

 

I'd be grateful if someone can help me to fix the problem and explain the error that I don't see... Thank you!

 

P.S. firstly I thought it's something related to field parameter, but even without it the result is the same (see page "Without Field Parameter" of pbix file)

2 Replies