Return a Prior Month's Value from within the Same Table?

chasfh

Board Regular
Joined
Dec 10, 2014
Messages
64
Office Version
  1. 365
Platform
  1. Windows
What I am actually trying to do is create a formula that will capture a stock's gain or loss by referring to the number of shares in a prior month within the same table, and then multiplying that by the price difference between this month and the prior month. I have included what the answer should be in the sample XL2BB table below.

I need to do this on the prior month's shares total because when I receive a dividend, it gets reinvested in more shares, and I don't want to do so on the current month's higher share total because I would be double-counting against a separate income spreadsheet we are keeping. That's why the should-be answer for DUHP is not simply this month's total minus last month's total.

Since I am trying to return a column's value based on lookups of other columns, I tried doing this with XLOOKUP, but since the lookup values and the lookup array are all the exact same columns, I get the #SPILL error. So, if this can be done, I assume it will entail use of a different formula.

Seems self-explanatory, but let me know whether I need to clarify. Thanks.

Book1
ABCDEFGHIJ
1PeriodTickerSharesPriceTotalMonthYearM/M G/LAnswer should be
2Aug-24DRIV20022.71$ 4,542.0082024
3Sep-24DRIV20023.33$ 4,666.0092024$ 124.00
4Aug-24DUHP317.909533.56$ 10,669.0482024
5Sep-24DUHP318.849433.91$ 10,812.1892024$ 111.27
6Aug-24EMXC73.2155660.66$ 4,441.2682024
7Sep-24EMXC73.2155660.71$ 4,444.9292024$ 3.66
Sheet1
Cell Formulas
RangeFormula
F2:F7F2=MONTH(A2)
G2:G7G2=YEAR(A2)
J3,J7,J5J3=C2*(D3-D2)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You can do something simple like this:

VBA Code:
Function gainSTCK(a As Range) As Double
If a = a.Offset(-1, 0) Then
gainSTCK = a.Offset(0, 1) * (a.Offset(0, 2) - a.Offset(-1, 2))
Else
gainSTCK = 0
End If
End Function

So practically we are checking the column B to see if values are equal.

an example will be:

VBA Code:
 In any cell = gainSTCK(B3) ' one example

You can use this function in a loop to get all values from the table.


Regards,
GB
 
Last edited:
Upvote 0
I'm not sure if each stock will always appear each month or what should happen if it doesn't, but see if this helps.

chasfh.xlsm
ABCDEH
1PeriodTickerSharesPriceTotalM/M G/L
2Aug-24DRIV20022.71$4,542.00 
3Sep-24DRIV20023.33$4,666.00124.00
4Aug-24DUHP317.909533.56$10,669.04 
5Sep-24DUHP318.849433.91$10,812.18111.27
6Aug-24EMXC73.2155660.66$4,441.26 
7Sep-24EMXC73.2155660.71$4,444.923.66
Sheet1
Cell Formulas
RangeFormula
H2:H7H2=LET(f,TAKE(FILTER(C$2:D2,(A$2:A2<=A2)*(B$2:B2=B2)),-2),IFERROR(INDEX(f,1,1)*(INDEX(f,2,2)-INDEX(f,1,2)),""))
 
Upvote 0
This is a fair bit longer but uses table structured referencing.
I didn't have the table name so I have used Table1.

