【问题标题】:How to calculate percentage using filters in DAX in Power BI?如何在 Power BI 中使用 DAX 中的筛选器计算百分比?
【发布时间】:2021-02-12 15:39:14
【问题描述】:

我有两列

CustomerCode | Segmentation

AU656 | abc
AU765 | cdf
AU563 | abc
AU235 | abc
AU324 | opr
AU908 | opr
AU123 | pqr
AU234 |pqr 

我必须找到一个不同的 CustomerCode 计数,其中分段为“abc”、“cdf”和“pqr”,并将其除以 CustomerCodes 的总数(全部)。

我创建了一个度量 -

#RSP =
CALCULATE (
    DISTINCTCOUNT ( 'Table'[CustomerCode] ),
    FILTER ( ALL ( 'Table' ), 'Table'[Segmentation] = "abc" ),
    'Table'[Segmentation] = "cdf",
    'Table'[Segmentation] = "opr"
)

但是,这没有任何价值。我是否使用错误的过滤器? 如何计算?

【问题讨论】:

    标签: powerbi dax powerbi-desktop


    【解决方案1】:

    在您的数据中,您没有满足特定条件的客户:细分为“abc”、“cdf”和“pqr”(任何行都不匹配)。您应该使用 IN(也可以使用 OR)

    #RSP = 
    CALCULATE(DISTINCTCOUNT('Table'[CustomerCode]),FILTER(ALL('Table'),'Table'[Segmentation] in ("abc","cdf","opr")))
    

    如果您想查找具有多个分段代码的行的客户:

    #RSP_2 =
    var __custSeg1 = filter('Table'[CustomerCode], TREATAS({"abc"}, 
    'Table'[Segmentation])
    var __custSeg2 = filter('Table'[CustomerCode], TREATAS({"cdf"}, 
    'Table'[Segmentation])
    var __custSeg2 = filter('Table'[CustomerCode], TREATAS({"opr"}, 
    'Table'[Segmentation])
    
    return
    calculate(DISTINCTCOUNT('Table'[CustomerCode]), __custSeg1 ,__custSeg2 
    ,__custSeg2 )
    

    【讨论】:

      【解决方案2】:

      您的测量失败,因为分段不能同时是多个值。试试这个:

      #RSP =
      CALCULATE (
          DISTINCTCOUNT ( 'Table'[CustomerCode] ),
          'Table'[Segmentation] IN { "abc", "cdf", "opr" }
      )
      
      Ratio = DIVIDE ( [#RSP], DISTINCTCOUNT ( 'Table'[CustomerCode] ) )
      

      【讨论】:

      • 嗨,我做了这样的事情。你认为这是对的吗? CALCULATE(DISTINCTCOUNT('Table'[CustomerCode]), FILTER(ALL('Table'),SEARCH("abc",Table'[Segmentation],,BLANK())) || SEARCH("pqr",Table'[Segmentation ],,BLANK()) || SEARCH("xyz",'table'[Segmentation],,BLANK())))
      • 看起来很合理。不过,我建议使用CONTAINSSTRING('Table'[Segmentation], "xyz") 而不是 SEARCH 函数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      相关资源
      最近更新 更多