Hans Cornette
03/29/2023, 2:50 PMFrancisco Antunes
03/29/2023, 4:11 PMSELECT RANK (SUM (Spend)) WHERE TOP (3%) IN (SELECT SUM (SPEND))
As for all the values displaying 1.00, this is most likely being caused by your use of the WITHIN clause. What is the goal of the WITHIN (ALL Other)
clause? Have you tried not using it altogether?Hans Cornette
03/29/2023, 7:19 PMSELECT RANK (SUM (Spend)) WHERE TOP (3%) IN (SELECT SUM (SPEND))
What I would like to display is 3%. Doing so, make the user understand that this cost center is in the top 3% tier of the spend. 1) Can that be done?
and 2) Also show a mere ranking number like 4. This cost center is the fourth most spending of them all. I tried select RANK (SUM (Spend)) but got nonsensical answers...Francisco Antunes
03/30/2023, 10:05 AMHans Cornette
03/30/2023, 10:13 AMMichal Hauzírek
04/01/2023, 1:09 PMSELECT MIN(
SELECT RANK(spend sum) DESC
BY CostCenterId ALL OTHER
WITH PF EXCEPT CostCenterId
)
here the spend sum
is a simple metric SELECT SUM(spend)
,
the BY CostCenterId ALL OTHER
part makes sure the rank is calculated internally just by the cost center even if it is not broken by it in the insight (i.e. it is a KPI)
and the WITH PF EXCEPT CostCenterID
part ensures that for this calculation the filter for cost center is ignored and therefore the ranking is calculated from all cost centers even if only one is selected
the outer MIN
function just makes sure it can return a single number even if there would be multiple cost centers selected and ensures the inner calculation logic remains unchanged
The Percentage
To get the percentage represented by the particular cost center, the TOP filter will not help you. You will need to use the CUME_DIST or PERCENT_RANK functions. And the metric logic will be the very same:
SELECT MIN(
SELECT CUME_DIST(spend sum) DESC
BY Cost Center Id ALL OTHER
WITH PF EXCEPT Cost Center Id
)
Please note that if you select more cost centers, these metrics will give you minimum of their rank and percentile (because of that MIN part).
I hope this helps.