---
schema_version: "1"
id: 088eb74ff164c6ff33720a4b78b82038d3b4ff731c95419a27ae74acd850a754
title: Window Functions for Formulas
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/analytics/264-0-0/bi_integrate_recipe_windowfunctions.md
canonical_url: https://help.salesforce.com/s/articleView?id=analytics.bi_integrate_recipe_windowfunctions.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 98971e2c21c684eb1c0062ca81de9455e67ee900db541cc46d6633d22919a634
description: In CRM Analytics, use window functions in a Data Prep recipe to perform calculations across rows. For each row of input data, perform a calculation on a selection—or window—of related rows. Unlike aggregation that groups 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:
  - CRM Analytics
  - Cross Cloud
release: "264"
---
# Window Functions for Formulas

In CRM Analytics, use window functions in a Data Prep recipe to perform calculations across rows. For each row of input data, perform a calculation on a selection—or window—of related rows. Unlike aggregation that groups 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.

## Window Functions for Formulas

When entering a formula, use the API name of the column 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="d121030e47">String Function</th>
            <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d121030e50">Description</th>
            <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d121030e53">Syntax</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="String Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e47 ">
              <code>current</code>
            </td>

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

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e53 ">
              <p>
                <code>current(<samp class="parmname">field</samp>)</code>
              </p>
              <p><samp class="parmname">field</samp> is
                the field you want to get the current value from.</p>
              <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>
            </td>

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

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e50 ">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="d121030e53 ">
              <p>
                <code>dense_rank()</code>
              </p>
              <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>
            </td>

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

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

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e53 ">
              <p>
                <code>first_value(<samp class="parmname">field</samp>)</code>
              </p>
              <p><samp class="parmname">field</samp>
                is the field you want to get the first value from.</p>
              <p>Example: Get the original
                quote value for the Quote
                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>first_value(Quote)</code></pre>
            </td>

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

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

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e53 ">
              <p><code>lag(<samp class="parmname">field</samp></code>)</p>
              <p><samp class="parmname">field</samp> is the
                field for which you want to obtain the previous value.</p>
              <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>
            </td>

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

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

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e53 ">
              <p>
                <code>last_value(<samp class="parmname">field</samp>)</code>
              </p>
              <p><samp class="parmname">field</samp> is
                the field you want to get the last value from.</p>
              <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>
            </td>

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

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

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e53 ">
              <p><code>lead(<samp class="parmname">field</samp></code>)</p>
              <p><samp class="parmname">field</samp> is the
                field for which you want to obtain the next value.</p>
              <p>Example: Get the next
                value for the Stage
                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>lead(Stage)</code></pre>
            </td>

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

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e50 ">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="d121030e53 ">
              <p>
                <code>rank()</code>
              </p>
              <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>
            </td>

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

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e50 ">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="d121030e53 ">
              <p>
                <code>row_number()</code>
              </p>
              <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>
            </td>

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

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e50 ">Returns the cumulative sum of each record in a partition based on order. When
              rows have the same values for the order-by column, the cumulative sum is calculated
              for all the rows with the same column value.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e53 ">
              <p>
                <code>sum()</code>
              </p>
              <p>Example: Track the cumulative number of monthly units shipped tied to a specific
                sales agreement.</p>
              <p>Example: Add OrderBy grouping to Activity Date. Then, 2
                rows with the same Activity Date and values of 2000 and 11000 both show the
                cumulative sum of 13000.</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>
            </td>

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

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

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e53 ">
              <p>
                <code>collect_list(<samp class="parmname">field</samp>)</code>
              </p>
              <p><samp class="parmname">field</samp>
                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>collect_list(OwnerId)</code></pre>
            </td>

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

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e50 ">Returns a multivalue without duplicates.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d121030e53 ">
              <p>
                <code>collect_set(<samp class="parmname">field</samp>)</code>
              </p>
              <p><samp class="parmname">field</samp>
                is the field for which you want to 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"><code>collect_set(OwnerId)</code></pre>
            </td>

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