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)
 
The $-127.27 figure is in cell Q7 of Post 13, in red.
:( :oops: Doh! Your whole mini sheet did not fit in my browser window and I hadn't scrolled right to see that. Sorry about that.

Would this small change do it? The only change is this
=LET(s,TAKE(SORT(FILTER(Table24[[Mo. End]:[Price]],([Mo. End]<=[@[Mo. End]])*([Account]=[@Account])*([Ticker]=[@Ticker])),1),-2),IFERROR(MIN(INDEX(s,0,4))*(INDEX(s,2,5)-INDEX(s,1,5)),""))

I have not used the Buy/Sell column. My logic was that if a SELL then use the current (smaller) number, if a BUY then use the previous (smaller) number and if neither SELL nor BUY then use either number (also = smaller) so always just use the smaller of current and previous number.
I had this logic in mind for this calculation when I asked before about whether you actually needed the Buy/Sell column.

chasfh.xlsm
ABCDEFJN
2Mo. EndAccountTickerSharesPriceTotalBuy/Sell?Test2
3May-24TerrieNICHX388.555627.59$10,720.25 
4Jun-24TerrieNICHX388.555627.16$10,553.17-167.078908
5Jul-24TerrieNICHX388.555627.41$10,650.3197.1389
6Aug-24TerrieNICHX388.555627.57$10,712.4862.168896
7Sep-24TerrieNICHX282.820927.12$7,670.10Sell-127.269405
8May-24CP-ManagedNPSRX395.13115.25$6,025.75 
9Jun-24CP-ManagedNPSRX397.093715.3$6,075.5319.75655
10Jul-24CP-ManagedNPSRX399.048915.44$6,161.3255.593118
11Aug-24CP-ManagedNPSRX400.990215.62$6,263.4771.828802
12Sep-24CP-ManagedNPSRX383.939615.81$6,070.09Sell72.948524
Sheet3
Cell Formulas
RangeFormula
N3:N12N3=LET(s,TAKE(SORT(FILTER(Table24[[Mo. End]:[Price]],([Mo. End]<=[@[Mo. End]])*([Account]=[@Account])*([Ticker]=[@Ticker])),1),-2),IFERROR(MIN(INDEX(s,0,4))*(INDEX(s,2,5)-INDEX(s,1,5)),""))
 
Upvote 0
Solution

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Again, @Peter_SSs, just great, great work. That seems to have solved it.

It's a more elegant solution to add the MIN in there because, you're right, the formula shouldn't have to depend on what's in the BUY/SELL column. The only possible glitch that might come up is when a stock is both bought (whether purchase or dividend reinvestment) and sold within the same month, which may be an issue since this table summarizes an entire month into one line.

I am fascinated by the formula, which employs a lot of command I had never used. I think I was able to figure out how LET, TAKE, SORT, and FILTER work. I am trying to get my arms around exactly how SORT and INDEX works in this case.

I understand SORT sorts the contents of a range or array in ascending or descending order, and that values can be sorted by one or more columns. How exactly is that working here? Month, Account, and Ticker can all be sorted individually—how does SORT determine sorting when it's in the formula as an array?

I know INDEX returns the value at a given location in a range or array, and can retrieve individual values, or entire rows and columns. I get that the "s" refers to the formula shortened by LET; I get that "4" is column 4 and "5" is column 5 in the table; and I get the math part. Can you help me understand how the "0" and "2" and "1" within works, precisely?

I really appreciate your work on this, and thanks in advance for helping me understand what's under the hood (or "bonnet", as the case may be) of the formula!
 
Upvote 0
Hmm, let's try by looking at how the result in row 7 is obtained

=LET(s,TAKE(SORT(FILTER(Table24[[Mo. End]:[Price]],([Mo. End]<=[@[Mo. End]])*([Account]=[@Account])*([Ticker]=[@Ticker])),1),-2),IFERROR(MIN(INDEX(s,0,4))*(INDEX(s,2,5)-INDEX(s,1,5)),""))

s is just a name/variable that takes its value from the red part of the formula. I think that you already have figured most of this but it takes the first 5 columns of the table Table24[[Mo. End]:[Price]] and filters that for rows that have a date <= the date in row 7 ([Mo. End]<=[@[Mo. End]]) and Account the same as the Account in row 7 ([Account]=[@Account]) and Ticker the same as the Ticker in row 7 ([Ticker]=[@Ticker])
Once that FILTER is done we would have this
1729574319768.png


That data is then SORTed by the first column, which in this case does not change anything since those dates are already in order.
Next is TAKE(...., -2) which takes just the last 2 rows giving this ..
1729574550468.png

which must be the current row and the most recent previous date row.

So now we get to the blue bit where s is the 2-row table above.
INDEX(s,0,4) The 0 means take all rows and the 4 means from column 4 (Shares column). MIN obviously takes the smallest value (per my logic outlined in post 21)
This is multiplied by (INDEX(s,2,5)-INDEX(s,1,5)) being the 2nd row col 5 (current price) minus 1st row col 5 (price in the most recent previous row)
The IFERROR is there to catch the case where there is not 2 rows in the "s" table (ie no previous data exists for that account & ticker)
 
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