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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
VikasD10
Frequent Visitor

Circular dependency

Hello, I want to create Rank in M-query. I have Participant ID and there status completed,Not completed,Deleted and Date Time of that Status. I want to create Rank based on below criteria: If Participant ID found duplicate for Completed I'd rank start with 1 for each Completed ID(New I'd start from 1). Not completed start from A. Deleted start from 1001. I want to create Rank during M-query

4 REPLIES 4
AmiraBedh
Resident Rockstar
Resident Rockstar

You need to sort your table by the Participant ID and then by the Date Time of the status to make sure that the rank is applied in the correct order.

Then you need to add a custom column that assigns a starting rank based on the status.

You will group your data by Participant ID and then apply the ranking logic within each group. This is where you will also reset the rank for each new 'Completed' ID.

Inside the for exampleGroup Byfor example operation, you'll need to create a custom column that applies the ranking logic based on the category rank you've defined.

For 'Completed', you'll increment numerically.

For 'Not Completed' and 'Deleted', you might need to handle these as special cases, depending on how you want to increment them ('A', 'B', 'C', ... for 'Not Completed' and numeric for 'Deleted').

Since M-query doesn't have a direct function for ranking with such specific conditions, you'll often need to use a combination of for exampleTable.AddIndexColumnfor example (to add a numeric index) and custom logic to reset and apply ranks based on your rule.

 

let
    Source = TableSource, // put your source table here
    SortedTable = Table.Sort(Source, {{"Participant ID", Order.Ascending}, {"Date Time", Order.Ascending}}),
    AddCategoryRank = Table.AddColumn(SortedTable, "Category Rank", each if [Status] = "Completed" then 1 else if [Status] = "Not Completed" then "A" else 1001),

    Grouped = Table.Group(AddCategoryRank, "Participant ID", {"Data", each _, type table}),

    Ranked = Table.TransformColumns(Grouped, {"Data", each Table.AddIndexColumn(_, "Rank", 1, 1, Int64.Type)})
in
    Ranked

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

Error :A cyclic reference was encountered during evaluation 

Can you share some data of your original column ?


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

17110128572773563117781962325834.jpg

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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