在 PowerBI 中使用 ALLEXCEPT 实现分组百分比的问题

Issues with implementing grouped percentage with ALLEXCEPT in PowerBI

我想获得一个分组百分比作为新列或新度量(根据我在论坛中阅读的内容推荐)。我有一个数据,用户可以在其中使用切片器来获取各种百分比。但是,目前我的查询一直在失败。我访问了论坛,但似乎无法弄清楚如何分组并获得组的百分比。此外,我无法在查询编辑器中使用分组依据工具,因为我的数据非常大。所以,我不得不依赖我不太了解的 DAX。

这是我创建的度量,它不起作用,因为当我对第 1 季度的数据进行切片时,百分比加起来不等于 1.0,但它在“总计”行中显示:

Percent Values = CALCULATE (
    SUM (question[Count Values] ),
    ALLEXCEPT ( question, question[Date].[Quarter], question[Date].[Year],question[Processing Type],question[Gender], question[Race] )
)
    / CALCULATE (
        SUM ( question[Count Values] ),
        allselected()   )

请在此处找到 excel 数据文件:

https://github.com/TestingGround00/powerbi_question/blob/main/input_data_table.xlsx

我期待得到这样的结果:

非常感谢任何帮助。谢谢。

逗号分隔数据:

Race,Gender,visa_type,Count Values,visa_country,Processing Type,Date
White,Female,C,1,Canada,Custodial,2/14/2014 0:00
Other,Male,M,5,Mexico,Express,1/20/2014 0:00
Hispanic,Male,R,6,Russia,Refugee,2/18/2014 0:00
White,Female,B,4,Brazil,Asylum,3/7/2014 0:00
Hispanic,Male,C,1,Canada,Refugee,4/11/2014 0:00
White,Female,R,7,Russia,Custodial,4/23/2014 0:00
White,Male,M,9,Mexico,Express,4/1/2014 0:00
Hispanic,Male,B,3,Brazil,Refugee,4/13/2014 0:00
White,Female,R,1,Russia,Express,7/31/2014 0:00
White,Male,C,7,Canada,Asylum,9/6/2014 0:00
White,Female,M,2,Mexico,Express,7/22/2014 0:00
Black,Female,B,5,Brazil,Custodial,8/13/2014 0:00
White,Male,R,1,Russia,Asylum,12/9/2014 0:00
White,Female,M,4,Mexico,Asylum,12/6/2014 0:00
Black,Female,B,6,Brazil,Express,12/13/2014 0:00
White,Male,C,7,Canada,Refugee,10/26/2014 0:00

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

编辑:

感谢@Phil Leh 的帮助。 不幸的是,结果还是有点偏差。请看下图:

本质上,我每个季度都在尝试将事件处理增加到 100%。但是,在对您的查询进行一些调整后,它已关闭。

你们真的很亲密。尝试将您的措施重写为以下内容(更详细的解释):

Percent Values =
-- calculation of the numerator
VAR num =
    SUM ( question[Count Values] )  -- numerator

-- calculation of the denominator
VAR denom =
    CALCULATE (
        SUM ( question[Count Values] ),
        -- usage auf ALLSELECTED() clear filter context from table visual,
        -- but keeps the filter values from slicers
        ALLSELECTED ()
    )
RETURN
    DIVIDE (
        nom,
        denom
    )

您得到“错误”结果的问题在于分子的计算。 此处您使用了 ALLEXCEPT(),但未包括字段 'question'[Count Values]'question'[visa_country].
在后台,table Power BI 为所有字段组合生成交叉连接。
下图展示了此 table 按年和季度过滤的输出(与您的切片器中的相同),并希望它更清晰:

突出显示的行就是您看到的行,因为它们将为 VAR num 生成一个值。图像输出是使用 DAX Studio

创建的

Update/Extension

要在您的编辑中完成任务,它会变得有点复杂,尤其是 rollups/totals

这是一个可能的 DAX 指标。主要是对上一个的扩展:

Percent Values =
VAR num =
    SUM ( question[Count Values] )

-- this one is new
VAR f =
    FILTER (
        -- clear all filter from our table
        ALL ( question ),
        -- and keep only the values matching the
        -- filter context's current quarter and year
        question[Date].[Quarter]
            = MAX ( question[Date].[Quarter] )
            && question[Date].[Year]
                = MAX ( question[Date].[Year] )
    )
VAR denom =
    IF (
        -- checking if Quarter is in current filter context
        ISINSCOPE ( question[Date].[Quarter] ),
        -- if yes, use the filter f (calculate denom for this quarter
        CALCULATE (
            SUM ( question[Count Values] ),
            ALLSELECTED (),
            f
        ),
        -- else, calculate the denom for all values
        -- this could also be BLANK() or some other calculation
        CALCULATE (
            SUM ( question[Count Values] ),
            ALLSELECTED ()
        )
    )
RETURN
    DIVIDE (
        num,
        denom
    )

在矩阵视觉中,结果将是

小提示:如果你想以百分比显示你的度量,点击它,在顶部的功能区你可以将度量格式化为百分比