Forum Discussion

Robin96's avatar
Robin96
Helper II
2 years ago

Calculation if ID match

Hey all,

i need to create a calculation that substract an amount from table1 from table2. (Amount from table1 - amount from table2). This should be based on if the ID's match. So if Reference ID from table1 match the ID from table2, then do the substraction, else nothing.

 

i have posted a screenshot of an example.

 

 

8 Replies

  • Robin96 

    create relationship between two tables and create two columns

    Column = RELATED('Table 1'[reference id])+0
    
    Column 2 = if(ISBLANK(sumx(FILTER('Table 1','Table 2'[id]='Table 1'[reference id]),'Table 1'[amount])),0,'Table 2'[amount]-sumx(FILTER('Table 1','Table 2'[id]='Table 1'[reference id]),'Table 1'[amount]))

    pls see the attachment below

    • Robin96's avatar
      Robin96
      Helper II

      Hey, Thanks for the response! There is no relation between these two ID's in the original semantic model. They both relate to the date table i have. Is there a way to add this link with for example TREATAS in the column syntax?

      • ryan_mayu's avatar
        ryan_mayu
        Super User

        Robin96 

        then you change the column  to 

        Column = maxx(FILTER('Table 1','Table 1'[id]='Table 2'[id]),'Table 1'[reference id])+0
  • Hi,

    In the last row of the desired output table, shouldn't the answer be 300?

    • Robin96's avatar
      Robin96
      Helper II

      Hey, yes. The last row of Desired output should be 300. thanks for highlighting this.

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Super User

        Hi,

        In table2, write these calculated column formulas

        Table1 reference ID = calculate(max(Table1[reference ID]),filter(Table1,Table1[reference ID]=earlier(Table2[id])))

        Amount = calculate(sum(Table1[amount]),filter(Table1,Table1[reference id]=earlier(Table1 reference ID)))

        Hope this helps.