Forum Discussion

Koushikrish's avatar
Koushikrish
Helper I
4 years ago
Solved

DAX TOP N Measure on a Matrix Issue

Hello,

 

I have a requirement to display TOP 10 States on a Volume measure across a matrix visual. I have used the TOP N DAX Measure to achieve this : 

CALCULATE([Volume], TOPN(10, ALL(dimGeography[State_Code]), [Volume], DESC), VALUES(dimGeography[State_Code]))

Now if I use this measure on a matrix with just the State field on the Columns it works fine by displaying the TOP 10 states:

 

But if I were to include some field on the rows of the matrix as well, its doesn't display the true TOP 10 states but TOP 10 for each item on the row. The bigger problem is that the Total appears only for the True TOP 10 states and not others:

 

 

I think this is correct because when you include a field for the row, it considers the row context and pulls TOP 10 for that row. And the Totals are showing up only on the actual true TOP 10 states and not others. But is there a way to tweak the measure or visual to just display the True TOP 10 states only when including a row field as well? I am guessing that it would involve eliminating the row context on the measure.

 

I have also tried using a variation using RANKX to acheive this TOP 10 measure but its the same result. 

 

CALCULATE([Volume], FILTER(VALUES(dimGeography[State_Code]),IF(RANKX(ALL(dimGeography[State_Code]),[Volume],,DESC)<=10,[Net Revenue], BLANK())))

 

Would appreciate any help on this. Please let me know if you need more information!

 

 

  • Anonymous's avatar
    Anonymous
    4 years ago

    Koushikrish glad it worked!

    To also remove the filter from Brand, this below might work (I've not tested this)

     

        CALCULATE(
            [Volume],
            TOPN(10,
                ALL(dimGeography[State_Code], dimGeography[RegionName]),
                CALCULATE([Volume], ALL('dimBrand'[BrandName]))
                DESC
            ),
            VALUES(dimGeography[State_Code])
        )

     

     

8 Replies

  • Koushikrish , try if this can help

    measure =
    var _tab = TOPN(10, ALL(dimGeography[State_Code]), [Volume], DESC)
    return
    CALCULATE([Volume], FILTER(VALUES(dimGeography[State_Code]),dimGeography[State_Code] in _tab))

    • Koushikrish's avatar
      Koushikrish
      Helper I

      Hi amitchandak , thanks for the response. This doesn't seem to fix the issue though. It doesn't appear to show just top 10 but all states and mapped to their corresponding region : 

       

  • Anonymous's avatar
    Anonymous
    Not applicable

    Koushikrish , how should the visual look when you have Region added? Could you post the desired output, please.

    • Koushikrish's avatar
      Koushikrish
      Helper I

      Hello Anonymous. These are the top 10 states. My desired output is to retain these Top 10 states only across columns even if I include anything on the rows.

      Top 10 states:

      This is how I need it when I include Region Name on the columns : 

       

       

       

      But right now it shows this way when I include Region Name, its displaying the Top 10 states for each Region.

      Hope that info was sufficient. Else please let me know.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Koushikrish I tried with some sample data but couldn't reproduce the first image in your firdt post.

        I think that perhaps that when the Region field is added, you need the measure remove the filters from the Region Field. I think this will do it:

         

         

            CALCULATE(
                [Volume],
                TOPN(10,
                    ALL(dimGeography[State_Code], dimGeography[RegionName]),
                    [Volume],
                    DESC
                ),
                VALUES(dimGeography[State_Code])
            )