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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
ncraft
Frequent Visitor

index/rank/generate for groups with dates

Hi team, 

I am stumbling in the dark on this one. I feel like I should be able to solve this but I just cant get there for some reason. 
I have a table of data that I need to create an index/rank for. The table has a column for names/IDs and dates: 

datename
1/31/2022A
2/1/2022A
2/2/2022A
2/3/2022A
2/4/2022A
2/7/2022A
2/8/2022A
2/9/2022A
2/10/2022A
2/11/2022A
2/1/2022B
2/2/2022B
2/3/2022B
2/4/2022B
2/7/2022B
2/8/2022B
2/9/2022B
2/10/2022B
2/11/2022B
1/31/2022C
2/1/2022C
2/2/2022C
2/3/2022C
2/4/2022C
2/7/2022C
2/8/2022C
2/9/2022C
2/10/2022C

 

Note how the date column is not unique but any name & date combination is unique. In this sample data, I have sorted the columns to group the names and rank the dates from min to max, but in the actual data table, they are unsorted. It may not be necessary to point it out but not all [name]s have the same dates. For example, "1/31/2022 & A" exists, "1/31/2022 & C" exists but "1/31/2022 & B" dose NOT exist. 
I need an index that ranks these dates for each name to produce the following result. 

datenameIndex_result
1/31/2022A1
2/1/2022A2
2/7/2022B5
2/8/2022B6
2/2/2022B2
2/3/2022B3
2/1/2022C2
2/4/2022C5
1/31/2022C1
2/10/2022C9
2/2/2022A3
2/3/2022A4
2/7/2022C6
2/10/2022A9
2/11/2022A10
2/1/2022B1
2/9/2022A8
2/10/2022B8
2/11/2022B9
2/2/2022C3
2/3/2022C4
2/4/2022A5
2/8/2022A7
2/9/2022B7
2/7/2022A6
2/4/2022B4
2/8/2022C7
2/9/2022C8

 

If possible, I'd like to know how to do this in both DAX as a calculated column and in Poiwer M Quary as a transform. 

Thank you!

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

Picture2.png

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

11 REPLIES 11
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

Picture2.png

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


How do you update this to rank where the dates are the same for the same name but you need a different rank to only get the first date with the same name?

@Jihwan_Kim, This solution works wonderfully as a calculated column. But what about as a transform step in Power Query Editor?

The reason I need it in Power M Query, is becuase I want to be able to perform a transformation on a seperate table that uses a merg queries to bring the index over to a new table of related data. If the index column only exisists as a calculated column, it wont be avalible in power query edidtor for transformations. 

ncraft
Frequent Visitor

Oh, I am sorry @Jihwan_Kim , I just noticed you included the PBIX file with the transform solutions on the sample data. 
Let me implement that now and see if it completes the solution. 

ncraft
Frequent Visitor

YES! Your code @Jihwan_Kim for power M Query solved this exactly as expected. 
Thank you!

TheoC
Super User
Super User

Hi @ncraft 

 

You can use the following column to achieve what you're after:

 

Columm = RANKX ( FILTER ( 'Table' , 'Table'[Name] = EARLIER ( 'Table'[Name]' ) ) , 'Table'[Date] )

 

Hopefully this helps.


In terms of using Power Query, using Transform unfortunately won't get you the outcome you need. You will need to use INDEX and a few other steps to get there.

 

Let me know if it's something you really need if a column is insufficient because it can get a little tedious. 


Thanks,

Theo

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

CNENFRNL
Community Champion
Community Champion

A simplest Excel worksheet formula is enough

CNENFRNL_0-1644899178273.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

thank you but the project I am working on is a large report in Power BI. The requested solution here is just a small piece of a much larger puzzle. A solution in excel is not feezable. 

TheoC
Super User
Super User

Hi @ncraft 

 

You can use the following column to achieve what you're after:

 

Columm = RANKX ( FILTER ( 'Table' , 'Table'[Name] = EARLIER ( 'Table'[Name]' ) ) , 'Table'[Date] )

 

Hopefully this helps.


In terms of using Power Query, using Transform unfortunately won't get you the outcome you need. You will need to use INDEX and a few other steps to get there.

 

Let me know if it's something you really need if a column is insufficient because it can get a little tedious. 


Thanks,

Theo

 

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

ncraft
Frequent Visitor

Thanks @TheoC , The calculated column helps me build a simple test to make sure the solution works, but in the end, I still need the solution to be written in Power Query. 

The reason I need it in Power M Query, is because I want to be able to perform a transformation on a separate table that uses a merge queries to bring the index over to a new table of related data. If the index column only exists as a calculated column, it wont be avalible in power query editor for transformations. 

For more detail, I have the first table of data described in my sample - lets call it the 'Calendar' table - but I also have a second table with more dates, names and groups - lets call it 'entries' 
It looks a little something like this:

NameGroupDate
A11/31/2022
A12/1/2022
B32/1/2022
B42/1/2022
C52/8/2022
C62/4/2022

 

Note how the name column on this 'entries' table matches the first name column on the 'calendar' table. None of the columns on the 'entries' table contain unique values, but any combination of [Name] & [Group] & [Date] will be unique. Also, any combination of [Group] & [Date] will be unique, but there will be duplicates for combinations of [Name] & [Date] and [Name] & [Group]. 

Once, I have the proper index column on the 'calendar' column, I plan to use merge quarries to then add that index column to the 'entries' table, such that the [date] values have the appropriate ranking by [name] on both tables. I need this to perform further calculations on those indexes by the [Group] values on the 'entries' table. 

 

@ncraft makes a lot of sense. I am glad that @Jihwan_Kim could provide the solution and PBIX to help. All the best in your Power BI journey! Theo

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.