如何在Power BI中使用DAX中的过滤器计算百分比?

时间:2020-10-30 06:59:57

标签: powerbi dax powerbi-desktop

我有两列

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"
)

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

2 个答案:

答案 0 :(得分:2)

您的测量失败,因为细分不能同时是多个值。尝试以下方法:

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

答案 1 :(得分:0)

您的数据中没有特定条件的客户:细分为“ 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 )