年同比方差计算错误 "EARLIER/EARLIEST" 指的是不存在的较早的行上下文

Year over Year Variance Calculation Error "EARLIER/EARLIEST" refers to an earlier row context which doesn't exist

在尝试计算年同比差异时(现在 2 天未成功),我收到以下错误消息。

EARLIER/EARLIEST refers to an earlier row context which doesn't exist.

YOY Variance = var PreviousYearPrinBal = CALCULATE(SUM(Deals[Principal Balance]),FILTER(ALL(Deals[Close Date].[Year]),Deals[Close Date].[Year] = EARLIER(Deals[Close Date].[Year])))
return
if(PreviousYearPrinBal = BLANK(), BLANK(), Deals[PrincipalBalance] - PreviousYearPrinBal)

在另一个 SO 问题中,有一种不同的方法给出了以下错误:

A column specified in the call to function 'SAMEPERIODLASTYEAR' is not of type DATE. This is not supported.

yoy = CALCULATE([PrincipalBalance], SAMEPERIODLASTYEAR(Deals[Close Date].[Year]))

虽然我知道这些是什么意思,但我不知道如何修复它们。这是我的 table.

这就是我所期望的结果。

我已尝试在 Power BI 社区中发布此问题,但尚未收到答复。 Calculate Year over Year Variance.

实际数据样本:

1)创建年份和年份差异列(计算列)

Year = YEAR(Table1[Date])
Year Difference = Table1[Year] - Min(Table1[Year])

2) 创建方差(度量)

Variance = 

   Var current_YearDifference = SELECTEDVALUE(Table1[Year Difference])  

   Var Current_PrincipalBalance = CALCULATE(SUM(Table1[Principal Balance]),FILTER(ALL(Table1), Table1[Year Difference] = (current_YearDifference)))                                            

   Var Previous_PrincipalBalance = CALCULATE(SUM(Table1[Principal Balance]),FILTER(ALL(Table1), Table1[Year Difference] = (current_YearDifference - 1)))                                  

Return if(current_YearDifference <> 0, (Current_PrincipalBalance - Previous_PrincipalBalance), 0)

3) 最后根据百分比(度量)创建方差,

Variance in terms of Percentage = 

   Var current_YearDifference = SELECTEDVALUE(Table1[Year Difference])  

   Var Current_PrincipalBalance = CALCULATE(SUM(Table1[Principal Balance]),FILTER(ALL(Table1), Table1[Year Difference] = (current_YearDifference)))                                 

   Var Previous_PrincipalBalance = CALCULATE(SUM(Table1[Principal Balance]),FILTER(ALL(Table1), Table1[Year Difference] = (current_YearDifference - 1)))                                  

Return if(current_YearDifference <> 0, ((Current_PrincipalBalance - Previous_PrincipalBalance) / Previous_PrincipalBalance), 0)

我的最终输出

本金余额在输出的值窗格中选择了 SUM 函数 Table,其中年份是不汇总。

我的最佳实践

  1. 在创建复杂度量时总是使用 Vars 来简化 公式.
  2. 然后 return 仅 Measure 的一部分来检查输出是否符合预期。

请告诉我是否有帮助。