title: DAX sum of multiplication (sumproduct) across multiple tables
body: <p>I am struggling with what I think is a really silly problem.</p>
  
  <p>I am trying to get "MTD target" values from an arbitrary date selection. I have a table 'out' which has many dimensions +date, as well as a target table which gives me a constant target value per day, for each month. My goal is to get the number of days selected per EACH month and multiply it by the corresponding daily target for the relevant month.</p>
  
  <p>For example, month 1 daily target is 2, month 2 daily target is 4. I have 4 days in month 1, and 3 days in month 2. My cumulative target should be 2*4+3*2 = 14.</p>
  
  <p>I can do this for a single month no problem. But as soon as I have a date range selected that crosses 2 or more months it all goes to hell.</p>
  
  <p>Table 'out' has date, country, and other dimensions.
  Table 'targets' has a year, month, and country dimensions.</p>
  
  <p>I am trying to get some join and multiplication that is something like SUM(month_country * selected_days)</p>
  
  <p>Here are the DAX measures I've tried:</p>
  
  <pre><code>mtd_inv_tgt :=
  CALCULATE (
      SUM ( targets[daily_spend] ),
      FILTER (
          targets,
          targets[market] = FIRSTNONBLANK ( out[co_market], "" )
              &amp;&amp; targets[yyyymm] &gt;= MIN ( out[yyyymm] )
              &amp;&amp; targets[yyyymm] &lt;= MAX ( out[yyyymm] )
      )
  )
      * DISTINCTCOUNT ( out[date] )
  </code></pre>
  
  <pre><code>mtd_inv_tgt :=
  SUMX (
      FILTER (
          targets,
          targets[market] = FIRSTNONBLANK ( out[co_market], "" )
              &amp;&amp; targets[yyyymm] &gt;= MIN ( out[yyyymm] )
              &amp;&amp; targets[yyyymm] &lt;= MAX ( out[yyyymm] )
      ),
      targets[daily_spend] * DISTINCTCOUNT ( out[date] )
  )
  </code></pre>
  
  <p>This works fine if the dates selected belong to one month. If I select 2 months it will add the daily spend across 2 months and then multiply it by the number of dates covering the 2 months. Like from the above example it would be (2+3)*(4+2) = 30, which is obviously wrong.</p>
  
  <p>The caveat is I can't use SUMX on the 'out' table because there are many records per date+country, whilst the targets are a single entry per month+country.</p>