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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Dynamic RANK on contract and monthly level not working

Hello Everyone,

 

I'm quite new to PoweBI. I've been reading posts here and trying many things to get a rank to work which I could get done in the back end with SQL rank and partition but I need to implement it in the dashboard to make it dynamic, depending on user selections.

 

So I have a set of lease contracts that have been restructured/modified due to COVID and other reasons since 2018. I need to rank the restructuring event(s) per contract depending on the date (year and month)  when they were restructured. So I need a RANK on contract level that tells me which restructure happened first, second etc depending on the ModifiactionDate slicer selection. Then taking the max of the rank I can count how many times each contract has been modified and calculate other KPIs I need like First wave mods (RANK = 1) and Second wave mods (RANK >=2). There are 2 restructure statuses: Booked (IsFinished i= 1 in the data model) and Pipeline(IsPipeline = 1) which the user can select through a slicer (Booked/Pipeline), and the rank needs to have the selection in consideration plus the ModificationDate Slicer (DAte between type).

 

This is what I should get if I don't filter out the ModifDate or Modification Status (SQL ranks on all history):

2021-01-21_12-04-29_2.jpg

 

All the necessary fields are within the same table: R_TEST and they are: 

CONTRACT_IDModifDateModifStatusIs RestructuredIsPipelineIsFinished

 

This is one of the DAX I tried that throws one for everything:

PBI RANK =
RANKX (
FILTER ( ALL ( R_TEST )
,R_TEST[IsFinished] = 1 && R_TEST[IsFinished] = 1
)
,DISTINCTCOUNT(R_TEST[CONTRACT_ID]) --R_TEST[OVERALL_SORT]
,--1/0
,ASC
,Skip --Dense
)

 

And here is how I made the ranks work in SQL:

,RANK() OVER (PARTITION BY Country, CONTRACT_ID ORDER BY Country, PartnerId, CONTRACT_ID, IsFinished DESC, ModifDate ) RNK_CONTRACT_ALL_MOD_HIST --For Booked and Pipeline Restructures
,(CASE WHEN IsFinished = 1
THEN RANK() OVER (PARTITION BY Country, CONTRACT_ID ORDER BY Country, PartnerId, CONTRACT_ID, IsFinished DESC, ModifDate )
END ) RNK_CONTRACT_BOOK_MOD_HIST --For Booked Restructures

 

BTW I already removed duplicates (restructures on the same contract during the same month) so that the ranks are unique

 

Here is a sample of the data set, help would be much appreciated!!!!!!!

COUNTRY_CODECONTRACT_IDPartnerIdModifDateModifReasonModifStatusIs RestructuredIsPipelineIsFinishedRNK_CONTRACT_ALLRNK_CONTRACT_ALL_MOD_HISTRNK_CONTRACT_BOOK_MOD_HISTRNK_CONTRACT_PIP_MOD_HISTRNK_CONTRACT_MOD_YR_ALLRNK_CONTRACT_BOOK_MOD_YRRNK_CONTRACT_PIP_MOD_YR
ATAT_95219_8AT13393371FCHARISMAAT-1356624/6/2020COVID-19Booked Mods101111NULL11NULL
ATAT_95219_8AT13393371FCHARISMAAT-13566211/4/2020COVID-19Pipeline Mods11022NULL12NULL1
DEDE_1_227482CHARISMADE-1442773/26/2020OtherBooked Mods101111NULL11NULL
DEDE_1_227482CHARISMADE-1442776/24/2020OtherBooked Mods101222NULL22NULL
DEDE_1_227482CHARISMADE-1442778/12/2020OtherBooked Mods101333NULL33NULL
DEDE_1_227482CHARISMADE-14427712/8/2020OtherPipeline Mods11044NULL14NULL1
DEDE_1_227485CHARISMADE-1442773/26/2020OtherBooked Mods101111NULL11NULL
DEDE_1_227485CHARISMADE-1442776/24/2020OtherBooked Mods101222NULL22NULL
DEDE_1_227485CHARISMADE-1442778/12/2020OtherBooked Mods101333NULL33NULL
DEDE_1_227485CHARISMADE-14427712/8/2020OtherPipeline Mods11044NULL14NULL1
DEDE_1_234766CHARISMADE-1625463/23/2020OtherBooked Mods101111NULL11NULL
DEDE_1_234766CHARISMADE-1625469/1/2020COVID-19Booked Mods101222NULL22NULL
DEDE_1_234766CHARISMADE-16254612/3/2020OtherBooked Mods101333NULL33NULL
ATAT_95219_AT2064097-1CHARISMAAT-1410091/15/2019OtherBooked Mods101111NULL11NULL
ATAT_95219_AT2064097-1CHARISMAAT-1410093/4/2020OtherBooked Mods101222NULL11NULL
ATAT_95219_AT2064097-1CHARISMAAT-14100911/6/2020COVID-19Booked Mods101333NULL22NULL

 

 

 

 

 

 

 

2 REPLIES 2
amitchandak
Super User
Super User

@Anonymous , Do you have any column in visual that do not below to table R_TEST  and not aggregated , if so rank will inside that column . Also, add calculate on distinctcount and check

PBI RANK =
RANKX (
FILTER ( ALL ( R_TEST )
,R_TEST[IsFinished] = 1 && R_TEST[IsFinished] = 1
)
,calculate(DISTINCTCOUNT(R_TEST[CONTRACT_ID])) --R_TEST[OVERALL_SORT]
,--1/0
,ASC
,Skip --Dense
)

 

For Rank Refer these links, if Needed
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-3-of-3-the-finale

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Anonymous
Not applicable

@amitchandak Yes all fields are in the same table, including the rank measure. I tried DISTINCTCOUNT but it didn't work. I'm not sure using DISTINCTCOUNT on CONTRACT_ID is what I need as I need to rank the CONTRACt IDs by ModifDate, not the unique values!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.