Skip to main content
Question

How can you dynamically display the last 5 years in Appian including current year?

Example : If current year is 2025 , need generate list starting from the current year

Output : {2025, 2024, 2023, 2022, 2021}

A
Anonymous
December 10, 2025

Answer

In Appian, we can achieve this is multiple ways. I am showing two approaches , first approach is the most commonly used by the appian developer. Second approach is more efficient than the firs approach


Approach 1: Using a!forEach()


Appian Code

a!localVariables(
local!currentYear: year(now()),
local!lastFiveYears: a!forEach(
items: enumerate(5),
expression: local!currentYear - fv!item
),
local!lastFiveYears
)

Explanation :

  1. now() returns the current date and time.
  2. year(now()) extracts the current year, for example 2026.
  3. enumerate(5) generates the list {0, 1, 2, 3, 4}.
  4. a!forEach() loops through those numbers, with fv!item holding the current one. Each pass subtracts it from the current year. For iteration it will run local!currentYear - fv!item => {( 2026 - 0), ( 2026 - 1), ( 2026 - 2), ( 2026 - 3), ( 2026 - 4)}

Output:

{2026, 2025, 2024, 2023, 2022}


Approach 2: Using enumerate() on its own

You can get the same list without a!forEach():

Appian Code

year(now()) - enumerate(5)


Why it works?

enumerate() already returns a list starting at 0, and Appian lets you do arithmetic on a whole list at once. The expression above works out to:

2026 - {0, 1, 2, 3, 4}

which gives:

{2026, 2025, 2024, 2023, 2022}

The second approach is shorter and needs neither a!forEach() nor a!localVariables().


Making it dynamic

Replace the hardcoded 5 with a rule input, ri!numberOfYears, to control how many years you get:

year(now()) - enumerate(ri!numberOfYears)
  1. If ri!numberOfYears = 3, the result is {2026, 2025, 2024}.
  2. If ri!numberOfYears = 5, the result is {2026, 2025, 2024, 2023, 2022}.
  3. If ri!numberOfYears = 10, the result is the current year and the previous nine.
  4. Before you reach for a!forEach() to loop over a list, check whether a built-in function or a list operation already does the job in one expression.


Output:

Image 38

Try it yourself

Change the inputs or the expression and run it to see what it returns.

ri!year=
ri!numberOfYears=

Independent evaluator: it approximates the Appian expression language and may differ from a real Appian environment. Verify anything that matters. Terms

JuniorExpressions#sailcoding