Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Rankx by a column

Hello, I am still very new to Power BI and I am having trouble understanding Rankx.

I would really appreciate if you could please help me with this ðŸ˜„

 

Currently, I have 4 measures, Item Type, Item Class, Brand and Dollar

Item Type: A1,A2,A3,A4,A5

Item Class: B11,B12,B13,B14,B15,B16,B17,B18,B19,B21,B22,B23,B24,B25,B26,B31,B32,B33,B41,B51,B52

Brand: C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12

 

Think of Item Type as "Laptop", "CellPhone" etc...

Item Class as "13' Laptop", "15' Laptop", "Flip phone", "Smart phone" etc...

Brand as "Samsung", "Apple" etc....

 

So there is unique set of Item Class designated to each Item Type, but Brand does not have this limitation.

 

I am using Item Type in a slicer and trying to rank Item Class by the Dollar like this:

where Rank1=RANKX(ALL('Table'[Item Class]),calculate(sum([Dollar])),,DESC)

(Dollar is not a measure, it is a summary of the column)

 

Now, I am trying to add the Brand column and hope to have the same rank number matching to the Item Class.

Instead, I get this:

And what I need is :

 

I intend to make a treemap with this rank.

This is what I am getting:

 

where
1st Dollar by Brand Test=calculate(sum('Table'[Dollar]),filter(allselected('Table'[Item Class]),[Rank1]=1))

 

But I would like to have the treemap to look like this instead:

(1st Dollar by Brand = calculate(sum('Table'[Dollar]),'Table'[Item Class]="B15") because "B15" is the Item Class where the Rank1=1)

 

And I would like to create this treemap for top 5 ranks (rank=1, rank=2, rank=3, etc...)

 

Though it is not included in this example, I am planning to use this logic in another dataset that will be filtered with multiple slicers. (Date, etc...)

 

Is there a way to achieve this using DAX?

 

Thank you!!

  • Anonymous's avatar
    Anonymous
    6 years ago

    Here's the measure. I think it now works correctly in ALL CIRCUMSTANCES.

     

     

    Ranking = 
        IF( HASONEFILTER( 'Data'[Item Class] ),
            RANKX(
                ALLSELECTED( Data[Item Class] ),
                var __itemClass = Data[Item Class]
                return
                    CALCULATE(
                        [Dollar Amount],
                        Data[Item Class] = __itemClass,
                        ALLSELECTED( Data )
                    ),
                CALCULATE(
                    [Dollar Amount],
                    VALUES( Data[Item Class] ),
                    ALLSELECTED( Data )
                ),
                DESC,
                Dense
            )
        )

     

     

    Best

    D

14 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    HI Anonymous ,

     

    Please share your sample data in text format.

     

    Copy in excel sheet and paste it.

     

    Regards,

    HN

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello! So it says I cannot paste the data on this, so I let me provide a pivoted table of the data!

       

      Item TypeItem ClassBrandSum of Dollar
      A1B11C4559638071
      A1B11C537638
      A1B12C28378024
      A1B12C424458186
      A1B12C5515795839
      A1B13C2146905813
      A1B13C486777553
      A1B14C1601528639
      A1B14C3698254
      A1B14C54277261
      A1B15C110731303201
      A1B15C21856720
      A1B15C387986539
      A1B15C419217314
      A1B16C21614411
      A1B16C38555446
      A1B16C451040
      A1B16C58992
      A1B17C21943911737
      A1B17C344
      A1B17C4117482329
      A1B18C34965049
      A1B18C5820918
      A1B19C1166831158
      A1B19C21
      A1B19C35870631
      A1B19C4792756
      A2B21C1161379075
      A2B21C285707959
      A2B21C35911
      A2B21C4168073
      A2B21C5843221864
      A2B21C625931941
      A2B21C747
      A2B22C119
      A2B22C331081
      A2B22C443
      A2B22C666203708
      A2B22C736329292
      A2B23C182633
      A2B23C262056222
      A2B23C34601008927
      A2B23C4483
      A2B23C5506
      A2B23C6520
      A2B24C2297645
      A2B24C342232079
      A2B24C42326158588
      A2B24C5662131297
      A2B24C61928812
      A2B24C76621
      A2B25C196983088
      A2B25C3496431282
      A2B25C41608955018
      A2B25C516766576
      A2B25C684
      A2B26C177201633
      A2B26C4417
      A2B26C66027347783
      A2B26C754460600
      A3B31C1016538019041
      A3B31C117798694
      A3B31C1210
      A3B31C23037667
      A3B31C3240519145
      A3B31C44600198227
      A3B31C521786
      A3B31C686
      A3B31C72266
      A3B31C83537
      A3B31C937538535
      A3B32C1472711
      A3B32C109874115
      A3B32C125211176855
      A3B32C232686956
      A3B32C63341362153
      A3B32C73337361
      A3B32C87198223
      A3B32C9623
      A3B33C1905
      A3B33C2712516648
      A3B33C3272
      A3B33C41941400395
      A3B33C5765114231
      A3B33C685670076
      A3B33C7106555
      A3B33C8221295
      A3B33C9546183285
      A4B41C1012186138703
      A4B41C11414246
      A4B41C39515872856
      A4B41C43817771326
      A4B41C559877132
      A4B41C618269309391
      A4B41C786929608
      A4B41C890886321
      A4B41C96820892674
      A5B51C347309110
      A5B51C49540363
      A5B51C512040613221
      A5B51C6213316664
      A5B51C79693108402
      A5B52C313778740590
      A5B52C447283886
      A5B52C51162957495
      A5B52C65837010088
      A5B52C76116792986
      • Anonymous's avatar
        Anonymous
        Not applicable

         

        [Ranking] =
        IF( HASONEFILTER( 'Table'[Item Class] ),
        	CALCULATE(
        		RANKX(
        			VALUES( 'Table'[Item Class] ),
        			CALCULATE( SUM('Table'[Dollar] ) ),
        			,
        			DESC
        		),	
        		ALLSELECTED( 'Table' )
        	)
        )

         

        Best

        D