Left Outer Join using DAX in PowerBI (many-to-many relationship)

How do I make a left join in DAX? When I try to add a relation or using the external DAX connection function, I get the following errors (see below). Any ideas would be greatly appreciated!

Error creating relationship: You cannot create a relationship between these two columns because one of the columns must have unique values.

Error trying NaturalLeftOuterJoin () No common join columns were found.  For join function

For reference, I am trying to create the profit and loss statement calculation lines.

Example:

  • Income: 100
  • Cost: 80
  • Profit: 20 (Income Value)

My tables are as follows:

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, Profit, :

 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. !

+4
1

- , Template, ? , (7 → 14 ) (, ).

, Measures DAX 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

P.S. , // , Column ( Measure). Power BI.

+1

Source: https://habr.com/ru/post/1681021/


All Articles