powerbi

Creating Dax query for table/matrix visuals


I have the following tables:

Counterparty Table

Counterparty
foo 
boo
fizz
buzz
huzz

Table1 Data:

Counterparty Commodity DealType   StartDate  FloatPrice  MTMValue
      foo       bar      Buy  07/01/2024       18.73        10
      fizz      bar      Buy  09/01/2024       17.12        10
      fizz      bar      Buy  10/01/2024       17.76        10
      fizz      bar      Buy  11/01/2024       18.72        10
      fizz      bar      Buy  12/01/2024       19.47        10
      foo   ab cred      Buy  01/01/2025       20.26        10
      boo       baz     Sell  01/01/2025       21.04        10
      boo       baz     Sell  01/01/2025       22.23        10
      fizz      baz     Sell  01/01/2025       11.89        10
      fizz      baz     Sell  01/01/2025       12.33        10
      foo       baz     Sell  01/01/2025       14.78        10
      foo       baz     Sell  01/01/2025       18.23        10
      boo      rins     Sell  01/01/2025       16.43        10
      boo   ab cred     Sell  01/01/2025       12.21        10

Table 2 Data

Counterparty Commodity DealType   StartDate  FloatPrice  MTMValue
      foo       bar      Buy  07/01/2024       18.73        10
      fizz      bar      Buy  09/01/2024       17.12        10
      fizz      bar      Buy  10/01/2024       17.76        10
      fizz      bar      Buy  11/01/2024       18.72        10
      fizz      bar      Buy  12/01/2024       19.47        10
      foo   ab cred      Buy  01/01/2025       20.26        10
      boo       baz     Sell  01/01/2025       21.04        10
      boo       baz     Sell  01/01/2025       22.23        10
      fizz      baz     Sell  01/01/2025       11.89        10
      fizz      baz     Sell  01/01/2025       12.33        10
      foo       baz     Sell  01/01/2025       14.78        10
      foo       baz     Sell  01/01/2025       18.23        10
      boo      rins     Sell  01/01/2025       16.43        10
      boo   ab cred     Sell  01/01/2025       12.21        10

For the sake of example table1 and table2 are exactly indentical. In practice, they are identically structured but have different values in the last columns.

How can I write a query to create a table like the following based on these 3 tables:

FOR COMMODITY = BAR

Counterparty  Table1 Sum  Table2 Sum
   foo            10          10
   boo            0           0
   fizz           40          40
   buzz           0           0
   huzz           0           0       

I will have a slicer attached to the table to filter between commodities


Solution

  • you can create a dim table

    Table = DISTINCT(Table1[Commodity])
    

    create relationships among 4 tables. enter image description here

    then use the commodity column in the new dim table as a filter and create two measures

    table1 sum = sum(Table1[MTMValue])+0
    table2 sum = sum(Table2[MTMValue])+0
    

    enter image description here