title: Finding difference between two Tables generated from one table using DAX
body: <p>I want to find the difference between the sum of all the common elements between 2 tables.</p>
  
  <p>We have one large table (Table A)</p>
  
  <p><a href="https://i.stack.imgur.com/6glQH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6glQH.png" alt="enter image description here"></a></p>
  
  <p>You can observe that neither the id, nor the date can be used individually as a unique identifier.</p>
  
  <p>Based on certain filters(one of it is date), we have to create 2 tables:
  <a href="https://i.stack.imgur.com/3wLVx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3wLVx.png" alt="enter image description here"></a></p>
  
  <p>We then have to find the difference between the sum of all the common elements.</p>
  
  <p>Table 1 and 2 may not be real tables and could be simply virtual tables (defined using VAR) declared in order to create the measure.</p>
  
  <p>I have tried the following code:</p>
  
  <p>Difference = 
  VAR 
  Table1= ADDCOLUMNS('TableA', "id", CALCULATE(VALUES('TableA'[Id]), ALL('Date'),ALL('TableA'), USERELATIONSHIP('Date'[As of Date], Previous_Date[Previous_Date]),USERELATIONSHIP('Date'[As of Date], 'TableA'[Date])), "Value", 'TableA'[Value] )</p>
  
  <p>VAR
  Table2= ADDCOLUMNS('TableA', "id", CALCULATE(VALUES('TableA'[Id]),ALL('TableA'), USERELATIONSHIP('Date'[As of Date], 'TableA'[Date])),"Value", 'TableA'[Value] )</p>
  
  <p>VAR
  abc = CALCULATE(SUMX(Table1,IF(VALUES('TableA'[Id]) IN Table1 &amp;&amp; VALUES('TableA'[Id]) in Table2,Table1[Value])) ) </p>
  
  <p>VAR
  pqr = CALCULATE(SUMX(Table2,IF(VALUES('TableA'[Id]) IN Table1 &amp;&amp; VALUES('TableA'[Id]) in Table2,Table2[Value])) )      </p>
  
  <p>RETURN
  abc-pqr</p>