仅针对一个类别使用 DAX 计算按类别划分的总计百分比

Calculating the percentage of grand total by category with DAX for one category only

我需要帮助计算 DAX 中一列(基于总计)中每个类别的百分比,但对于一个特定类别。

这就是数据的结构。每行都是带有 ID 列和项目列的单独交易。

我需要获取百吉饼交易的百分比。这是我目前使用的 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 的新手,不知道从哪里开始。

谢谢。

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

我用过以下:

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)

希望这就是您要找的!