---
schema_version: "1"
id: e4f830c1cb60a2290ecadcbda93c94f87b2d203b9e5eb1edfbcd329d06ccdd2a
title: Examples of Manual Accumulated Variables with the Let Function
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/sales/264-0-0/spiff_accumulated_variable_let.md
canonical_url: https://help.salesforce.com/s/articleView?id=sales.spiff_accumulated_variable_let.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 6939665e164e0b784f4cdef4e7542f379c18146a4ac19226631f10402e4d3814
description: Sometimes, an accumulated variable in Salesforce Spiff can't calculate an amount, so you must manually create accumulated variables by using the let() function.
products:
  - Sales Cloud
  - Sales Enablement
release: "264"
---
# Examples of Manual Accumulated Variables with the Let Function

Sometimes, an accumulated variable in Salesforce Spiff can't calculate an amount, so you must manually create accumulated variables by using the `let()` function.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: both Salesforce Classic (not available in all orgs) and Lightning Experience</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Enterprise</strong>, <strong>Unlimited</strong>, and <strong>Developer</strong> Editions</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available for an additional cost in: <strong>Professional</strong> Edition with Web Services API Enabled</td></tr></tbody></table>

A common reason why an accumulated variable doesn't calculate amount is when the period to sum varies for each record in the rule. For example, maybe a rule shows opportunities with payment dates within the period, and each opportunity has a close date in a different year, so calculating annual attainment with an accumulated variable isn't possible.

With the `let()` function, you can assign an alias to a field so that Spiff can compare dynamic values, such as each record’s close date, to a static value, such as a specific deal’s close date. For example, you can assign the alias ThisDealsCloseDate to a field that helps identify the field when you’re reading the logic.

## Single Sort

In this example, the calculation sorts by CloseDate, summing deals that precede the current deal. The first deal, worth $4.357 million, is the total for deals closed from January through April, but doesn’t include May 1.

```
let(ThisDealsCloseDate, CloseDate,
sumif(ClosedInYearDataset, CloseDate < ThisDealsCloseDate, ARR))
```

![The let function creating a single-sorted manual accumulated variable](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-sales-sales_performance_management-264-0-0-production-enus/b1f2feb4-26b8-444f-97f7-be2349869d93/sales_performance_management/images/spiff/spiff_accumulated_variable_let_1.png)

A secondary sort is almost always required. If multiple deals close on the same day, a manual accumulated variable returns the same value for each deal because they aren’t further sorted. You can introduce additional `let()` functions to sort by other criteria, such as DealID or DealValue, and ensure each deal is uniquely evaluated.

## Secondary Sort

In the previous example, the primary sort is CloseDate. To expand this example and further differentiate deals closed on the same day, add a secondary sort by ID. The logic includes extra line breaks and indentations to identify each sort.

```
let(ThisDealsCloseDate, CloseDate, 
let(ThisDealsId, Id,
sumif(ClosedInYearDataset,
    CloseDate < ThisDealsCloseDate OR
    (CloseDate = ThisDealsCloseDate AND Id > ThisDealsId)
, ARR)))
```

![The let function creating a secondary sort](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-sales-sales_performance_management-264-0-0-production-enus/b1f2feb4-26b8-444f-97f7-be2349869d93/sales_performance_management/images/spiff/spiff_accumulated_variable_let_2.png)

The accumulated values for rows 2 through 4 are now different. The increasing values for the day demonstrate the effectiveness of the secondary sort.

## Lookback

Sorting criteria that covers multiple attainment periods is the most common use case for a manual accumulated variable. In this example, consider a rule that shows deals based on the payment date but the commission calculation is based on the closed date. The commission is based on annual attainment, so a manual accumulated variable is required if the opportunities calculated in the rule have close dates that span multiple years. If the opportunities all have the same close year, the accumulated variable is available because the opportunities all use the same data filter.

If not all deals can use the same data filter, you can use multiple accumulated variables for each data filter or a dynamic manual accumulation. Typically, a manual accumulated variable is recommended because it requires less maintenance. However, multiple accumulated variables are necessary if the `let()` function results in timeouts or memory issues.

Determine how far back you want to calculate. You can use a data filter with every deal closed, but if a shorter period is sufficient, use a smaller data filter. In this example, replace AllClosedOpportunitiesDataFilter with something like ClosedInPast24Months.

Adjust the sorts. This example calculates annual attainment, and the opportunities in the rule have close dates in 2022 and 2023. For deals closed in 2022, sum the 2022 opportunities up to the current deal, and for deals closed in 2023, sum the 2023 opportunities. The data filter covers a longer period, and the primary sort includes a start date.

```
let(ThisDealsCloseDate, CloseDate,
let(ThisDealsId, Id, 
sumif(AllClosedOpportunitiesDataFilter, 
    (CloseDate >= beginning_of_year(ThisDealsCloseDate) AND CloseDate < ThisDealsCloseDate) OR
    (CloseDate = ThisDealsCloseDate AND id < ThisDealsId)
, ARR__c)))
```

The single-sorted example covered the data filter only from the beginning of the year, so it didn't require a start date filter.

If an opportunity closed on August 15, 2022 with an Id of 123456abcd, the logic sums opportunities with these criteria.

- CloseDate >= 2022-01-01 (beginning\_of\_year(2022-08-15))
- CloseDate < 2022-08-15
- CloseDate = 2022-08-15 AND Id < 123456abcd