Forum Discussion

calamistratus's avatar
calamistratus
New Member
4 months ago
Solved

Calculating filter only for certain rows in a table

I'm having troblues optimizing fitter for a table of 25K rows. It contains companies and their items, there are only 100 companies and a very high number of Items. The filter I want to apply picks ...
  • Juan-Power-bi's avatar
    4 months ago

    Hola Amigo

    The problem is that your measure runs the TOPN calculation once per row in the visual, so with 25k rows it's running 25k times. The fix is to compute it once and reuse it.

     

    The key trick is to make DAX cache the TopN result instead of recalculating it per row.


    Something like this should be much lighter:
    daxShowCompany =
    VAR N = SELECTEDVALUE('TopNValues'[Ranks])
    VAR TopNBrands =
    CALCULATETABLE(
    TOPN(N, ALL('unique_brands'[brand]), [SalesForChosenPeriods], DESC),
    ALL('unique_brands')
    )
    RETURN
    IF(
    N = 0 || SELECTEDVALUE('unique_brands'[brand]) IN TopNBrands,
    1,
    0
    )
    Using IN instead of CONTAINS is also faster. The main thing though is making sure the TOPN runs against the brand table directly without being re-evaluated in item-level context.

  • AnkitKukreja's avatar
    4 months ago

    Hi!   calamistratus 

    You can try this

    ShowCompany :=
    VAR N = SELECTEDVALUE ( 'TopNValues'[Ranks], 0 )
    VAR BrandRank =
        RANKX(
            ALL ( 'unique_brands'[brand] ),
            [SalesForChosenPeriods],
            ,
            DESC,
            DENSE
        )
    RETURN
    IF ( N = 0 || BrandRank <= N, 1, 0 )