Power Bi Dax,仅在最后日期使用过滤器的总和列

发布于 2025-01-31 10:36:31 字数 465 浏览 0 评论 0原文

我有一个'发票'带有不同日期的金额,我想总结最大日期的金额。 例如,如果最大日期为25/03/2022,我将仅总结此日期的行。

last_month_amount = calculate(SUM(invoice[amount]),FILTER(invoice, invoice[date] = LASTDATE('invoice'[date])))

但是它可以计算所有金额,而无需任何过滤器。

在SQL中,我想要这个:

select sum(invoice.amount)
from invoice
where invoice.date = (select max(date) from invoice)

但是我明白了

select sum(invoice.amount)
from invoice

I've a table 'invoice' of amount with different Dates, i want to sum the amount for the max date.
For example if the max date is 25/03/2022, i'll sum only the rows of this date.

last_month_amount = calculate(SUM(invoice[amount]),FILTER(invoice, invoice[date] = LASTDATE('invoice'[date])))

but it calculate all the amounts without any filter.

in sql i want this:

select sum(invoice.amount)
from invoice
where invoice.date = (select max(date) from invoice)

but i got this

select sum(invoice.amount)
from invoice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2025-02-07 10:36:31

公式中有两个错误:

  1. 您在发票表上使用LastDate函数,但应在适当的日历表上使用(预计会连续日期);
  2. 您将持久的计算和过滤功能放置。结果,它计算这些功能的过滤器上下文中的最后一个日期(也就是说,它计算每个记录的最后日期,而不是整个表的最后日期)。

要解决问题,只需将最后一个日期存储在变量中:

last_month_amount =
VAR Latest_Date = MAX ( 'invoice'[date] )
RETURN
    CALCULATE ( SUM ( invoice[amount] ), 'invoice'[date] = Latest_Date )

There are two errors in your formula:

  1. You are using LASTDATE function on the invoice table, but it should be used on a proper calendar table (it expects continuous dates);
  2. You placed LASTDATE inside the CALCULATE and FILTER functions. As a result, it computes the last date within the filter context of these functions (that is, it calculates the last date for each record instead of the last date for the entire table).

To solve the problem, simply store the last date in a variable:

last_month_amount =
VAR Latest_Date = MAX ( 'invoice'[date] )
RETURN
    CALCULATE ( SUM ( invoice[amount] ), 'invoice'[date] = Latest_Date )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文