Power Bi: how to parameterize Top N visual level filter

2.4k Views Asked by At

Since PowerBI don't support Top N filter on page level, I want to use N as a parameter to change it at once per multiple visuals. Is it possible?

enter image description here

P.S.In this video (9:15) solution for more complex case is provided.

In the end of this article sample file available

2

There are 2 best solutions below

13
davidebacci On BEST ANSWER

Using the sample dataset, insert a new parameter.

Add a measure as follows:

Measure = 
IF(
    SELECTEDVALUE('Product'[Product Name]) IN 
    SELECTCOLUMNS( 
        TOPN(
            [Parameter Value], 
            ADDCOLUMNS( ALLSELECTED( 'Product'),"@Sales", [Sales Amount] ), 
            [@Sales]
            ), 
        "x", 
        'Product'[Product Name]), 
1)

Every visual you want affected by the TopN should have this filter.

enter image description here

That's it.

enter image description here enter image description here

enter image description here

0
Denis On

From usability perspective it's preferable to return Sales Rank in measure.

Solution below is a copy/paste from SQLBI experts solution with minimal code changes ( ALLSELECTED ( 'Product'[Product Name] ) replaced by ALLSELECTED ( 'Product' ) ):

rnkSales = 
    IF (
        ISINSCOPE ( 'Product'[Product Name] ),
        VAR ProductsToRank = [TopN Value]
        VAR SalesAmount = [Sales Amount]
        RETURN
            IF (
                SalesAmount > 0,
                VAR VisibleProducts =                 
                    FILTER(     -- filters out data with no sales
                        CALCULATETABLE (
                            VALUES ( 'Product' ),
                            ALLSELECTED ( 'Product')   -- Use this if VisualFilterTopN equivalent required
                            //ALLSELECTED ( 'Product'[Product Name] ) -- Original code - returns TopN per dimension
                        ),
                        NOT ISBLANK( [Sales Amount] ) -- looks more universal then [Sales Amount]>0 (if calculation for Margin required, it could be negative)
                    )
                VAR Ranking =
                    RANKX (
                        VisibleProducts,                    
                        [Sales Amount],
                        SalesAmount
                    )
                RETURN
                    IF (
                        Ranking > 0 && Ranking <= ProductsToRank,
                        Ranking
                    )
            )
    )

enter image description here