【问题标题】:Getting amount for current date and prior week in same table在同一张表中获取当前日期和前一周的金额
【发布时间】:2020-05-03 20:09:33
【问题描述】:

我试图在同一张表中获取当前日期和 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 字段?我怀疑这涉及使用行上下文,但似乎无法让它工作。

【问题讨论】:

    标签: dax powerbi-desktop


    【解决方案1】:

    我得到了这个工作。诀窍是使用变量。更多信息和示例在下面的链接中,但我引用了相关细节。

    变量让您可以访问外部过滤器上下文。此外,DAX 在其定义的上下文中评估变量,而不是在一个 在哪里使用它们。

    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 
    

    【讨论】:

      猜你喜欢
      • 2014-05-18
      • 2023-02-08
      • 1970-01-01
      • 2017-09-16
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多