仅使用一种类别的DAX计算按类别的总计的百分比

时间:2020-09-01 17:20:44

标签: powerbi dax percentage

我需要帮助来计算DAX中某个类别中某一类别中每个类别的百分比(基于总计)。

这是数据的结构方式。每行都是具有ID列和item列的单独交易。

enter image description here

我需要获取仅用于百吉饼的交易百分比。这是我目前使用的sql代码。

`Select 100 - CAST(count([Items]) 
 - count(case [Items] when 'Bagel' then 1 else null end) AS FLOAT) 
 / count([Items]) * 100 as Percent_Bagel 
 from Items_Table where Items != 'Coffee' 
 and Items != 'Muffin'`

如果可能的话,我需要将其转换为DAX公式以用于Power BI度量,但是我对DAX并不陌生,也不知道从哪里开始。

谢谢。

1 个答案:

答案 0 :(得分:2)

您的“正确”实现始终取决于上下文。您可以通过不同的方法实现目标。

我使用了以下内容:

Measure = 
DIVIDE(
    -- Numerator: Filter all Bagel's transaction and count them
    CALCULATE(
        COUNT('Table'[Transaction ID]),
        FILTER('Table', 'Table'[Item] = "Bagel")
    ),
    -- Denominator: Remove any filter - essentially fixing the full table - and count all transactions we have
    CALCULATE(
        COUNT('Table'[Transaction ID]), 
        ALL('Table'[Item])
    ),
    -- If something goes wrong with the DIVIDE, go for 0
    0
)

您还可以使用过滤器排除所有非空白的度量。

Without measure filter

With measure filter (Other categories are gone)

希望这就是您想要的!