在同一时间获取当前日期和前一周的金额 table

Getting amount for current date and prior week in same table

我正在尝试在同一 table 中获取当前日期和之前 7(或 X)天的金额。

我有这个 PowerBI 矩阵 (TransactionDate, Amount),我需要将行日期减去 10 天的金额相加。

参见下面的示例

TransactionDate     Amount    Amount_TransactionDateMinus7
 01/10                 100               25     -- this is the amount for 1/3
 01/11                 150               33     -- this is the amount for 1/4
 01/12                 200               50
  ...
 01/17                 500              100     -- this ties to 1/10 date

为简单起见,我们假设数据源只是一个只有这两个字段的视图。 数据源:

TrxDate    Amount
1/3          20
1/3           5
...many rows
1/10         60
1/10         40
...

如何计算 Amount_TransactionDateMinus7 字段?我怀疑这涉及到使用行上下文,但似乎无法让它工作。

我成功了。诀窍是使用变量。更多信息和示例在下面的 link 中,但我引用了相关详细信息。

Variables let you access the outer filter context. Moreover, DAX evaluates variables in the context of their definition, not in the one where they are used.

https://www.sqlbi.com/articles/variables-in-dax/

我添加了一个新的计算列,如下所示。

VAR Prev_Week_Amount =
    CALCULATE(
        SUM('tbl'[Amount]),
        DATEADD('Date'[Date], -7, DAY)
    )
RETURN
    Prev_Week_Amount