2017-07-08 980 views
4

我会怎么做左中加入DAX?当我尝试添加关系或使用左外部联接DAX函数时,出现以下错误(请参见下文)。任何想法将不胜感激!试图NaturalLeftOuterJoin当 You can't create a relationship between these two columns becasue one of the columns must have unique values.左外连接在PowerBI使用DAX(多对一一对多的关系)

错误() No common join columns detected. The join function 'NATURALLEFTOUTERJOIN' requires at-least one common join column.

仅供参考,我试图创建一个损益表计算行:创建关系时

错误。

实施例:

  • 收入:100
  • 成本:80
  • 利润:20(收入-成本)

我的表是象下面这样:

 
Fact table: 
╔═══════════╦═════════╦═══════════╦════════╗ 
║ YearMonth ║ StoreID ║ AccountID ║ Amount ║ 
╠═══════════╬═════════╬═══════════╬════════╣ 
║ 2017-01 ║ A  ║   1 ║ 100 ║ 
║ 2017-01 ║ B  ║   1 ║ 200 ║ 
║ 2017-01 ║ A  ║   2 ║ -50 ║ 
║ 2017-01 ║ B  ║   2 ║ -50 ║ 
║ 2017-02 ║ A  ║   1 ║  20 ║ 
║ 2017-02 ║ B  ║   1 ║ 150 ║ 
║ 2017-02 ║ B  ║   2 ║ -20 ║ 
╚═══════════╩═════════╩═══════════╩════════╝ 

Template table: 
╔════════════╦═══════════╦═════════╗ 
║ TemplateID ║ AccountID ║ Line ║ 
╠════════════╬═══════════╬═════════╣ 
║  105 ║   1 ║ Revenue ║ 
║  105 ║   2 ║ Cost ║ 
║  105 ║   1 ║ Profit ║ 
║  105 ║   2 ║ Profit ║ 
╚════════════╩═══════════╩═════════╝ 

在SQL这是超级简单 - 我只是做了左外连接,在其上利润线创造的记录,像下面的AccountID领域:

SELECT 
     f.[YearMonth] 
     ,f.[StoreID] 
     ,f.[AccountID] 
     ,f.[Amount] 
     ,t.[TemplateID] 
     ,t.[AccountID] 
     ,t.[Line] 
    FROM [dbo].[Fact] f 
    left join [dbo].[Templates] t 
    on f.[AccountID] = t.[AccountID] 

结果:

╔═══════════╦═════════╦═══════════╦════════╦════════════╦═══════════╦═════════╗ 
║ YearMonth ║ StoreID ║ AccountID ║ Amount ║ TemplateID ║ AccountID ║ Line ║ 
╠═══════════╬═════════╬═══════════╬════════╬════════════╬═══════════╬═════════╣ 
║ 2017-01 ║ A  ║   1 ║ 100 ║  105 ║   1 ║ Revenue ║ 
║ 2017-01 ║ B  ║   1 ║ 200 ║  105 ║   1 ║ Revenue ║ 
║ 2017-02 ║ A  ║   1 ║  20 ║  105 ║   1 ║ Revenue ║ 
║ 2017-02 ║ B  ║   1 ║ 150 ║  105 ║   1 ║ Revenue ║ 
║ 2017-01 ║ A  ║   2 ║ -50 ║  105 ║   2 ║ Cost ║ 
║ 2017-01 ║ B  ║   2 ║ -50 ║  105 ║   2 ║ Cost ║ 
║ 2017-02 ║ B  ║   2 ║ -20 ║  105 ║   2 ║ Cost ║ 
║ 2017-01 ║ A  ║   1 ║ 100 ║  105 ║   1 ║ Profit ║ 
║ 2017-01 ║ B  ║   1 ║ 200 ║  105 ║   1 ║ Profit ║ 
║ 2017-02 ║ A  ║   1 ║  20 ║  105 ║   1 ║ Profit ║ 
║ 2017-02 ║ B  ║   1 ║ 150 ║  105 ║   1 ║ Profit ║ 
║ 2017-01 ║ A  ║   2 ║ -50 ║  105 ║   2 ║ Profit ║ 
║ 2017-01 ║ B  ║   2 ║ -50 ║  105 ║   2 ║ Profit ║ 
║ 2017-02 ║ B  ║   2 ║ -20 ║  105 ║   2 ║ Profit ║ 
╚═══════════╩═════════╩═══════════╩════════╩════════════╩═══════════╩═════════╝ 

那么我就可以像这样转动它:

╔═════════╦═════════╦═════════╗ 
║ Line ║ Store A ║ Store B ║ 
╠═════════╬═════════╬═════════╣ 
║ Revenue ║  120 ║  350 ║ 
║ Cost ║  -50 ║  -70 ║ 
║ Profit ║  70 ║  280 ║ 
╚═════════╩═════════╩═════════╝ 

在DAX中,它看起来要复杂得多 - 希望有人能证明我的错误!我已阅读双向过滤可能允许多对多的关系,但我无法在这里工作。我尝试这样做的原因是在DAX中加入而不是在SQL中,因为我有几个语句模板,如果可以通过DAX动态完成,则不希望有几个加载的数据非常相似。谢谢!

+0

您试过了'NATURALINNERJOIN()'https://msdn.microsoft.com/en-us/library/dn802543.aspx?f=255&MSPPError=-2147217396 – Horaciux

+0

另外,看一看在这个职位https://www.sqlbi.com/articles/from-sql-to-dax-joining-tables/ – Horaciux

+0

谢谢Horaciux,NaturalInnerJoin不会给出想要的结果(真的想要一个左连接),说,它仍然产生相同的错误。我查看了链接,但没有太多意义 - 希望有人可以为这种情况提供解决方案。还是)感谢你的建议!! :) – FirstRedPepper

回答

1

有为什么需要Template表中的任何原因,不是虚拟的计算等?因为只是从样本数据中我看到事实表被重复(7 - > 14行)不必要的(也许我错过了一些关键点)。

如果不是,您可以简单地在DAX中写几个Measures来完成Power BI中的计算(这正是Power BI的功能),并且只需要Fact表。

DAX:

收入:

Revenue = 
CALCULATE(
    SUM('Fact'[Amount]), 
    FILTER(
     'Fact', 
     'Fact'[Amount] > 0 
    ) 
) 

费用:

Cost = 
CALCULATE(
    SUM('Fact'[Amount]), 
    FILTER(
     'Fact', 
     'Fact'[Amount] < 0 
    ) 
) 

利润:

Profit = [Revenue] + [Cost] 

然后你可以使用一个Matrix可视化得到想要的结果:

result

附:如果您确实需要将收入/成本/利润置于行而不是列中,则可能需要将数据转储或将计算结果写为新的Column(而不是Measure)。这是由于Power BI中的product limitation

+0

感谢Foxan。是的,需要连续计算。它使用模板表完成,因为已经创建了数百个报告模板。 – FirstRedPepper