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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
zazaalaza
Frequent Visitor

Dense Rank by ordering on 3 columns with a twist

I am looking to calculate a dense rank as shown in the image. Order by [StartDate], [EndDate] and [Phase] and then rank on [ID], [Phase]. The issue is that the values in [Phase] are not necessarily are in Ascending order since the [StartDate] & [EndDate] takes precedent. 

 

How can I go about solving this?

Screen Shot 2022-08-25 at 6.35.37 PM.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi@ zazaalaza,

According to your description, You want to group by [ID] field, then sort by [StartDate],[EndDate],[Phase] fields(priority: [StartDate]>[EndDate]>[Phase]) and count after that, and the [Phase] field is the same as The previous line is the same and remains unincremented..Right?

Here are the steps you can follow:

(1)This is my test data:

yingyinr_0-1661845458521.png

 

(2)We can sort [ID], [StartDate],[EndDate],[Phase] fields in Power Query Editor. Set priority: [ID]>[StartDate]>[EndDate]>[Phase]

yingyinr_1-1661845458540.png

 

Then it will Automatically generate M-statements:       = Table.Sort(#"Changed Type",{{"ID", Order.Ascending}, {"StartDate", Order.Ascending}, {"EndDate", Order.Ascending}, {"Phase", Order.Ascending}})

(3) We can click Index Column From 1 in Add Column to create a index column.After it,we can click Apply and close to submit the data to Power BI Desktop.

yingyinr_2-1661845458556.png

 

(4)We can create a calculated column:  Count

Count = 
var _current_phase='Table1'[Phase]
var _current_index='Table1'[Index]
var _cuurent_id='Table1'[ID]
var _pre_line_phase=MAXX( FILTER('Table1','Table1'[Index]=_current_index-1 && 'Table1'[ID]=_cuurent_id) ,'Table1'[Phase])
return
  IF( _current_phase= _pre_line_phase , 0,1)

 

(5) Finally, we create another calculated column so that we can meet your need:

Rank = 
var _first=MINX( FILTER('Table1','Table1'[ID]=EARLIER('Table1'[ID])) ,'Table1'[Index])
var _last= 'Table1'[Index]
return
  CALCULATE(SUM('Table1'[Count]),FILTER( ALL('Table1'),'Table1'[Index]>=_first &&'Table1'[Index] <=_last))

yingyinr_3-1661845458563.png

 

If this method dose not meet your needs, can you provide some special inputs and outputs so that we can help you better.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi@ zazaalaza,

According to your description, You want to group by [ID] field, then sort by [StartDate],[EndDate],[Phase] fields(priority: [StartDate]>[EndDate]>[Phase]) and count after that, and the [Phase] field is the same as The previous line is the same and remains unincremented..Right?

Here are the steps you can follow:

(1)This is my test data:

yingyinr_0-1661845458521.png

 

(2)We can sort [ID], [StartDate],[EndDate],[Phase] fields in Power Query Editor. Set priority: [ID]>[StartDate]>[EndDate]>[Phase]

yingyinr_1-1661845458540.png

 

Then it will Automatically generate M-statements:       = Table.Sort(#"Changed Type",{{"ID", Order.Ascending}, {"StartDate", Order.Ascending}, {"EndDate", Order.Ascending}, {"Phase", Order.Ascending}})

(3) We can click Index Column From 1 in Add Column to create a index column.After it,we can click Apply and close to submit the data to Power BI Desktop.

yingyinr_2-1661845458556.png

 

(4)We can create a calculated column:  Count

Count = 
var _current_phase='Table1'[Phase]
var _current_index='Table1'[Index]
var _cuurent_id='Table1'[ID]
var _pre_line_phase=MAXX( FILTER('Table1','Table1'[Index]=_current_index-1 && 'Table1'[ID]=_cuurent_id) ,'Table1'[Phase])
return
  IF( _current_phase= _pre_line_phase , 0,1)

 

(5) Finally, we create another calculated column so that we can meet your need:

Rank = 
var _first=MINX( FILTER('Table1','Table1'[ID]=EARLIER('Table1'[ID])) ,'Table1'[Index])
var _last= 'Table1'[Index]
return
  CALCULATE(SUM('Table1'[Count]),FILTER( ALL('Table1'),'Table1'[Index]>=_first &&'Table1'[Index] <=_last))

yingyinr_3-1661845458563.png

 

If this method dose not meet your needs, can you provide some special inputs and outputs so that we can help you better.

daXtreme
Solution Sage
Solution Sage

Hi @zazaalaza 

 

This kind of "ranking" (but it's not really real ranking) would be far easier to do in Power Query than in DAX. So, where do you want to do it?

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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