Forum Discussion
Dynamic Ranking based on date and key
Hi SunStorm,
Try the below for your measure:
Count of Key by Status =
VAR MaxDate = MAX ( 'Calendar'[Date] )
VAR SelStatus = SELECTEDVALUE ( table_1[objective_status] )
-- summarizes the relevant columns, filtering by date
VAR Summarized =
CALCULATETABLE (
SUMMARIZE ( table_1, table_1[key], table_1[entry_date], table_1[objective_status] ),
REMOVEFILTERS (),
'Calendar'[Date] <= MaxDate
)
-- ranks the summarized table for the latest entry date
VAR Ranked =
ADDCOLUMNS (
Summarized,
"Objective Rank",
ROWNUMBER (
Summarized,
ORDERBY ( table_1[entry_date], DESC ),
DEFAULT,
PARTITIONBY ( table_1[key] )
)
)
-- filters the ranked table for only the most recent entry date and matching objective status
VAR Filtered =
FILTER (
Ranked,
[Objective Rank] = 1 &&
[objective_status] = SelStatus
)
RETURN COUNTROWS ( Filtered ) + 0
I added comments in the measure to illustrate the logic, but essentially the measure:
- filters for only the relevant columns and records
- ranks the table above so we can filter for only the latest entry date per key
- filters the above table for only the latest entry dates and relevant objective status
- counts the rows in the above table
This is what my data model looks like:
Let me know if that's not what you were looking for.
----------------------------------
If this post helps, please consider accepting it as the solution to help other members find it quickly. Also, don't forget to hit that thumbs up and subscribe! (Oh, uh, wrong platform?)
Hey,
Thanks for a prompt response. It seems that my version of PowerBI (January 2023) doesn't have the ROWNUMBER function implemented. Would there be any workaround here?
- Wilson_2 years agoMemorable Member
SunStorm,
Is it possible to update your Power BI version? 🙂 (ROWNUMBER was introduced in April 2023 it seems.)
If not, I'm sure there's something you can do with WINDOW, which was introduced in late 2022 if I googled correctly.
----------------------------------
If this post helps, please consider accepting it as the solution to help other members find it quickly. Also, don't forget to hit that thumbs up and subscribe! (Oh, uh, wrong platform?)- SunStorm2 years agoFrequent Visitor
Unfortunately this is controlled by the admin in the organization and I have no influence over the software version we use 😐