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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
tvalente
Helper I
Helper I

How to do rank partition like SQL in DAX

Hi All,

 

I am trying to return a column where in SQL you would rank by partition and sort the date descending. I am successfully ranking the max insurance date but am struggling to return the insurance type along with the maximum date.

 

As per my original table VAR, I am trying to connect that value back to together but struggled. In my summarize statement, if I introduce [Insurance type], it returns the maximum date along with the insurance type. This returns more values. I just want the maximum date and then whatever the insurance type is return that in a table.

 

This also could just be a calculated column as well perhaps. 

 

MaxInsuranceStatus =
VAR vBaseTable =
ADDCOLUMNS (
SUMMARIZE (
'Employee List',
'Employee List'[Employee number],
'Employee List'[Employee name]
),
"@Date", MAX ( 'Employee List'[Insurance Date] )
)
VAR vRankTable =
ADDCOLUMNS ( vBaseTable, "@Rank", RANKX ( vBaseTable, [@Date],, DESC, DENSE ) )
VAR vTopRank =
FILTER ( vRankTable, [@Rank] = 1 )
Var OriginalTable =
SELECTCOLUMNS('Employee List',"Employee number",'Employee List'[Employee number],"Insurance Date",'Employee List'[Insurance Date],"Vaccination type",'Employee List'[Insurance type])
//Var FilterOriginal =
//FILTER(vTopRank,[@Date] = OriginalTable
RETURN
vTopRank

1 ACCEPTED SOLUTION

Found a solution from the sqlbi guys:

 

As below this works as a calculated column:

 

VaccineRank =
VAR CurrentEmployee = 'Employee List'[Employee number]
VAR EmployeesGroup =
FILTER (
'Employee List',
'Employee List'[Employee number] = CurrentEmployee
)
RETURN
RANKX (
EmployeesGroup,
'Employee List'[Vaccine Date],,DESC
)

View solution in original post

3 REPLIES 3
amitchandak
Super User
Super User

@tvalente ,  You need column rank or measure rank .

 

 

Column Rank = rankx(filter( 'Employee List', 'Employee List'[Employee number] = max('Employee List'[Employee number])) 'Employee List'[Insurance Date],,desc,dense)

 

measure Rank

Column Rank = rankx(allselected( 'Employee List'[Employee name], 'Employee List'[Employee number]) , max('Employee List'[Insurance Date]),,desc,dense)

 

 

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks,

 

This did not work.

 

The result was this:

 

Employee nameEmployee numberInsurance typeInsurance DateColumn Rank
ABC1105Type AWednesday, 11 August 20211
ABC1105Type BWednesday, 19 May 20211

 

My desired result is this:

 

Employee nameEmployee numberInsurance typeInsurance DateColumn Rank
ABC1105Type AWednesday, 11 August 20211
ABC1105Type BWednesday, 19 May 20212

Found a solution from the sqlbi guys:

 

As below this works as a calculated column:

 

VaccineRank =
VAR CurrentEmployee = 'Employee List'[Employee number]
VAR EmployeesGroup =
FILTER (
'Employee List',
'Employee List'[Employee number] = CurrentEmployee
)
RETURN
RANKX (
EmployeesGroup,
'Employee List'[Vaccine Date],,DESC
)

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.