title: Rankx function for every month DAX Power BI
body: <p>So I have a table in Power BI with Snapshot dates, Company Name and a Rating. I want to Rank the Ratings for each particular month (no month has repeated companies).</p>
  <p>So as a simple example, for the table below:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Snapshot_date</th>
  <th>Company Name</th>
  <th>Rating</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>31/03/2021</td>
  <td>A</td>
  <td>1</td>
  </tr>
  <tr>
  <td>31/03/2021</td>
  <td>B</td>
  <td>8</td>
  </tr>
  <tr>
  <td>31/03/2021</td>
  <td>C</td>
  <td>6</td>
  </tr>
  <tr>
  <td>31/03/2021</td>
  <td>D</td>
  <td>5</td>
  </tr>
  <tr>
  <td>30/04/2021</td>
  <td>A</td>
  <td>4</td>
  </tr>
  <tr>
  <td>30/04/2021</td>
  <td>B</td>
  <td>7</td>
  </tr>
  <tr>
  <td>30/04/2021</td>
  <td>C</td>
  <td>9</td>
  </tr>
  <tr>
  <td>30/04/2021</td>
  <td>D</td>
  <td>3</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I want to end up with:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Snapshot_date</th>
  <th>Company Name</th>
  <th>Rating</th>
  <th>Rank</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>31/03/2021</td>
  <td>A</td>
  <td>1</td>
  <td>1</td>
  </tr>
  <tr>
  <td>31/03/2021</td>
  <td>B</td>
  <td>8</td>
  <td>4</td>
  </tr>
  <tr>
  <td>31/03/2021</td>
  <td>C</td>
  <td>6</td>
  <td>3</td>
  </tr>
  <tr>
  <td>31/03/2021</td>
  <td>D</td>
  <td>5</td>
  <td>2</td>
  </tr>
  <tr>
  <td>30/04/2021</td>
  <td>A</td>
  <td>4</td>
  <td>2</td>
  </tr>
  <tr>
  <td>30/04/2021</td>
  <td>B</td>
  <td>7</td>
  <td>3</td>
  </tr>
  <tr>
  <td>30/04/2021</td>
  <td>C</td>
  <td>9</td>
  <td>4</td>
  </tr>
  <tr>
  <td>30/04/2021</td>
  <td>D</td>
  <td>3</td>
  <td>1</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>It seems the measure should be something like:</p>
  <pre><code>Rank = Rankx(filter(
                      all(table),
                      table[Snapshot_date].[month] = Earlier(table[Snapshot_date].month)
                      &amp;&amp; table[Snapshot_date].[year] = Earlier(table[Snapshot_date].year)
                       ),
                       Calculate(Sum(table[Rating])),,1)
  </code></pre>
  <p>But here I get an error where it states that parameter in the Earlier function is not the correct type, and I am referring to an earlier row context which doesn't exist.</p>
  <p>I have also tried:</p>
  <pre><code>Rank = Rankx(allexcept(table,
                      table[Snapshot_date].[month], table[Snapshot_date].[year]
                       ),
                       Calculate(Sum(table[Rating])),,1)
  </code></pre>
  <p>However this simply does the rank for the whole table rather than for each month.</p>
  <p>In the simple example with the table above with only two months, what has worked is:</p>
  <pre><code>Rank = Rankx(filter(
                      all(table),
                      table[Snapshot_date].[month] = 1
                      &amp;&amp; table[Snapshot_date].[year] = 2021)
                       ),
                       Calculate(Sum(table[Rating])),,1)
  
  </code></pre>
  <p>This weirdly works for both months if the month and year is one of the two dates. However if the month and year I use is not one of the two dates the ranks all become 1.</p>
  <p>Also I want this to work on a large table with many dates and want it to be dynamic. Any help would be greatly appreciated!</p>