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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
sruthicb
Frequent Visitor

Increment using dense rank in power BI DAX

Hello,

I have 4 columns . I want the below mentioned output. but I don't know what do we call this output. an denserank? I don't think it is the case because dense rank gives different results than I wanted.
Can anyone tell what approach we need to follow in DAX to get this desire output. I thought I can use dense rank but not in my case. Thank you

 

test.png

 

 

 

 

1 ACCEPTED SOLUTION
Deku
Community Champion
Community Champion

This measure gives the required ranking

Rank = 
var streaks =
	ADDCOLUMNS(
		ALLSELECTED( 'Table'[Category], 'Table'[date], 'Table'[Diffference] )
		,"@StreakChanged",
			IF(
			COALESCE(
				SELECTCOLUMNS(
					CALCULATETABLE(
						OFFSET( 
							-1, 
							ALLSELECTED( 'Table'[Category], 'Table'[date], 'Table'[Diffference] ),
							ORDERBY( 'Table'[date], ASC ),
							PARTITIONBY( 'Table'[Category] )
						)
					)
					,'Table'[Diffference]
				)
				,'Table'[Diffference]
			) = 'Table'[Diffference]
			, 1, 0
		)
		,"@RowNum",
			ROWNUMBER(
				ALLSELECTED( 'Table'[Category], 'Table'[date], 'Table'[Diffference] ),
				ORDERBY( 'Table'[date], ASC ),
				PARTITIONBY( 'Table'[Category] )
			)
	)
var islands = 
	ADDCOLUMNS(
		streaks,
		"@rank",
			var dt = 'Table'[date]
			var cat = 'Table'[Category]
			return
			( [@RowNum] - SUMX( FILTER( streaks, 'Table'[date] <= dt && 'Table'[Category] = cat), [@StreakChanged] ) ) + 1
	) 
RETURN
SELECTCOLUMNS(
    FILTER(
        islands
        ,var dt = SELECTEDVALUE( 'Table'[date] )
        var cat = SELECTEDVALUE( 'Table'[Category] )
        return
        'Table'[date] = dt && 'Table'[Category] = cat
    )
    ,[@rank]
)

Deku_0-1742768343198.png

 

 


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

10 REPLIES 10
bhanu_gautam
Super User
Super User

@sruthicb , Try using

DAX
Rank =
VAR CurrentScore = 'Table'[score]
VAR CurrentName = 'Table'[name]
VAR CurrentGroup = 'Table'[Group]
RETURN
RANKX(
FILTER(
'Table',
'Table'[Group] = CurrentGroup && 'Table'[name] = CurrentName
),
'Table'[score],
,
DESC,
DENSE
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






This doesnot work for me.
where ever there is score 90, it is giving me the same Rank. 
It is working same as Rank. but I want the result as mentioned in the screenshot

Deku
Community Champion
Community Champion

In your table there is no column giving a ordering, without this your request is not possible


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
sruthicb
Frequent Visitor

Hi @Deku , Can you please suggest which one to use to get the desired result in the screenshot? Thank you for your help. 

Deku
Community Champion
Community Champion

This measure gives the required ranking

Rank = 
var streaks =
	ADDCOLUMNS(
		ALLSELECTED( 'Table'[Category], 'Table'[date], 'Table'[Diffference] )
		,"@StreakChanged",
			IF(
			COALESCE(
				SELECTCOLUMNS(
					CALCULATETABLE(
						OFFSET( 
							-1, 
							ALLSELECTED( 'Table'[Category], 'Table'[date], 'Table'[Diffference] ),
							ORDERBY( 'Table'[date], ASC ),
							PARTITIONBY( 'Table'[Category] )
						)
					)
					,'Table'[Diffference]
				)
				,'Table'[Diffference]
			) = 'Table'[Diffference]
			, 1, 0
		)
		,"@RowNum",
			ROWNUMBER(
				ALLSELECTED( 'Table'[Category], 'Table'[date], 'Table'[Diffference] ),
				ORDERBY( 'Table'[date], ASC ),
				PARTITIONBY( 'Table'[Category] )
			)
	)
var islands = 
	ADDCOLUMNS(
		streaks,
		"@rank",
			var dt = 'Table'[date]
			var cat = 'Table'[Category]
			return
			( [@RowNum] - SUMX( FILTER( streaks, 'Table'[date] <= dt && 'Table'[Category] = cat), [@StreakChanged] ) ) + 1
	) 
RETURN
SELECTCOLUMNS(
    FILTER(
        islands
        ,var dt = SELECTEDVALUE( 'Table'[date] )
        var cat = SELECTEDVALUE( 'Table'[Category] )
        return
        'Table'[date] = dt && 'Table'[Category] = cat
    )
    ,[@rank]
)

Deku_0-1742768343198.png

 

 


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
sruthicb
Frequent Visitor

Hi @Deku  thanks for the response. 

Deku
Community Champion
Community Champion

Just have to add EARLIER to out the outer row context

rnk = 
var streaks =
	ADDCOLUMNS(
		ALLSELECTED( 'Table'[Category], 'Table'[date], 'Table'[Diffference] )
		,"@StreakChanged",
			IF(
			COALESCE(
				SELECTCOLUMNS(
					CALCULATETABLE(
						OFFSET( 
							-1, 
							ALLSELECTED( 'Table'[Category], 'Table'[date], 'Table'[Diffference] ),
							ORDERBY( 'Table'[date], ASC ),
							PARTITIONBY( 'Table'[Category] )
						)
					)
					,'Table'[Diffference]
				)
				,'Table'[Diffference]
			) = 'Table'[Diffference]
			, 1, 0
		)
		,"@RowNum",
			ROWNUMBER(
				ALLSELECTED( 'Table'[Category], 'Table'[date], 'Table'[Diffference] ),
				ORDERBY( 'Table'[date], ASC ),
				PARTITIONBY( 'Table'[Category] )
			)
	)
var islands = 
	ADDCOLUMNS(
		streaks,
		"@rank",
			var dt = 'Table'[date]
			var cat = 'Table'[Category]
			return
			( [@RowNum] - SUMX( FILTER( streaks, 'Table'[date] <= dt && 'Table'[Category] = cat), [@StreakChanged] ) ) + 1
	)
var dt = 'Table'[date]
RETURN
SELECTCOLUMNS(
    FILTER(
        islands
        ,var dt = EARLIER( 'Table'[date] )
        var cat = EARLIER( 'Table'[Category] )
        return
        'Table'[date] = dt && 'Table'[Category] = cat 
    )
    ,[@rank]
)

Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
sruthicb
Frequent Visitor

what id I add Index column?

Deku
Community Champion
Community Champion

A date or some kind of index


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
sruthicb
Frequent Visitor

I changed my input and added a date column. here instead of score , now it is difference column.the main date column is DATE

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

Top Solution Authors