Excel Formula:
=LET(
arrLookup,Table1[[#Headers],[Ticker]]:[@Ticker],
arrReturn,Table1[[#Headers],[Shares]]:[@Price],
arrPrevSharesPrice, XLOOKUP([@Ticker],
                            DROP(arrLookup,-1),
                            DROP(arrReturn,-1),
                            "",0,-1),
PrevShares,CHOOSECOLS(arrPrevSharesPrice,1),
PrevPrice,CHOOSECOLS(arrPrevSharesPrice,2),
CurrPrice,[@Price],
IFERROR(PrevShares*(CurrPrice-PrevPrice),""))
 
Upvote 0
I gotta say, @Peter_SSs and @Alex Blakenburg, you guys are absolute wizards. There was no way I was going to arrive at these solutions on my own, and I really appreciate you guys taking a good hack at this.

I'm going to have to crown Alex the winner on this one. What I showed you was a simplified table; there are actually multiple accounts and the same stock shows up in more than one account, and Alex's formula was the one that was able to separate the accounts out and calculate correctly.

Of course I had to add another column, the Account column, to the #Headers and XLOOKUP portion of the formula, so that it looks more like this:

=LET(
arrLookup,Table1[[#Headers],[Account]]:[@Account]&Table1[[#Headers],[Ticker]]:[@Ticker],
arrReturn,Table1[[#Headers],[Shares]]:[@Price],
arrPrevSharesPrice, XLOOKUP([@Account]&[@Ticker],
DROP(arrLookup,-1),
DROP(arrReturn,-1),
"",0,-1),
PrevShares,CHOOSECOLS(arrPrevSharesPrice,1),
PrevPrice,CHOOSECOLS(arrPrevSharesPrice,2),
CurrPrice,[@Price],
IFERROR(PrevShares*(CurrPrice-PrevPrice),""))

And of course, remove the spaces for the formula to work in the worksheet in the first place, and change the table name, so this is the actual formula from the top of the table:

=LET(arrLookup,Table2[[#Headers],[Account]]:[@Account]&Table2[[#Headers],[Ticker]]:[@Ticker],arrReturn,Table2[[#Headers],[Shares]]:[@Price],arrPrevSharesPrice,XLOOKUP([@Account]&[@Ticker],DROP(arrLookup,-1),DROP(arrReturn,-1),"",0,-1),PrevShares,CHOOSECOLS(arrPrevSharesPrice,1),PrevPrice,CHOOSECOLS(arrPrevSharesPrice,2),CurrPrice,[@Price],IFERROR(PrevShares*(CurrPrice-PrevPrice),""))

Which looks like alphanumeric vomit but, as you guys with overseas accents like to say, it "works a treat!"

Thanks a bunch!
 
Upvote 0
Glad you have it working how you want.
Any chance you could post a slightly larger set of realistic sample data with the correct columns and your final formula in it so I can take a look at it in action?
Before creating an XL2BB sample, you can hide any of the columns that the formula does not use (to keep the XL2BB sample more manageable).
 
Upvote 0
Sure, @Peter_SSs, here you go. M/M G/L means month over month gain or loss, based on previous months' shares totals, since additional shares purchased or added by dividend reinvestment would constitute buys or income, and not gains or losses.

_Net Worth - Terrie and Chuck.xlsx
ABCDEFG
1Sort by
2Ascending
3Mo. EndAccountTickerSharesPriceTotalM/M G/L
4Jul-24MainACIC12912.21$ 1,575.09
5Aug-24MainACIC28111.23$ 3,155.63$ (126.42)
6Sep-24MainACIC28110.93$ 3,071.33$ (84.30)
7Sep-24MainACMR48419.11$ 9,249.24
8Sep-24MainALAB17850.54$ 8,996.12
9Jul-24MainAMZN83186.98$ 15,519.34
10Aug-24MainAMZN83178.50$ 14,815.50$ (703.84)
11Sep-24MainAMZN83184.76$ 15,335.08$ 519.58
12Aug-24MainBCAB4011.77$ 709.77
13Sep-24MainBCAB6072.03$ 1,232.21$ 104.26
14Jul-24MainC8264.88$ 5,320.16
15Aug-24MainFOUR11083.10$ 9,141.00
16Sep-24MainFOUR11088.90$ 9,779.00$ 638.00
17Jul-24MainFSLR17215.99$ 3,671.83
18Jul-24MainHCA17363.05$ 6,171.85
19Aug-24MainHPP9115.15$ 4,691.65
20Sep-24MainHPP12594.55$ 5,728.45$ (546.60)
21Aug-24MainMOH25349.79$ 8,744.75
22Sep-24MainMOH25331.80$ 8,295.00$ (449.75)
23Sep-24MainMU9499.85$ 9,385.90
24Aug-24MainOLPX9112.09$ 1,903.99
25Sep-24MainOLPX9112.34$ 2,131.74$ 227.75
26Sep-24MainOS26432.24$ 8,511.36
27Jul-24MainPHYS27118.98$ 5,143.58
28Jul-24MainREAL8413.74$ 3,145.34
29Aug-24MainREAL14202.64$ 3,748.80$ (925.10)
30Sep-24MainREAL14202.99$ 4,245.80$ 497.00
31Jul-24MainSB2335.06$ 1,178.98
32Jul-24MainSBLK15322.54$ 3,448.62
33Jul-24MainSCYX1802.13$ 383.40
34Aug-24MainSCYX1801.46$ 262.80$ (120.60)
35Sep-24MainSCYX1801.43$ 257.40$ (5.40)
36Aug-24MainSHOP20074.07$ 14,814.00
37Sep-24MainSHOP20079.14$ 15,828.00$ 1,014.00
38Jul-24MainSQ7061.88$ 4,331.60
39Aug-24MainSWVXX1065.371.00$ 1,065.37
40Sep-24MainSWVXX01.00$ -$ -
41Jul-24MainVTLE8943.61$ 3,881.29
42Jul-24MainVYM91.0073124.33$ 11,314.94
43Aug-24MainVYM91.0073127.36$ 11,590.69$ 275.75
44Sep-24MainVYM91.6129128.00$ 11,726.45$ 58.24
R-IY Worksheet
Cell Formulas
RangeFormula
G4:G44G4=LET(arrLookup,Table2[[#Headers],[Account]]:[@Account]&Table2[[#Headers],[Ticker]]:[@Ticker],arrReturn,Table2[[#Headers],[Shares]]:[@Price],arrPrevSharesPrice,XLOOKUP([@Account]&[@Ticker],DROP(arrLookup,-1),DROP(arrReturn,-1),"",0,-1),PrevShares,CHOOSECOLS(arrPrevSharesPrice,1),PrevPrice,CHOOSECOLS(arrPrevSharesPrice,2),CurrPrice,[@Price],IFERROR(PrevShares*(CurrPrice-PrevPrice),""))
 
Upvote 0
Thanks for the comprehensive feedback. Glad we could help.

Sure thing, and I do have a question: it seems the formula won't really work unless the table is actively sorted by month ascending, whether primarily or secondarily. If I sort it by any other column and there is no secondary sort by month ascending, the calculations from the formula breaks. Is that a requirement that's built into the formula? Is there any way to tweak the formula so that it will calculate properly no matter how I sort the table?
 
Upvote 0
So try this. I have the previous suggestion formula in col G and a new formula in col H.
This much shorter formula produces the same results for this layout ..

chasfh.xlsm
ABCDEFGH
3Mo. EndAccountTickerSharesPriceTotalM/M G/LTest
4Jul-24MainACIC12912.21$1,575.09  
5Aug-24MainACIC28111.23$3,155.63-126.42-126.42
6Sep-24MainACIC28110.93$3,071.33-84.3-84.3
7Sep-24MainACMR48419.11$9,249.24  
8Sep-24MainALAB17850.54$8,996.12  
9Jul-24MainAMZN83186.98$15,519.34  
10Aug-24MainAMZN83178.5$14,815.50-703.84-703.84
11Sep-24MainAMZN83184.76$15,335.08519.58519.58
12Aug-24MainBCAB4011.77$709.77  
13Sep-24MainBCAB6072.03$1,232.21104.26104.26
14Jul-24MainC8264.88$5,320.16  
15Aug-24MainFOUR11083.1$9,141.00  
16Sep-24MainFOUR11088.9$9,779.00638638
17Jul-24MainFSLR17215.99$3,671.83  
18Jul-24MainHCA17363.05$6,171.85  
19Aug-24MainHPP9115.15$4,691.65  
20Sep-24MainHPP12594.55$5,728.45-546.6-546.6
21Aug-24MainMOH25349.79$8,744.75  
22Sep-24MainMOH25331.8$8,295.00-449.75-449.75
23Sep-24MainMU9499.85$9,385.90  
24Aug-24MainOLPX9112.09$1,903.99  
25Sep-24MainOLPX9112.34$2,131.74227.75227.75
26Sep-24MainOS26432.24$8,511.36  
27Jul-24MainPHYS27118.98$5,143.58  
28Jul-24MainREAL8413.74$3,145.34  
29Aug-24MainREAL14202.64$3,748.80-925.1-925.1
30Sep-24MainREAL14202.99$4,245.80497497
31Jul-24MainSB2335.06$1,178.98  
32Jul-24MainSBLK15322.54$3,448.62  
33Jul-24MainSCYX1802.13$383.40  
34Aug-24MainSCYX1801.46$262.80-120.6-120.6
35Sep-24MainSCYX1801.43$257.40-5.4-5.4
36Aug-24MainSHOP20074.07$14,814.00  
37Sep-24MainSHOP20079.14$15,828.0010141014
38Jul-24MainSQ7061.88$4,331.60  
39Aug-24MainSWVXX1065.371$1,065.37  
40Sep-24MainSWVXX01$ -00
41Jul-24MainVTLE8943.61$3,881.29  
42Jul-24MainVYM91.0073124.33$11,314.94  
43Aug-24MainVYM91.0073127.36$11,590.69275.752119275.752119
44Sep-24MainVYM91.6129128$11,726.4558.24467258.244672
Sheet2
Cell Formulas
RangeFormula
G4:G44G4=LET(arrLookup,Table2[[#Headers],[Account]]:[@Account]&Table2[[#Headers],[Ticker]]:[@Ticker],arrReturn,Table2[[#Headers],[Shares]]:[@Price],arrPrevSharesPrice,XLOOKUP([@Account]&[@Ticker],DROP(arrLookup,-1),DROP(arrReturn,-1),"",0,-1),PrevShares,CHOOSECOLS(arrPrevSharesPrice,1),PrevPrice,CHOOSECOLS(arrPrevSharesPrice,2),CurrPrice,[@Price],IFERROR(PrevShares*(CurrPrice-PrevPrice),""))
H4:H44H4=LET(s,TAKE(SORT(FILTER(Table2[[Mo. End]:[Price]],([Mo. End]<=[@[Mo. End]])*([Account]=[@Account])*([Ticker]=[@Ticker])),1),-2),IFERROR(INDEX(s,1,4)*(INDEX(s,2,5)-INDEX(s,1,5)),""))
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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