Forum Discussion
Complex Dax Query Help
// Calculate the period use for each day of use, looking ahead by the number of replenishment days for the product's class
var _PeriodUsage =
filter(
KEEPFILTERS(
SUMMARIZECOLUMNS(
//Products[SalesCode],
//'Flowline Stations'[StationId],
'Station Component Stock'[StationStockControlId],
Dates[Date],
_ReplenishmentPeriod,
_UpperBound,
_LowerBound,
_UsageClass,
_C_ClassReplen,
_B_ClassReplen,
_A_ClassReplen,
"DailyUsage", sum('Flowline Station Usage'[DailyUsage]),
"PeriodUsage", [Period Usage]
)
),
not(ISBLANK([DailyUsage]))
)
// Rank each period use for a product at a station Asc and Desc
var _RankedUsage =
ADDCOLUMNS(
'_PeriodUsage',
"Rank",
RANKX(filter(_PeriodUsage, 'Station Component Stock'[StationStockControlId] == EARLIER('Station Component Stock'[StationStockControlId])), [PeriodUsage],,asc, dense),
"ReverseRank",
RANKX(filter(_PeriodUsage, 'Station Component Stock'[StationStockControlId] == EARLIER('Station Component Stock'[StationStockControlId])), [PeriodUsage],,desc, dense)
)
table _RankedUsageTbl = _RankedUsage
// Find the rank at the 95th percentile of all ranks for each StationStockControlId
table _ThresholdRank =
ADDCOLUMNS(
FILTER(_RankedUsage,
[ReverseRank] == 1),
"ThresholdRank", roundup([Rank]*0.95, 0)
)
// **Missing Step** - Create a list of Period Usage for each StationStockControlId where the rank = the 95%ile rank above
Hi Greg_Deckler
Thank you for the response. I did reply the other day but I've revisited this topic and my latst post is missing? I even received a badge after I sent the reply so no idea how it disappeared!
Anyway, re-writing the reply below:
I'll provide a streamlined part of the query that focuses on the important part. Basically here's waht I'm trying to do:
For each StationStockControlId, Rank the PeriodUsage
Find the Rank at the 95th percentile potision for all PeriodUsage values for each StationStockControlId
Return a list with 1 row per StationStockControlId showing the value at the 95th percentile rank
the below query starts with calculating _PeriodUsage, I've provided a sample output from _PeriodUsage which will let the rest of the query run:
| StationStockControlId | Date | DailyUsage | PeriodUsage |
| 165 | 18/10/2021 00:00 | 1064 | 2169 |
| 165 | 25/06/2021 00:00 | 1064 | 1204 |
| 256 | 25/06/2021 00:00 | 1064 | 1204 |
| 256 | 18/10/2021 00:00 | 1064 | 2169 |
| 420 | 18/10/2021 00:00 | 1064 | 2169 |
| 420 | 25/06/2021 00:00 | 1064 | 1204 |
The part that has me stumped is how I can filter _RankedUsage by the results of _ThresholdUsage. If I was using sql this would be a join on StationStockControlId and Rank.
Ultimately, I am looking to get the PeriodUsage for each StationStockControlId where the PeriodUsage is ranked at the 95th percentile of all PeriodUsages for that StationStockControlId. If there is a better way to get the end result in DAX than the way I am trying, please advise!
Thanks