How to do the equivalent of sumif using a date that matches a date from a separate table in power bi

huangapple go评论64阅读模式
英文:

How to do the equivalent of sumif using a date that matches a date from a separate table in power bi

问题

我正在尝试在Power BI中的两个表格中根据匹配的日期来对一行进行求和。目标是使每个表格中的日期都匹配,并在新列中提供基于日期的不同记录的总计数。示例 SUMIF

失败的代码

英文:

I am trying to sum a row based on matching dates from 2 tables in power bi. The goal is to have both dates match from each table with the new column provided the total count of distinct records based on the date example sumif

[Failing code] (https://i.stack.imgur.com/vobAh.png)

答案1

得分: 0

在关系视图中,只需在日期列上建立Table1Table2之间的关系。这应该是一种以Table2为一端,Table1为多端的关系。这样,Table2可以向Table1传递筛选条件。

在建立关系之后,在Table2中简单添加一个计算列:

Sumif = 
CALCULATE ( SUM ( 'Table1'[Count] ) )

您还可以添加一个简单的度量:

SimpleSum = SUM ( 'Table1'[Count] )

然后将其与Table2[Date]一起添加到表可视化中。这将为您提供所需的结果,而无需将计算列持久保存到数据模型中。除非您计划将添加的列用作切片维度或类别,否则无需将其持久保存在数据模型中,使用度量是最佳实践。

编辑:您的原始DAX代码出错是因为您未在FILTER函数中关闭括号。正确的代码是:

Col =
SUMX (
    FILTER (
        'acumatica wv_install_revenue_nopids',
        'acumatica vw_install_revenue_nopids'[Revenue Recognition Date] = 'Revenue Recognition Date'[Rev_Recog_Date]
    ),
    'acumatica wv_install_revenue_nopids'[Confirmed]
)

我建议您开始系统地格式化您的代码,以便更容易发现错误 - 并且我建议避免使用过长的表名,例如acumatica vw_install_revenue_nopids - 这会使错误更难发现。

英文:

Just establish a relationship between Table1 and Table2 on the date columns, in the relationship view. This should be a relationship with Table2 on the one-side of the relationship, and Table1 on the many-side. This way, Table2 can pass filters to Table1.

After the relationship is established, simply add a calculated column in Table2:

Sumif = 
CALCULATE ( SUM ( 'Table1'[Count] ) )

You can also add a simple measure:

SimpleSum = SUM ( 'Table1'[Count] )

And add this to a table visual together with Table2[Date]. This will also give you what you are after, without having to persist a calculated column to your data model. Unless you are planning to use the added column as a slicing dimension or a category, you do not need to persist it in your datamodel, and using a measure is the best practice.

Edit: Your original DAX code is throwing the error you see because you haven't closed your FILTER function with a parenthesis. The correct code is:

Col =
SUMX (
    FILTER (
        'acumatica wv_install_revenue_nopids',
        'acumatica vw_install_revenue_nopids'[Revenue Recognition Date] = 'Revenue Recognition Date'[Rev_Recog_Date]
    ),
    'acumatica wv_install_revenue_nopids'[Confirmed]
)

I suggest you start formatting your code systematically to make this easier to spot - and I would recommend avoiding ridiculously long table names like acumatica vw_install_revenue_nopids - makes it even harder to spot mistakes.

huangapple
  • 本文由 发表于 2023年6月13日 02:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定