Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago

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):

 

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