# excel-lambda-depn.schedule – Create a depreciation schedule in Excel with one function

The gist for the lambdas shown in this post can be found here.

When importing this gist, be sure to select “Add formulas to new namespace” and use the name “depn”. ## The goal

There are several methods of calculating depreciation in Excel.

The functions SLN (straight line) , DB (declining balance) , DDB (double declining balance) and SYD (sum-of-years’ digits) are commonly used.

In addition, it’s useful to calculate a table showing the depreciation in each period over the life of the asset. As an example, this table shows the depreciation of an asset with a life of 9 years using the SLN function: We can easily transpose this table to have the time periods on the column axis.

The SLN function is only used in the “Depreciation” column. Everything else is independent of the function used to calculate that column.

Further to this, the functions that can be used to calculate depreciation generally share the same parameters: So, if we ignore the [factor] parameter only used by DDB, we can consider a generic function fn(cost, salvage, life, period) to calculate depreciation where fn is one of {SLN,DB,DDB,SYD}.

With all of that in mind, the goal of this post will be to:

Create a lambda to produce an asset depreciation schedule with a parameterized depreciation function

## A solution

Here’s a lambda called depn.schedule:

```schedule = LAMBDA(cost,salvage,life,purchase_year,function,[return_header],[vertical],
LET(

/*handle missing vertical argument*/
_v,IF(ISOMITTED(vertical),FALSE,vertical),

/*create an array that is life+1 rows, starting at 0*/
_periods,SEQUENCE(life+1,,0),
_years,purchase_year + _periods,

/*apply the depreciation function to the inputs*/
_depr,IFERROR(function(cost,salvage,life,_periods),0),

/*calculate the accumulated depreciation over the life of the asset*/
_acc,SCAN(0,_depr,LAMBDA(a,b,a+b)),
_depr_val,cost-_acc,

/*place the various vectors in an array - one row per year, one column per vector
(simpler with HSTACK)*/
_arr,CHOOSE({1,2,3,4,5},_years,_periods,_depr,_acc,_depr_val),

/*append the header to the array
(simpler with VSTACK)*/

/*if the calling function has passed [return_header]=FALSE, then return _arr,

/*if the calling function has passed [vertical]=TRUE, then
return with years on rows, otherwise return with years on columns*/
IF(_v,_output,TRANSPOSE(_output))
)
);
```

depn.schedule takes five required parameters:

1. cost – the cost of the asset.
2. salvage – the salvage value of the asset at the end of its life.
3. life – the life (in years) of the asset. This should be an integer.
4. purchase_year – the year the asset was purchased, which should be a four-digit integer.
5. function – the function to use to calculate the depreciation. This must be one of:
1. depn.sln (for straight-line)
2. depn.db (for declining balance)
3. depn.ddb (for double-declining balance)
4. depn.syd (for sum-of-years’ digits)
And two optional parameters:
1. [return_header] – OPTIONAL – indicates whether to return the header. Default is TRUE.
2. [vertical] – OPTIONAL – indicates whether to return the years on rows (TRUE) or columns (FALSE). Default is FALSE.

The fifth parameter to the lambda above is a function that calculates depreciation.

While we can certainly add more, and really it need only be any function that takes 4 parameters, the intent is to use one of the following four names.

Each of these is in the same namespace as the schedule function above, and as such are referred to by depn.sln, depn.db, depn.ddb and depn.syd:

```sln = LAMBDA(cost,salvage,life,periods,
LET(
v,SLN(cost,salvage,life),
MAKEARRAY(ROWS(periods),1,LAMBDA(r,c,IF(r=1,0,v)))
)
);

db = LAMBDA(cost,salvage,life,periods,
DB(cost,salvage,life,periods)
);

ddb = LAMBDA(cost,salvage,life,periods,
DDB(cost,salvage,life,periods)
);

syd = LAMBDA(cost,salvage,life,periods,
SYD(cost,salvage,life,periods)
);
```

There’s nothing special about these functions – in each case they are simply creating a vector of depreciation values for the periods passed into the fourth parameter.

