Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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:

example 1.png

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:

example 2.png

And what I need is :

example 3.png

 

I intend to make a treemap with this rank.

This is what I am getting:

 

example 4.png
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:

example 5.png

(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!!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

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

View solution in original post

14 REPLIES 14
harshnathani
Community Champion
Community Champion

HI @Anonymous ,

 

Please share your sample data in text format.

 

Copy in excel sheet and paste it.

 

Regards,

HN

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
Not applicable

 

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

 

Best

D

Anonymous
Not applicable

Hello @Anonymous !

 

Thank you so much for your reply. I really appreciate it.

However, your DAX has given me a column of 1's.

I figured out the DAX that I wanted, which is:

 

RANKX(ALL('Table'[Item Class]),calculate(sum([Dollar]),all('Table'[Brand])),,DESC)
 
 
Thank you! Have a great day 🙂 
Anonymous
Not applicable

Well, sadly, your measure will be misbehaving as soon as you add other columns to your table and start slicing by them... So your solution is only partial.

Best
D
Anonymous
Not applicable

Hello @Anonymous !

You are right.

I thought it was correct because it worked on my example data.

But when I tried to apply the logic into my real data, and it seems like some ranking are being skipped.

Without the Brand, the DAX will give me the correct Rank, but with the Brand, it skips some Rank # (no #1, no #4, etc...)

 

However, your's is still showing a column of 1's, both my real data and the example data.

If you don't mind, could you please help me furthur on this problem?

 

Thank you in advance!

 

Hi,

 

Regarding the skipping of ranks, you should probably set the ties parameter to 'dense'.

 

Please mark as solution if this solves it for you.

 

Kind regards,

 

Steve

Anonymous
Not applicable

I know mine is wrong. I just typed it without any data to test it on. That's why. To write the measure you're requesting is not an easy feat. I'll try to figure this out but can't guarantee...

Best
D
Anonymous
Not applicable

Hello @Anonymous 

 

Thank you.

I will continue working on it as well!

 

Is mine working funky because of 'All' statement?

Should I have a 'Allselected' statement somewhere in the DAX?

Anonymous
Not applicable

Removing the gaps is easy:

Your Ranking = 
RANKX(
    ALLSELECTED( 'Table'[Item Class] ),
    CALCULATE(
        SUM( 'Table'[Dollar] ),
        ALL('Table'[Brand])
    ),,
    DESC,
    Dense
)

But this still is not correct under all conditions, especially if you add new columns and start slicing by them.

 

Best

D

Anonymous
Not applicable

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

Anonymous
Not applicable

By the way, in the formula above [Dollar Amount] is SUM( Data[Dollar] ).

Best
D
Anonymous
Not applicable

Sadly, it's not that easy. The problem is this. You have filters on your slicers and in the table itself (each row in the table filters data, of course). ALLSELECTED works only on individual columns or the whole table. Here the problem is that we have to be in a position to somehow discover that some filters are coming from slicers and some from the table itself. This is the a REALLY HARD PART. I'm not even sure it can be done in full generality (I have not had to do it throughout the whole of my career as a DAX developer - 5 years).

It might well be that you'll have to stick to some partial versions working correctly only in certain circumstances 😞

Best
D
Anonymous
Not applicable

Hi @Anonymous 

 

Thank you so much for your assitance! 

I really appreciate your help. 😄

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors