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)
 
.. and I think still produces the correct results with any sorting (this one sorted by Total)

chasfh.xlsm
ABCDEFGH
3Mo. EndAccountTickerSharesPriceTotalM/M G/LTest
4Sep-24MainSCYX1801.43$257.40 -5.4
5Aug-24MainSCYX1801.46$262.805.4-120.6
6Jul-24MainSCYX1802.13$383.40120.6 
7Aug-24MainBCAB4011.77$709.77  
8Aug-24MainSWVXX1065.371$1,065.37  
9Jul-24MainSB2335.06$1,178.98  
10Sep-24MainBCAB6072.03$1,232.21104.26104.26
11Jul-24MainACIC12912.21$1,575.09  
12Aug-24MainOLPX9112.09$1,903.99  
13Sep-24MainOLPX9112.34$2,131.74227.75227.75
14Sep-24MainACIC28110.93$3,071.33-165.12-84.3
15Jul-24MainREAL8413.74$3,145.34  
16Aug-24MainACIC28111.23$3,155.6384.3-126.42
17Jul-24MainSBLK15322.54$3,448.62  
18Jul-24MainFSLR17215.99$3,671.83  
19Aug-24MainREAL14202.64$3,748.80-925.1-925.1
20Jul-24MainVTLE8943.61$3,881.29  
21Sep-24MainREAL14202.99$4,245.80497497
22Jul-24MainSQ7061.88$4,331.60  
23Aug-24MainHPP9115.15$4,691.65  
24Jul-24MainPHYS27118.98$5,143.58  
25Jul-24MainC8264.88$5,320.16  
26Sep-24MainHPP12594.55$5,728.45-546.6-546.6
27Jul-24MainHCA17363.05$6,171.85  
28Sep-24MainMOH25331.8$8,295.00 -449.75
29Sep-24MainOS26432.24$8,511.36  
30Aug-24MainMOH25349.79$8,744.75449.75 
31Sep-24MainALAB17850.54$8,996.12  
32Aug-24MainFOUR11083.1$9,141.00  
33Sep-24MainACMR48419.11$9,249.24  
34Sep-24MainMU9499.85$9,385.90  
35Sep-24MainFOUR11088.9$9,779.00638638
36Jul-24MainVYM91.0073124.33$11,314.94  
37Aug-24MainVYM91.0073127.36$11,590.69275.752119275.752119
38Sep-24MainVYM91.6129128$11,726.4558.24467258.244672
39Aug-24MainSHOP20074.07$14,814.00  
40Aug-24MainAMZN83178.5$14,815.50 -703.84
41Sep-24MainAMZN83184.76$15,335.08519.58519.58
42Jul-24MainAMZN83186.98$15,519.34184.26 
43Sep-24MainSHOP20079.14$15,828.0010141014
44Sep-24MainSWVXX01$ -00
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

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Man, I just don't know how you guys can figure this stuff out, but it's pretty amazing!

I apologize, @Alex Blakenburg, but I had to mark @Peter_SSs's new formula as the solution, since I can now sort by any column and the calculation does not break.

Peter, for the life of me, I cannot deconstruct this formula and figure out exactly how it works. Would you be so kind as to break it down and explain exactly why this does what we are asking it to do? I think I get the LET part, but the INDEX parts at the end are especially confusing at the moment. Thanks!
 
Upvote 0
Ok, so, @Peter_SSs, apologies for this, but I'm running into another issue, and it's my fault for not sharing this with you because I did not think of this needing to happen.

Typically we want to do month-over-month calculation based on prior month's shares because additional shares are added, and we can't include that in the gain/loss total. that would happen 95% of the time.

But sometimes shares are sold off, not added, so in those cases we want to base the calculation on the current month's shares, not the previous month's.

In that case we need some flavor of IF test where, if the value under Buy/Sell? column equals "Sell", we need to make that calculation based on same month's shares.

I tried to see if I could fit a revision into your formula, but since I can't deconstruct it to understand it, I don't know how I can add a reference to the additional column anits alternate formula.

