Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Subtract columns from 2 different tables

Hello, I am trying to subtract values from 2 columns from 2 different tables. When I use SUM(column1)- SUM(column2), it is subtracting the values but it ignores the date value in the table and ends up subtracting everything. I want to remove Qty from table 1 that are in table 2 with date still considered. Please attached the data sample below:

Table 1
ItemQtyDate
111102/25/2022
112212/25/2022
111152/26/2022
112232/26/2022
Table 2
ItemQtyDate
11152/25/2022
11272/25/2022
11132/26/2022
112102/26/2022
   
Required output
ItemQtyDate
11152/25/2022
112142/25/2022
111122/26/2022
112132/26/2022

 

I would really appreciate the help from the community!

 

Thank you in advance !

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Anonymous ,

     

    Using a date table and a table with an item column and creating a relationship with the original table is a workable solution.

    For example. First create two calculated tables.

     

    Date = CALENDAR(MIN('Table 1'[Date]),MAX('Table 2'[Date]))

     

     

    Table 3 = VALUES('Table 1'[Item])

     

    Then create the following relationship.

    If you want the calculated column.

     

    Column = 
    VAR _qty_1 = CALCULATE(SUM('Table 1'[Qty]))
    var _qty_2 = CALCULATE(SUM('Table 2'[Qty]))
    return
    _qty_1-_qty_2

     

    If what you want is a measure, you can use your original expression and drag it into the visual along with columns

    'Date'[Date] and 'Table 3'[Item].

    Attach the PBIX file for reference. Hope it helps.

     

    Best Regards,
    Community Support Team_Gao

     

    If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly -- How to provide sample data

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    Using a date table and a table with an item column and creating a relationship with the original table is a workable solution.

    For example. First create two calculated tables.

     

    Date = CALENDAR(MIN('Table 1'[Date]),MAX('Table 2'[Date]))

     

     

    Table 3 = VALUES('Table 1'[Item])

     

    Then create the following relationship.

    If you want the calculated column.

     

    Column = 
    VAR _qty_1 = CALCULATE(SUM('Table 1'[Qty]))
    var _qty_2 = CALCULATE(SUM('Table 2'[Qty]))
    return
    _qty_1-_qty_2

     

    If what you want is a measure, you can use your original expression and drag it into the visual along with columns

    'Date'[Date] and 'Table 3'[Item].

    Attach the PBIX file for reference. Hope it helps.

     

    Best Regards,
    Community Support Team_Gao

     

    If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly -- How to provide sample data

    • Anonymous's avatar
      Anonymous
      Not applicable

      That worked! Thank you so much. Anonymous