ASC parameter when using TOPN function in Power BI

吃可爱长大的小学妹 提交于 2021-01-01 06:28:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!