---
schema_version: "1"
id: 7b228ecc876f57c2c2fc47a65437b8ee4b7b31b960ec1d2e3ce8439c95bda297
title: Window Functions for Formulas
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/data/264-0-0/c360_a_batch_transform_window.md
canonical_url: https://help.salesforce.com/s/articleView?id=data.c360_a_batch_transform_window.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 04c0b6e93ae532aaab7009d0dff231bdd8034e728dfd1ca3b64894dc5cba4708
description: Use window functions to perform calculations across rows. For each row of input data, perform a calculation on a selection—or window—of related rows. Unlike aggregation functions that group rows, window functions return a result for each row. For example, you can calculate the changes to the opportunity amount throughout the stages of each opportunity to see if you can hit your sales target.
products:
  - Data 360
release: "264"
---
# Window Functions for Formulas

Use window functions to perform calculations across rows. For each row of input data, perform a calculation on a selection—or window—of related rows. Unlike aggregation functions that group rows, window functions return a result for each row. For example, you can calculate the changes to the opportunity amount throughout the stages of each opportunity to see if you can hit your sales target.

When entering a formula, use the API name of the field in the expression, not the label. Surround the parameters with parentheses.

You can use the following window functions in a formula expression.

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small ">
      <colgroup>
        <col style="width:18.02773497688752%">
        <col style="width:15.408320493066254%">
        <col style="width:66.56394453004623%">
      </colgroup>
      <thead class="slds-text-title_caps ">
        <tr class="">
          <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d103368e44">String Function</th>
          <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d103368e47">Description</th>
          <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d103368e50">Syntax</th>
        </tr>
      </thead>
      <tbody>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>collect_list</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns a array type containing duplicates.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p>
              <code>collect_list(<var>field</var>)</code>
            </p>
            <p><var>field</var> is the field for which you want all the values for.
            </p>
            <p>Example: Return all the values from the Owner ID column.</p>
            <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>Owner Id
--------
A
B
C

array_join(collect_list(OwnerId), ',') -&gt; A,B,C</code></pre>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>collect_set</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns an array type without duplicates.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p>
              <code>collect_set(<var>field</var>)</code>
            </p>
            <p><var>field</var> is the field for which you want remove duplicate values
              from. </p>
            <p>Example: Return only the unique values from the Owner ID
              column.</p>
            <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x" id="c360_a_batch_transform_window__codeblock_qk2_z5d_f2c"><a name="c360_a_batch_transform_window__codeblock_qk2_z5d_f2c"><!-- --></a><code>Owner Id
--------
A
B
C

array_join(collect_set(OwnerId), ',') -&gt; A,B,C</code></pre>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>current</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns the value from the current record in the partition.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p>
              <code>current(<var>field</var>)</code>
            </p>
            <p><var>field</var> is the field that you want to get the current value
              from.</p>
            <div>
              <p>Example: Get the value of the Amount field for the current
                row.</p>
              <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>current(Amount)</code></pre>
            </div>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>dense_rank</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns the rank of each record in the partition based on order. Ranks are
            consecutive—they don’t repeat when the values match. For example, if the first three rows
            have the same value, then the first four ranks are 1, 1, 1, and 2.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p>
              <code>dense_rank()</code>
            </p>
            <div>
              <p>Example: Calculate the dense rank
                for rows within each
                partition.</p>
              <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>dense_rank()</code></pre>
            </div>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>first_value</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns the value from the first record in the partition.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p>
              <code>first_value(<var>field</var>)</code>
            </p>
            <p><var>field</var> is the field
              that you want to get the first value from.</p>
            <div>
              <p>Example: Get the original quote value for
                the Quote field.</p>
              <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>first_value(Quote)</code></pre>
            </div>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>lag</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns the value from the previous record in the partition.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p><code>lag(<var>field</var></code>)</p>
            <p><var>field</var> is the field for which
              you want to obtain the previous value.</p>
            <div>
              <p>Example: Calculate the percentage change in
                profit between the current and previous
                records.</p>
              <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>(((current(Profit)-lag(Profit)))/current(Profit))*100</code></pre>
            </div>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>last_value</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns the value from the last record in the partition.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p>
              <code>last_value(<var>field</var>)</code>
            </p>
            <p><var>field</var> is the field that
              you want to get the last value from.</p>
            <div>
              <p>Example: Calculate the difference between the
                original quote and final
                quote.</p>
              <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>first_value(Quote)-last_value(Quote)</code></pre>
            </div>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>lead</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns the value from the next record in the partition.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p><code>lead(<var>field</var></code>)</p>
            <p><var>field</var> is the field for which
              you want to obtain the next value.</p>
            <div>
              <p>Example: Get the next value for the Stage
                field.</p>
              <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>lead(Stage)</code></pre>
            </div>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>rank</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns the rank of each record in the partition based on order. Repeats the rank when
            the values are the same, and skips as many on the next non-match. For example, if the first
            three rows have the same value, then the first four ranks are 1, 1, 1, and 4.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p>
              <code>rank()</code>
            </p>
            <div>
              <p>Example: Rank the records in each
                partition.</p>
              <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>rank()</code></pre>
            </div>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>row_number</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns the row number in the partition. Increments by 1 for every row in the
            partition. For example, if the first three rows have the same value, then the first four
            ranks are 1, 2, 3, and 4.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p>
              <code>row_number()</code>
            </p>
            <div>
              <p>Example: Assign a row number to
                each record in each
                partition.</p>
              <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>row_number()</code></pre>
            </div>
          </td>

        </tr>
        <tr class="">
          <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e44 ">
            <code>sum</code>
          </td>

          <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e47 ">Returns the cumulative sum from groups of values.</td>

          <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d103368e50 ">
            <p>
              <code>sum()</code>
            </p>
            <div>
              <p>Example: Track the cumulative number of
                monthly units shipped tied to a specific sales
                agreement.</p>
              <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>sum()</code></pre>
            </div>
          </td>

        </tr>
      </tbody>
    </table>