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}
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}
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
Explanation :
now()returns the current date and time.year(now())extracts the current year, for example2026.enumerate(5)generates the list{0, 1, 2, 3, 4}.a!forEach()loops through those numbers, withfv!itemholding the current one. Each pass subtracts it from the current year. For iteration it will runlocal!currentYear - fv!item=> {( 2026 - 0), ( 2026 - 1), ( 2026 - 2), ( 2026 - 3), ( 2026 - 4)}
Output:
Approach 2: Using enumerate() on its own
You can get the same list without a!forEach():
Appian Code
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:
which gives:
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:
- If
ri!numberOfYears = 3, the result is{2026, 2025, 2024}. - If
ri!numberOfYears = 5, the result is{2026, 2025, 2024, 2023, 2022}. - If
ri!numberOfYears = 10, the result is the current year and the previous nine. - 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:

Try it yourself
Change the inputs or the expression and run it to see what it returns.
Independent evaluator: it approximates the Appian expression language and may differ from a real Appian environment. Verify anything that matters. Terms