计算连续行之间的百分比差异 - Power BI

Calculate percentage difference between consecutive rows - Power BI

我有一个 table 结构如下:

Forecast Type     Group     Forecast 
June 00             A          120
July 01             A          125
June 00             B          250
July 01             B          222
June 00             C          308
June 01             C          270

我想通过按“预测类型”分组并通过对“预测”求和进行聚合来将其可视化为矩阵。然后,我想创建一个计算列或指标,其中包含预测类型之间的百分比差异。如果用户使用切片器按“组”过滤,则也应相应更新。这是选择所有组后矩阵的样子:

                  Forecast      Difference in EUR      Percentage Difference
June 00           678                  null                        null            
July 01           617                  -61                          -9%

通过创建索引列并使用以下方法,我能够通过一些研究创建“欧元差异”变量:

Difference in EUR = 
VAR RowAbove=
    CALCULATE (
        SUM ('Forecast Development'[Forecast]),
            FILTER ('Forecast Development', 'Forecast Development'[Index] = EARLIER ('Forecast Development'[Index]) -1))
    RETURN
    'Forecast Development'[Forecast] - RowAbove

但是,我无法使用此计算列公式创建百分比。此外,在“欧元差异”列中,第一行不为空,因为我没有其他值来计算差异。 我还尝试通过创建另一个计算列并过滤掉 June 00 的“预测类型”来强制第一行为空,但显然我不能这样做。

我也不确定如何创建索引,目前我只是正常地从 1 到 6 创建索引。但是也许我需要在每次组更改时重新启动索引?那么,1,2,1,2,1,2?

我是 Power BI 的新手,希望得到帮助。谢谢!

如果您的[预测类型]最后总是包含有效数字,那么我们可以这样计算:

Diff = 
var __CurrentType = value( RIGHT(SELECTEDVALUE(Sheet1[Forecast Type]),2))
var _prevForecast = CALCULATE( sum(Sheet1[Forecast]), filter(all(Sheet1[Forecast Type]), value( RIGHT(Sheet1[Forecast Type],2)) = __CurrentType -1 ))
var __currForecast = CALCULATE(sum(Sheet1[Forecast]))
return
if (ISBLANK(_prevForecast), BLANK(),
__currForecast - _prevForecast)


Percentage%% = 
var __CurrentType = value( RIGHT(SELECTEDVALUE(Sheet1[Forecast Type]),2))
var _prevForecast = CALCULATE( sum(Sheet1[Forecast]), filter(all(Sheet1[Forecast Type]), value( RIGHT(Sheet1[Forecast Type],2)) = __CurrentType -1 ))
var __currForecast = CALCULATE(sum(Sheet1[Forecast]))
return
DIVIDE(__currForecast, _prevForecast)