Forum Discussion
Daretoexplore
2 years agoAdvocate I
Ranking query
Hi everybody I have a dataset such as the following: Customer ID. TransactionDate(UK format) CUST001. 12/2/24 CUST001. 13/2/24 CUST001. 14/2/24 CUST002. ...
- 2 years ago
Using DAX :
TransactionRank = RANKX( FILTER( 'YourTableName', 'YourTableName'[Customer ID] = EARLIER('YourTableName'[Customer ID]) ), 'YourTableName'[TransactionDate], , ASC, Dense )Using M :
- Go to "Home" > "Edit Queries" to open the Power Query Editor.
- Select the table you want to modify.
- Go to "Add Column" tab > "Index Column" > "Custom Index".
- Start the index from 1 and increment by 1.
- Before applying the custom index, you might want to ensure your rows are sorted by "Customer ID" and then by "TransactionDate". You can do this by selecting the "Sort Ascending" option on the "Customer ID" column and then doing the same for the "TransactionDate" column.
- Use the "Group By" feature to group by "Customer ID", and within each group, sort by "TransactionDate" and then add the index column. This would require some advanced M code manipulation.
= Table.Group( YourTableName, ["Customer ID"], { "Data", each Table.AddIndexColumn( Table.Sort(_, {"TransactionDate", Ascending}), "TransactionRank", 1, 1, Int64.Type ), type table } )
AmiraBedh
2 years agoSuper User
Using DAX :
TransactionRank =
RANKX(
FILTER(
'YourTableName',
'YourTableName'[Customer ID] = EARLIER('YourTableName'[Customer ID])
),
'YourTableName'[TransactionDate],
,
ASC,
Dense
)
Using M :
- Go to "Home" > "Edit Queries" to open the Power Query Editor.
- Select the table you want to modify.
- Go to "Add Column" tab > "Index Column" > "Custom Index".
- Start the index from 1 and increment by 1.
- Before applying the custom index, you might want to ensure your rows are sorted by "Customer ID" and then by "TransactionDate". You can do this by selecting the "Sort Ascending" option on the "Customer ID" column and then doing the same for the "TransactionDate" column.
- Use the "Group By" feature to group by "Customer ID", and within each group, sort by "TransactionDate" and then add the index column. This would require some advanced M code manipulation.
= Table.Group(
YourTableName,
["Customer ID"],
{
"Data",
each Table.AddIndexColumn(
Table.Sort(_, {"TransactionDate", Ascending}),
"TransactionRank",
1,
1,
Int64.Type
),
type table
}
)
- Daretoexplore2 years agoAdvocate I
Thank you so much
- AmiraBedh2 years agoSuper User
Welcome ! Good luck with M it is a little bit tricky sometime 😄