title: Calculate average within category
body: <p>How to calculate the average of a measure within a category?</p>
  
  <p>For example, in my data model, I have a table "Geo" that contains the field "País" (Country).</p>
  
  <p>In table "Clientes" I have the field "Cliente" (Customer).</p>
  
  <p>This is how the relationship looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/8SUIk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8SUIk.jpg" alt="enter image description here"></a> </p>
  
  <p>The facts table is "Vendas".</p>
  
  <p>I need the average of measure "Margem Líquida *" per country, so I'm using the following measure:</p>
  
  <pre><code>Margem Média País = AVERAGEX(Clientes;[Margem Líquida *])
  </code></pre>
  
  <p>This is ok on a Country level, but as you can see in the following table I can't compare the value of the measure with the average per country on a Customer ("Cliente") level. </p>
  
  <p><a href="https://i.stack.imgur.com/yUIQR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yUIQR.jpg" alt="enter image description here"></a></p>
  
  <p>I'd like to have the value of the country level repeated for each customer within the country.</p>
  
  <p>How can I do that? I've tried all sorts of <code>CALCULATE</code> with <code>EARLIER</code> but no luck...</p>
  
  <p>Thanks in advance!</p>