简体   繁体   English

Power Bi:Top N 视觉级别过滤器作为 Measure

[英]Power Bi: Top N visual level filter as Measure

Need a measure that will provide the same output as TopN visual level filter (than I can parameterize it).需要一个将提供与 TopN 视觉级别过滤器相同的 output 的度量(比我可以参数化它)。

The solution for simple cases provided HERE此处提供的简单案例的解决方案

But it doesn't work for more complicated cases...但它不适用于更复杂的情况......

EXAMPLE:例子:

Don't work if you add any dimension that has Many to One Product Name relationship ( Order Number for example).如果您添加任何具有多对一Product Name关系的维度(例如Order Number ),则不起作用。

Desired output: both tables (top and bottom) should be equal:所需的 output:两个表(顶部和底部)应该相等: 在此处输入图像描述 在此处输入图像描述

Example from screen available here HERE 此处提供的屏幕示例

NB!注意! From usability perspective it's preferable to return Sales Rank in measure.从可用性的角度来看,最好返回 Sales Rank 的度量值。

Here you go.这里是 go。

在此处输入图像描述

在此处输入图像描述

BottomN = 

VAR param = [TopN Value]
VAR topNTable =  CALCULATETABLE( TOPN(param,'Product', [Sales Amount], ASC), ALLSELECTED('Product'[Category],'Product'[Product Name]), FILTER(allselected(Sales), [Sales Amount] <> BLANK()))
RETURN 
IF(NOT(ISEMPTY(Sales)),IF( SELECTEDVALUE('Product'[Product Name]) IN SELECTCOLUMNS( topNTable,"a", 'Product'[Product Name]) ,[Sales Amount]))

A bit reworked solution from David Bacci :来自David Bacci的一点修改解决方案:

1. If you need just TopN Sales : 1.如果您只需要TopN Sales

TopnSalesAmount = 
VAR param = [TopN Value]
VAR topNTable =
    CALCULATETABLE (
        TOPN ( param, 'Product', [Sales Amount], ASC ),        
        ALLSELECTED ( 'Product'[Product Name] ),
        FILTER ( ALLSELECTED ( Sales ), [Sales Amount] <> BLANK () )
    )
RETURN
    IF (
        NOT ( ISEMPTY ( Sales ) ),
        IF (
            SELECTEDVALUE ( 'Product'[Product Name] )
                IN SELECTCOLUMNS ( topNTable, "a", 'Product'[Product Name] ),
            [Sales Amount]
        )
    )

在此处输入图像描述

2. If you need Rank for TopN Sales: 2.如果您需要 TopN Sales 的排名

rnkTopnSalesAmount = 
IF (
  //ISINSCOPE ( 'Product Names'[Product Name]), -- depends, which one is used in visual
    ISINSCOPE ( 'Product'[Product Name] ) 
    && NOT ( ISEMPTY ( Sales ) ), 

    VAR ProductsToRank = [TopN Value]
    VAR topNTable =
        CALCULATETABLE (
            TOPN (
                ProductsToRank,
                ADDCOLUMNS ( VALUES ( 'Product'[Product Name] ), "@Amt", [Sales Amount] ),
                [Sales Amount], ASC
            ),
            FILTER ( ALLSELECTED ( Sales ), [Sales Amount] <> BLANK () )
        )
    RETURN
        IF (
            SELECTEDVALUE ( 'Product'[Product Name] )
                IN SELECTCOLUMNS ( topNTable, "a", 'Product'[Product Name] ),
            RANKX ( topNTable, [@Amt], [Sales Amount], ASC )
        )
)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM