Can this formula be optimized any further?

Mr. Snrub

Board Regular
Joined
May 22, 2005
Messages
150
Office Version
  1. 365
Platform
  1. Windows
I have the following formula that calculates yearly return:

Excel Formula:
=IF([@Date]>=TODAY(),NA(),IF(ROW()-ROW([#Headers])=1,0,LET(current_rows_year,YEAR([@Date]),prev_rows_year,YEAR(OFFSET([@Date],-1,0)),            prev_ytd_return,OFFSET([@[YTD Return]],-1,0),IF(current_rows_year<>prev_rows_year,[@[Daily Return]],(1+prev_ytd_return)*([@Daily Return]+1)-1))))

Expanded for readability:

Excel Formula:
=IF(
    [@Date]>=TODAY(),NA(),
    IF(
        ROW()-ROW([#Headers])=1,0,
        LET(
            current_rows_year,YEAR([@Date]),
            prev_rows_year,YEAR(OFFSET([@Date],-1,0)),
            prev_ytd_return,OFFSET([@[YTD Return]],-1,0),
            IF(
                current_rows_year<>prev_rows_year,[@[Daily Return]],
                (1+prev_ytd_return)*([@Daily Return]+1)-1
            )
        )
    )
)

Is there a way to optimize it any further? I was hoping to find some way to prevent using OFFSET, for one.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I think it's as good as you're going to get. Clearly this formula is in a table. Tables have some issues, like not allowing Spill formulas, and not having a good way to reference previous or next row data. The OFFSET you use is the cleanest way to do it, volatility notwithstanding. I know some ways to use INDEX instead (using the same kind of logic as ROW()-ROW([#Headers]) ), but it would really complicate the formula, with probably little gain. OFFSET is fast, and unless you have some specific issues with your worksheet lagging, you'll probably never notice.
 
Upvote 0
One option for referencing the row above is to define a relative named range in Name Manager. Since no sample data was provided, consider the following example...

Assuming Table1 is located on Sheet1 with "Date" in column A, "Daily Return" in column B, and "YTD Return" in column C, start by selecting any cell in row 2 (e.g. E2). Then, open Name Manager (Ctrl+F3) and define the following two names using an absolute column/relative row reference to $A1 and $C1 respectively:

Excel Formula:
PrevRowYear:
=IFERROR(YEAR(Sheet1!$A1),0)

PrevRowYTD:
=Sheet1!$C1

named_range_row_above.png


The calculated column formula can then be simplified as follows:

Excel Formula:
=IF([@Date]>=TODAY(),NA(),IF(ISREF(PrevRowYTD Table1[#Headers]),0,IF(YEAR([@Date])=PrevRowYear,(1+PrevRowYTD)*([@[Daily Return]]+1)-1,[@[Daily Return]])))

Please adjust the sheet/table names and column references as needed to coincide with your actual workbook structure.
 
Upvote 0
Your formula uses TODAY so will always be volatile anyway.
 
Upvote 0
Your formula uses TODAY so will always be volatile anyway.

On my real workbook that's actually a Named Range that points to a cell on another worksheet whose value is
Excel Formula:
=TODAY()
. So I probably should've mentioned that.

Excel Formula:
=IF([@Date]>=TODAY(),NA(),IF(ISREF(PrevRowYTD Table1[#Headers]),0,IF(YEAR([@Date])=PrevRowYear,(1+PrevRowYTD)*([@[Daily Return]]+1)-1,[@[Daily Return]])))

What does
Excel Formula:
ISREF(PrevRowYTD Table1[#Headers])
do? Are you sure that's correct? With the space between PrevRowYTD and Table1[#Headers]?
 
Upvote 0
never mind, that formula is correct. My workbook wasn't giving the correct values because I had the wrong row reference in the PrevYear and PrevYTDReturn ranges. But they work now! Thanks! This is great!
 
Upvote 0
What does
Excel Formula:
ISREF(PrevRowYTD Table1[#Headers])
do? Are you sure that's correct? With the space between PrevRowYTD and Table1[#Headers]?

Space is the intersection operator, which returns the intersection point between two ranges. If the ranges don't intersect, it returns #NULL!. ISREF returns TRUE if they intersect, or FALSE if they do not. It was suggested as an alternative to the ROW()-ROW(Table1[#Headers])=1 method, but is not a game-changer by any means.

Glad you were able to figure it out and get it working in your actual workbook. Cheers!
 
Upvote 0

Forum statistics

Threads
1,223,108
Messages
6,170,142
Members
452,304
Latest member
Thelingly95

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top