I've posted the XL2BB below, showing the example for two issues we have. Off on the right are the differneces based on the calculation you did, and then what they should be with a new calcuation. Let me know if there's something more you need? I appreciate your continuing help on this (and I hope it's fun for you to figure it out!). Thanks again!

_Net Worth - Terrie and Chuck.xlsx
ABCDEFGHIJKLMNOPQ
2Mo. EndAccountTickerSharesPriceTotalM/M G/LMonthYearBuy/Sell?Shares2Total2Realized
3May-24TerrieNICHX388.555627.59$ 10,720.25 52024    Calculation: Should be:
4Jun-24TerrieNICHX388.555627.16$ 10,553.17$ (167.08)62024   $ (167.08)$ (167.08)
5Jul-24TerrieNICHX388.555627.41$ 10,650.31$ 97.1472024   $ 97.14$ 97.14
6Aug-24TerrieNICHX388.555627.57$ 10,712.48$ 62.1782024   $ 62.17$ 62.17
7Sep-24TerrieNICHX282.820927.12$ 7,670.10$ (174.85)92024Sell-1057.3529252.36$ (174.85)$ (127.27)
8May-24CP-ManagedNPSRX395.13115.25$ 6,025.75 52024   
9Jun-24CP-ManagedNPSRX397.093715.30$ 6,075.53$ 19.7662024   $ 19.76$ 19.76
10Jul-24CP-ManagedNPSRX399.048915.44$ 6,161.32$ 55.5972024   $ 55.59$ 55.59
11Aug-24CP-ManagedNPSRX400.990215.62$ 6,263.47$ 71.8382024   $ 71.83$ 71.83
12Sep-24CP-ManagedNPSRX383.939615.81$ 6,070.09$ 76.1992024Sell-189.6333000$ 76.19$ 72.95
R-IY Worksheet (2)
Cell Formulas
RangeFormula
H3:H9,H11:H12H3=MONTH(A3)
I3:I9,I11:I12I3=YEAR(A3)
J3:J12J3=IFNA(IF(XLOOKUP([@Account]&[@Ticker]&[@Month]&[@Year],History[Account]&History[Symbol]&History[Month]&History[Year],History[Action])="buy","Buy",IF(XLOOKUP([@Account]&[@Ticker]&[@Month]&[@Year],History[Account]&History[Symbol]&History[Month]&History[Year],History[Action])="sell","Sell","")),"")
K3:K12K3=IF([@[Buy/Sell?]]="Buy",(XLOOKUP([@Account]&[@Ticker]&[@Month]&[@Year],History[Account]&History[Symbol]&History[Month]&History[Year],History[Quantity])),IF([@[Buy/Sell?]]="Sell",-(XLOOKUP([@Account]&[@Ticker]&[@Month]&[@Year],History[Account]&History[Symbol]&History[Month]&History[Year],History[Quantity])),""))
L3:L12L3=IF([@[Buy/Sell?]]="Buy",(XLOOKUP([@Account]&[@Ticker]&[@Month]&[@Year],History[Account]&History[Symbol]&History[Month]&History[Year],History[Amount])),IF([@[Buy/Sell?]]="Sell",(XLOOKUP([@Account]&[@Ticker]&[@Month]&[@Year],History[Account]&History[Symbol]&History[Month]&History[Year],History[Amount])),""))
Q4:Q6,Q9:Q11Q4=O4
O4:O7,O9:O12O4=D3*(Table24[@Price]-E3)
G3:G12G3=LET(s,TAKE(SORT(FILTER(Table24[[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
As you previously deduced my original formula was based on the table being sorted, so if that is the case then Peter's formula is definitely the way to go.
If you just want to use the current Qty if the previous Qty > the current row Qty, this modification to Peter's formula should work.
Note: It mentions Table3 in one spot change that to your table name

Rich (BB code):
=LET(s,TAKE(SORT(
              FILTER(Table3[[Mo. End]:[Price]],
                     ([Mo. End]<=[@[Mo. End]])*([Account]=[@Account])*([Ticker]=[@Ticker])),1),-2),
     SharesQtyToUse, IF(INDEX(s,1,4)<=INDEX(s,2,4),INDEX(s,1,4),INDEX(s,1,5)),
     IFERROR(SharesQtyToUse*(INDEX(s,2,5)-INDEX(s,1,5)),""))
 
Upvote 0
I don't follow the examples. Formulas shown seem to refer to tables called 'Table24' and 'History'. I don't know what either of those are in relation to the sample data shown.

But sometimes shares are sold off, not added, so in those cases we want to base the calculation on the current month's shares, not the previous month's.

In that case we need some flavor of IF test where, if the value under Buy/Sell? column equals "Sell", we need to make that calculation based on same month's shares.
For what you are asking, do you really need a Buy/Sell? column? Wouldn't it be simply that if the number of shares is less that what it was in the previous month then there must have been a sale?

I suspect there will be a simpler formula but until I can understand the data and requirement better .....

Back to the sample data:
NICHX was 282.8209 shares @ 27.12 in Sep-24. In Aug-24 it was 388.5556 shares @ 27.57. What are the steps in the calculation you want for NICHX for the row Sep-24?
 
Upvote 0
I don't follow the examples. Formulas shown seem to refer to tables called 'Table24' and 'History'. I don't know what either of those are in relation to the sample data shown.


For what you are asking, do you really need a Buy/Sell? column? Wouldn't it be simply that if the number of shares is less that what it was in the previous month then there must have been a sale?

I suspect there will be a simpler formula but until I can understand the data and requirement better .....

Back to the sample data:
NICHX was 282.8209 shares @ 27.12 in Sep-24. In Aug-24 it was 388.5556 shares @ 27.57. What are the steps in the calculation you want for NICHX for the row Sep-24?
'Table24' is Table 2, I had copied the tab so I could manipulate that one instead of the original so I could avoid inadvertently screwing up that original. 'History' is a table on another tab this is pulling the words "Buy" or "Sell" into the cell, if there was a purchase or a sale.

I do need a buy/sell column, for two reasons: (1) if there was a "buy", I need to subtract the bought shares under Shares2 column from the same month's shares under Shares column so I can calculate the M/M G/L of this month's price minus last month's price based only on those shares carried over; and (2) if there was a "sell", I need to calculate M/M G/L based only on remaining shares this month after the sell, not on prior month's shares carried over—but, additionally, I need to calculate a realized gain under Realized column, which you don't have to worry about for the purposes of helping me here.

Am I making sense?
 
Upvote 0
Am I making sense?
Yes, in relation to my first question.


No, in relation to my second question which doesn't seem to me to have been addressed.
Back to the sample data:
NICHX was 282.8209 shares @ 27.12 in Sep-24. In Aug-24 it was 388.5556 shares @ 27.57. What are the steps in the calculation you want for NICHX for the row Sep-24?
 
Upvote 0
Back to the sample data:
NICHX was 282.8209 shares @ 27.12 in Sep-24. In Aug-24 it was 388.5556 shares @ 27.57. What are the steps in the calculation you want for NICHX for the row Sep-24?
I hope I understand what you're looking for here ...

The logic will go:
  • Check to see whether Buy/Sell? column (cell J7) contains "Sell";
  • If yes, return same month's shares (D7) minus (same month's price (E7) minus previous month's price (E6))
  • If no, return previous month's shares (D6) minus (same month's price (E7) minus previous month's price (E6))—that is, same process that you have in there now.
If this isn't what you're looking for, please advise. Thanks!
 
Upvote 0
If yes, return same month's shares (D7) minus (same month's price (E7) minus previous month's price (E6))
So according to that
D7 minus (E7 -E6)
282.8209 - (27.12 - 27.57)
282.8209 - (-0.45)
283.2709

You said post 13 showed what the result should be but I don't see this figure anywhere in that post.

I did wonder if you actually meant
If yes, return same month's shares (D7) times (same month's price (E7) minus previous month's price (E6))
. . but applying that
D7 * (E7 - E6)
282.8209 * (-0.45)
-127.27
Again I do not see that figure anywhere in post 13.

So I am still not sure what calculation you do actually want. :confused:
 
Upvote 0
So according to that
D7 minus (E7 -E6)
282.8209 - (27.12 - 27.57)
282.8209 - (-0.45)
283.2709

You said post 13 showed what the result should be but I don't see this figure anywhere in that post.

I did wonder if you actually meant

. . but applying that
D7 * (E7 - E6)
282.8209 * (-0.45)
-127.27
Again I do not see that figure anywhere in post 13.

So I am still not sure what calculation you do actually want. :confused:

The $-127.27 figure is in cell Q7 of Post 13, in red.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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