简体   繁体   English

DAX-Power BI:如何避免计算列中的循环依赖

[英]DAX-Power BI: How to avoid circular dependency in calculated column

I'm working in a report in PowerBi Desktop and I have multiple tables, some have a Period Column with the value but a pair of them don't have it.我正在编写 PowerBi 桌面的报告,我有多个表,有些表有一个带有值的期间列,但其中一对没有。 I can calculate the Period based in the record dates and another table that has the period name and the data ranges.我可以根据记录日期和另一个包含期间名称和数据范围的表来计算期间。 The problem comes when I'm trying to establish the relation between the tables based on the periods due to the circular dependency between the calculated periods and the main table for the relationships.当我试图基于周期建立表之间的关系时,问题就出现了,因为计算的周期和关系的主表之间存在循环依赖关系。 The idea of the relations is to be able to filter the report according to the periods.关系的想法是能够根据期间过滤报告。

To simplify the data, this is the problem: I have an issue (circular dependency) trying to add a relationship between table Periods[PeriodName] and Income[CalculatedPeriod]为了简化数据,这就是问题所在:我有一个问题(循环依赖)试图在表Periods[PeriodName]Income[CalculatedPeriod]之间添加关系

I need to be able to use a single filter for both Income and Outcome in the report view.我需要能够在报告视图中对收入和结果使用单个过滤器。 Usually, this is done using the key that relates all the tables.通常,这是使用关联所有表的键来完成的。 I have no problem doing the relationships between Periods and Outcome (1: *) because in table outcome, PeriodName is not a calculated column.我在处理PeriodsOutcome (1: *) 之间的关系时没有问题,因为在表结果中,PeriodName 不是计算列。 But, when I try to add the relationship with the Income column (1: *) a circular dependency is detected.但是,当我尝试添加与收入列 (1: *) 的关系时,会检测到循环依赖。 I know is because the [CalculatedPeriod] is calculated using the Periods table but I don't know how to solve this我知道是因为[CalculatedPeriod]是使用Periods表计算的,但我不知道如何解决这个问题

Periods时期

PeriodName  StartDate    FinishDate
Period1     2020-Jan-01  2020-Jan-15
Period2     2020-Jan-15  2020-Jan-31
Period3     2020-Feb-01  2020-Feb-28
Period4     2020-Mar-01  2020-Mar-15
Period5     2020-Mar-15  2020-Abr-2

Income收入

Date        Value [CalculatedPeriod]
2020-Jan-09 25000 Period1
2020-Jan-11  5000 Period1
2020-Jan-28  3000 Period2
2020-Feb-14 18000 Period3
2020-Mar-14 12000 Period4

CalculatedPeriod= CALCULATE(VALUES(Periods[PeriodName]),FILTER(Periods,[Date]>=Periods[StartDate] && [Date] <= Periods[FinishDate]))

Outcome结果

Date        Value  Period
2020-Jan-07 16000  Period1
2020-Jan-17 11000  Period2
2020-Jan-31  5000  Period3
2020-Feb-14 13000  Period3
2020-Mar-14  9000  Period4

The data has a lot of other columns and the period column is supposed to work as a filter, but if I can't create the relationship with all the tables, the filter will only work with the tables related and will show always the total from the unrelated table.数据有很多其他列,并且周期列应该用作过滤器,但如果我无法与所有表创建关系,则过滤器将仅适用于相关表,并且始终显示来自的总数不相关的表。

(Filter: No filters)
Income     Outcome
63000      54000

(Filter: Periods[PeriodName:"Period3"])
Income     Outcome
63000      18000

(Filter: Periods[PeriodName:"Period5"])
Income     Outcome
63000      

How can I solve this problem?我怎么解决这个问题?

So far, the only workaround that I might think is to duplicate the periods table.到目前为止,我可能认为的唯一解决方法是复制周期表。

Thank you谢谢

You can use DISTINCT, taking an example for a very simple model if there are 2 tables A and B. And let's say B is created using VALUES ( A[Column] ) and then you build a relationship between A and B then that's not allowed because VALUES depends on the automatic blank row that is added to the one side of the relationship due to invalid realtionship ( caused because of missing values ) meaning there is a possibility that A might contain blank row added because of invalid relationship so if a relationship was possible using VALUES then B would have depended on A and A would depend on B.您可以使用 DISTINCT,举一个非常简单的 model 为例,如果有 2 个表 A 和 B。假设 B 是使用 VALUES ( A[Column] )创建的,然后您在 A 和 B 之间建立关系,那么这是不允许的因为 VALUES 取决于由于无效的关系(由于缺少值而导致)而添加到关系一侧的自动空白行,这意味着 A 可能包含由于无效关系而添加的空白行,所以如果关系是可能使用 VALUES,那么 B 将依赖于 A,而 A 将依赖于 B。

CalculatedPeriod =
CALCULATE (
    DISTINCT ( Periods[PeriodName] ),
    FILTER (
        Periods,
        [Date] >= Periods[StartDate]
            && [Date] <= Periods[FinishDate]
    )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM