问题
I've got this data:
Then have this measure:
amount = SUM( play[amount] )
Then I've tried to use the ASC/DESC
arguments of the TOPN
function in these two measures:
Top 2 customer per category ASC =
VAR rnk = VALUES( play[customer] )
RETURN
CALCULATE(
[amount],
TOPN(
2,
ALL( play[customer] ),
[amount],
ASC
),
RNK
)
Top 2 customer per category DESC =
VAR rnk = VALUES( play[customer] )
RETURN
CALCULATE(
[amount],
TOPN(
2,
ALL( play[customer] ),
[amount],
DESC
),
RNK
)
Now if I use these two measures it looks like the following:
What is going on?
Why is the measure Top 2 customer per category ASC
showing nothing?
How do I amend that measure so that it shows values for the bottom two values of each category?
回答1:
The problem here is that the second argument of TOPN
should be a table, not an unfiltered column.
Regardless of what the category
is, ALL(play[customer])
returns the table:
customer
--------
xx
yy
zz
jj
qq
ff
The measure [amount]
is still evaluated within the category
filter context though so for category = "a"
you get
customer [amount]
------------------
xx 10
yy 12
zz 13
jj
qq
ff
and for category = "b"
you get
customer [amount]
------------------
xx
yy
zz
jj 15
qq 16
ff 9
These blanks are considered smaller than any number so they are what gets selected when you sort ASC
.
Try this slightly modified measure instead:
Top 2 customer per category ASC =
VAR rnk = VALUES ( play[customer] )
RETURN
CALCULATE (
[amount],
TOPN ( 2, CALCULATETABLE ( play, ALL ( play[customer] ) ), [amount], ASC ),
RNK
)
Using CALCULATETABLE
, the category
filter context gets preserved.
P.S. To generate the tables above you can write a new calculated table like this:
Top2Table =
CALCULATETABLE (
ADDCOLUMNS ( ALL ( play[customer] ), "amount", [amount] ),
play[category] = "a" <or "b">
)
来源:https://stackoverflow.com/questions/58291621/asc-parameter-when-using-topn-function-in-power-bi