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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Daretoexplore
Frequent Visitor

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.           12/2/24

CUST002.           13/2/24

CUST002.           14/2/24

 

I'm trying to write DAX or write some M Code to Rank each of the transactions per customer in date order.

The solution I am looking for would be a column which reads 

1

2

3

1

2

3

 

(this is based on my example above)

 

Many thanks

1 ACCEPTED SOLUTION
AmiraBedh
Super User
Super User

Using DAX : 

TransactionRank = 
RANKX(
    FILTER(
        'YourTableName',
        'YourTableName'[Customer ID] = EARLIER('YourTableName'[Customer ID])
    ),
    'YourTableName'[TransactionDate],
    ,
    ASC,
    Dense
)

 

Using M :

  1. Go to "Home" > "Edit Queries" to open the Power Query Editor.
  2. Select the table you want to modify.
  3. Go to "Add Column" tab > "Index Column" > "Custom Index".
  4. Start the index from 1 and increment by 1.
  5. 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.
  6. 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
    }
)

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

3 REPLIES 3
AmiraBedh
Super User
Super User

Using DAX : 

TransactionRank = 
RANKX(
    FILTER(
        'YourTableName',
        'YourTableName'[Customer ID] = EARLIER('YourTableName'[Customer ID])
    ),
    'YourTableName'[TransactionDate],
    ,
    ASC,
    Dense
)

 

Using M :

  1. Go to "Home" > "Edit Queries" to open the Power Query Editor.
  2. Select the table you want to modify.
  3. Go to "Add Column" tab > "Index Column" > "Custom Index".
  4. Start the index from 1 and increment by 1.
  5. 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.
  6. 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
    }
)

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Thank you so much

Welcome ! Good luck with M it is a little bit tricky sometime 😄


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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.

Top Solution Authors
Top Kudoed Authors