I'm looking to add an index column, but have it increase according to a certain column value. Let me give an example; let's say my data is:
Group | Date |
A | 18-Apr |
A | 19-Apr |
A | 23-Apr |
A | 1-May |
B | 21-Apr |
B | 21-Apr |
B | 30-Apr |
B | 4-May |
And I would like to have the indices show like this:
Group | Date | Index |
A | 18-Apr | 1 |
A | 19-Apr | 2 |
A | 23-Apr | 3 |
A | 1-May | 4 |
B | 21-Apr | 1 |
B | 21-Apr | 2 |
B | 30-Apr | 3 |
B | 4-May | 4 |
How can I perform this dynamically?
Solved! Go to Solution.
Thats like an index on a table partition. You can create that by using grouping on the column and returning "_" - which means that all column of the table (but only for the specific value in the column) will be return. You then nest your Index-command in:
let Source = Table1, Partition = Table.Group(Source, {"Group"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}), #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Date", "Index"}, {"Date", "Index"}) in #"Expanded Partition"
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
Hi ImkeF,
Your solution works great! You are so smart! Thank you!
Excellent solution - and thanks for uploading the video.
Just one question, how can I choose which column to rank on?
Runar Wigestrand
Not sure if I understood you correctly, but I think you mean the field/column to group on? In this example it was also called Group:
Partition = Table.Group(Source, {"Group"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
You are a genious!!!
Hi, Imke.
Thanks for the quick reply. I was thinking more about if you have duplicates and want to keep only the newest record based on date. Then I want to group on one column and rank on another column ( for example the date column).
Something like this:
Group on SerialNum, rank on date column
Thanks.
Runar
Oh, I see: You need to fold in a Table.Sort-operation that will be executed before the Index is added. Sth like this:
Partition = Table.Group(Source, {"SerialNum"}, {{"Partition", each Table.AddIndexColumn(Table.Sort(_,{{"Date_Updated", Order.Ascending}}), "Index",1,1), type table}}),
Does that work for you?
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
Hi, Imke.
Perfect!
Thank you!
Runar Wigestrand
YES!!! Imke you are an absolute star 🙂
I have been stuck on this for weeks and weeks. The solution you have provided works perfectly. Thank you so much! What a great way to start the day...