根据累积度量切换DAX折扣

问题描述 投票:1回答:1

我有点困难时间绕着这个问题。我有一个基于小时的累计总数,我需要计算回扣。这里的规则是,在总额超过金额后,更改回扣百分比(10%,15%)。无论我如何尝试,我最终得到错误的总数或错误的百分比。

这是我失败的方法:

+------------+-------------+------------+
| Day        | BilledHours | LaborPrice |
+------------+-------------+------------+
| 07/01/2018 | 98          | 13000      |
+------------+-------------+------------+
| 07/02/2018 | 89          | 12000      |
+------------+-------------+------------+
| 07/03/2018 | 80          | 11000      |
+------------+-------------+------------+
| 07/04/2018 | 92          | 9000       |
+------------+-------------+------------+
| 07/05/2018 | 52          | 8000       |
+------------+-------------+------------+
| 07/06/2018 | 73          | 7000       |
+------------+-------------+------------+
| 07/07/2018 | 82          | 11000      |
+------------+-------------+------------+


Cumulative Hours = 
CALCULATE(
    SUM(Rebates[BilledHours]),
    FILTER(
        ALL(Rebates[Day]),
        Rebates[Day]<=MAX(Rebates[Day]))
        )

Rebate = 
SUMX(Rebates,
    SWITCH(
        TRUE(),
        [Cumulative Hours]<=400,Rebates[LaborPrice] * 0.10,
        [Cumulative Hours]>=401,Rebates[LaborPrice] * 0.15)
        )

enter image description here

我怎样才能获得正确的回扣和总额?如果我在总和之前使用SWITCH,我最终会得到不正确的总数(因为总数总是> 400)。

powerbi dax measure
1个回答
2
投票

首先,修改您的累计小时数量,如下所示:

Cumulative Hours =
CALCULATE (
    SUM ( Rebates[BilledHours] ),
    FILTER ( ALL ( Rebates ), Rebates[Day] <= MAX ( Rebates[Day] ) )
)

二,修改回扣措施:

Rebate =
SUMX (
    Rebates,
    VAR Cumulative_Hours = [Cumulative Hours]
    RETURN
        SWITCH (
            TRUE (),
            Cumulative_Hours <= 400, Rebates[LaborPrice] * 0.1,
            Cumulative_Hours >= 401, Rebates[LaborPrice] * 0.15
        )
)

结果:enter image description here

为什么你的公式不起作用:

创建一个简单的测试度量并将其可视化:

   Wrong Rebate Hours = SUMX( Rebates, [Cumulative Hours])

enter image description here

很容易看出SUMX中没有“累积” - [累积小时数]停止累积,因此您的SWITCH测试总是<400。实际上,SUMX通过引入行来修改[累计小时]度量的过滤器上下文来自Rebates的上下文(通过“上下文转换” - 在Google上查找概念)。

© www.soinside.com 2019 - 2024. All rights reserved.