The only one that’s slightly different is depn.sln. It calls Excel’s native SLN function, which doesn’t take a period parameter (since all periods have the same depreciation – it’s a straight line). As such, we build the vector manually to ensure a zero in the first row and a fixed depreciation amount in every other row.

By defining these as lambda functions, we can now pass them as a parameter to the depn.schedule function.

This is how it works: As mentioned above, we can easily pivot this output such that the years are on the column axis by either omitting the vertical parameter or setting it to FALSE.

As you can see, using this function makes it trivially simple to create a table illustrating the depreciation of a fixed asset.  You can grab the code from the gist linked at the top of this post if you want to use it.  If you’d like to understand how it works, please read on.

## How it works

As a reminder, the steps of the depn.schedule function are:

```schedule = LAMBDA(cost,salvage,life,purchase_year,function,[return_header],[vertical],
LET(

/*handle missing vertical argument*/
_v,IF(ISOMITTED(vertical),FALSE,vertical),

/*create an array that is life+1 rows, starting at 0*/
_periods,SEQUENCE(life+1,,0),
_years,purchase_year + _periods,

/*apply the depreciation function to the inputs*/
_depr,IFERROR(function(cost,salvage,life,_periods),0),

/*calculate the accumulated depreciation over the life of the asset*/
_acc,SCAN(0,_depr,LAMBDA(a,b,a+b)),
_depr_val,cost-_acc,

/*place the various vectors in an array - one row per year, one column per vector
(simpler with HSTACK)*/
_arr,CHOOSE({1,2,3,4,5},_years,_periods,_depr,_acc,_depr_val),

/*append the header to the array
(simpler with VSTACK)*/

/*if the calling function has passed [return_header]=FALSE, then return _arr,

/*if the calling function has passed [vertical]=TRUE, then
return with years on rows, otherwise return with years on columns*/
IF(_v,_output,TRANSPOSE(_output))
)
);
```

As usual, we use LET to define variables:

• _rh – here we handle the optional [return_header] parameter. If it is not provided, we set a default of TRUE, otherwise we use the value provided. If the argument passed is text, the function will error. Otherwise a zero will equate to FALSE and any other non-zero number will equate to TRUE.
• _v – similarly, we handle the optional [vertical] parameter. If the parameter is omitted, the default is FALSE (horizontal layout), otherwise use the argument passed.
• _periods – we create a sequence of integers that’s life+1 rows long, starting at 0 (the purchase year). For example, for life=5, _periods = {0,1,2,3,4,5}
• _years – we simply add the purchase year to the _periods array, which gives us a list of years. For example, for purchase_year = 2022 and life = 5, _years = {2022,2023,2024,2025,2026,2027}
• _depr – here we use the function passed into the function parameter to calculate the depreciation in each period. As mentioned before, exactly what this function does is dependent on the method used (depn.sln, depn.db, depn.ddb or depn.syd). All that is required here is a function that will accept the arguments being passed in this definition. So, if you wanted to add another method, you would only need to define a new lambda for that method, then pass the name of that lambda as the fourth argument to depn.schedule.
• _acc – here we SCAN through the _depr vector and calculate a running sum by adding each row to the result of the scan on the previous row (a+b).
• _depr_val – is just the cost minus the accumulated depreciation.
• _header – is a one-row array of headers. Edit as you prefer.
• _arr – here we put each of the five columns next to each other using CHOOSE. This is also easily possible with HSTACK if you are an Office Insider.
• _arr_with_header – we use MAKEARRAY to stack the _header variable on top of the _arr variable. Again, this is possible and easier with VSTACK. I have not used VSTACK here because it is not currently widely available.
• _output – here we are checking the _rh variable (return header) to determine whether to return either _arr or _arr_with_header.
• And finally, we check the _v variable to decide whether to return the table as a horizontal schedule or a vertical schedule.

## In summary

That’s how to create a depreciation schedule in Excel with one function.

Excel provides several native functions for different methods of calculating depreciation of a fixed asset.

By first comparing the parameters between the different methods and standardizing their inputs by wrapping them in the LAMBDA function, we can pass them as a parameter to a function that produces a depreciation schedule.

I hope this is useful and sparks some ideas for using lambda to simplify your work.