Calculate # repurchases within 30 days of last sale

M10Ant

New Member
Joined
Dec 30, 2021
Messages
36
Office Version
  1. 365
Platform
  1. Windows
Hoping someone might be able to help as currently at a loss on working out a formula for this

I need a formula that will count the number of times a stock has been bought within 30 days of the last sale of that stock.

In the below example, my summary table on the right should be recording a count of "2" for APPL stock and "1" for MSFT stock.

For the APPL stock, the count of 2 would come from the two Buy orders on 16/06/2021 and 22/07/2021, both being within 30 days of the last sale of APPL stock on 10/07/2021.
For the MFST stock, the count of 1 would come from the Buy order on 26/07/2021, being within 30 days of the last sale of MFST stock on 23/07/2021

No of repurchases within 30 days of last disposal.xlsx
ABCDEFG
1
2Trade Execution TimeOrder TypeStockStockNo. of Repurchases within 30 days of last sale
303/06/2021BuyAPPLAPPL
407/06/2021BuyAPPLMSFT
514/06/2021BuyMSFT
615/06/2021BuyAPPL
722/06/2021BuyMSFT
810/07/2021SellAPPL
916/07/2021BuyAPPL
1022/07/2021BuyAPPL
1123/07/2021SellMSFT
1226/07/2021BuyMSFT
1305/11/2021BuyMSFT
Sheet1


I think I might need to use COUNTIFS or MAX, but so far have only worked out how to count the number of trades for each different type of stock, not a count of all buy orders within 30 days of the last sale (per each type of stock).
I'm also running Excel 365 so have access to the new MINIFS, MAXIFS functions. I just can't work out how to solve this currently.



Thanks in advance.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
try this:
工作簿1.xlsx
ABCDEF
1Trade Execution TimeOrder TypeStockStockNo. of Repurchases within 30 days of last sale
22021-6-3BuyAPPLAPPL2
32021-6-7BuyAPPLMSFT1
42021-6-14BuyMSFT
52021-6-15BuyAPPL
62021-6-22BuyMSFT
72021-7-10SellAPPL
82021-7-16BuyAPPL
92021-7-22BuyAPPL
102021-7-23SellMSFT
112021-7-26BuyMSFT
122021-11-5BuyMSFT
13
14I need a formula that will count the number of times a stock has been bought within 30 days of the last sale of that stock.
15
16
Sheet1
Cell Formulas
RangeFormula
F2:F3F2=LET(s,MAXIFS(A:A,C:C,E2,B:B,"Sell"),COUNTIFS(C:C,E2,B:B,"Buy",A:A,"<="&30+s,A:A,">"&s))
 
Upvote 0
.. same thing but applying it to your two apparent formal table structures

22 07 21.xlsm
ABCDEFG
1
2Trade Execution TimeOrder TypeStockStockNo. of Repurchases within 30 days of last sale
33/06/2021BuyAPPLAPPL2
47/06/2021BuyAPPLMSFT1
514/06/2021BuyMSFT
615/06/2021BuyAPPL
722/06/2021BuyMSFT
810/07/2021SellAPPL
916/07/2021BuyAPPL
1022/07/2021BuyAPPL
1123/07/2021SellMSFT
1226/07/2021BuyMSFT
135/11/2021BuyMSFT
14
Bought
Cell Formulas
RangeFormula
G3:G4G3=LET(tet,Table1[Trade Execution Time],stk,Table1[Stock],ot,Table1[Order Type],s,MAXIFS(tet,stk,[@Stock],ot,"Sell"),COUNTIFS(stk,[@Stock],ot,"Buy",tet,"<="&30+s,tet,">"&s))
 
Upvote 0
try this:
工作簿1.xlsx
ABCDEF
1Trade Execution TimeOrder TypeStockStockNo. of Repurchases within 30 days of last sale
22021-6-3BuyAPPLAPPL2
32021-6-7BuyAPPLMSFT1
42021-6-14BuyMSFT
52021-6-15BuyAPPL
62021-6-22BuyMSFT
72021-7-10SellAPPL
82021-7-16BuyAPPL
92021-7-22BuyAPPL
102021-7-23SellMSFT
112021-7-26BuyMSFT
122021-11-5BuyMSFT
13
14I need a formula that will count the number of times a stock has been bought within 30 days of the last sale of that stock.
15
16
Sheet1
Cell Formulas
RangeFormula
F2:F3F2=LET(s,MAXIFS(A:A,C:C,E2,B:B,"Sell"),COUNTIFS(C:C,E2,B:B,"Buy",A:A,"<="&30+s,A:A,">"&s))
Hi shaowu459. Thank you very much for this. I've plugged in your formula and converted to structured table references and it all works perfectly :)
 
Upvote 0
.. same thing but applying it to your two apparent formal table structures

22 07 21.xlsm
ABCDEFG
1
2Trade Execution TimeOrder TypeStockStockNo. of Repurchases within 30 days of last sale
33/06/2021BuyAPPLAPPL2
47/06/2021BuyAPPLMSFT1
514/06/2021BuyMSFT
615/06/2021BuyAPPL
722/06/2021BuyMSFT
810/07/2021SellAPPL
916/07/2021BuyAPPL
1022/07/2021BuyAPPL
1123/07/2021SellMSFT
1226/07/2021BuyMSFT
135/11/2021BuyMSFT
14
Bought
Cell Formulas
RangeFormula
G3:G4G3=LET(tet,Table1[Trade Execution Time],stk,Table1[Stock],ot,Table1[Order Type],s,MAXIFS(tet,stk,[@Stock],ot,"Sell"),COUNTIFS(stk,[@Stock],ot,"Buy",tet,"<="&30+s,tet,">"&s))
Hi Peter. Thank you very much for the formula also :) That gives me the same expected result as well. I've never come across "LET" or the "tet", "stk" "ot" before. I can possibly work out the rest but wondered if you could give me a brief explanation of what they are doing please.
 
Upvote 0
The LET function effectively allows you to introduce variables into your formula. This can make the formula shorter and more efficient than repeating the same calculations or references over and over in the formula. For example if you just plugged the relevant structured references into @shaowu459's formula, you would have come up with a much longer formula than mine.

There is nothing special about "tet", "stk" or "ot', they are just variable names that I chose (initials from the table column headings but they could just as easily have been "x", "y" and "z") to make repeated references to those table columns shorter.

More information about LET here: LET function
 
Upvote 0
The LET function effectively allows you to introduce variables into your formula. This can make the formula shorter and more efficient than repeating the same calculations or references over and over in the formula. For example if you just plugged the relevant structured references into @shaowu459's formula, you would have come up with a much longer formula than mine.

There is nothing special about "tet", "stk" or "ot', they are just variable names that I chose (initials from the table column headings but they could just as easily have been "x", "y" and "z") to make repeated references to those table columns shorter.

More information about LET here: LET function
Ah thank you Peter. I should have picked that up regards the variable names! That makes sense now.
 
Upvote 0
If either of you knew how and could advise, how would the formula need to be amended to restrict the returned "counts" to those found within a tax year (for me in the UK that would be start date 6th April one year, and 5th April the next).
 
Upvote 0
Just checking further, I don't think the formula is quite right yet, as when adding another sell and buy order for MFST, it should increase the count to a total of 2.
One buy within 30 days after sell on 23/07/2021 and one buy within 30 days after sell on 06/12/2021

No of repurchases within 30 days of last disposal.xlsx
ABCDEFG
1
2Trade Execution TimeOrder TypeStockStockNo. of Repurchases within 30 days of last sale
303/06/2021BuyAPPLAPPL2
407/06/2021BuyAPPLMSFT1
514/06/2021BuyMSFT
615/06/2021BuyAPPL
722/06/2021BuyMSFT
810/07/2021SellAPPL
916/07/2021BuyAPPL
1022/07/2021BuyAPPL
1123/07/2021SellMSFT
1226/07/2021BuyMSFT
1305/11/2021BuyMSFT
1406/12/2021SellMSFT
1510/12/2021BuyMSFT
16
Sheet1
Cell Formulas
RangeFormula
G3:G4G3=LET(tet,tblTrades[Trade Execution Time],stk,tblTrades[Stock],ot,tblTrades[Order Type],s,MAXIFS(tet,stk,[@Stock],ot,"Sell"),COUNTIFS(stk,[@Stock],ot,"Buy",tet,"<="&30+s,tet,">"&s))
 
Upvote 0
Sorry I appreciate I may not have explained the problem fully initially.
I'm needing a formula that records the number of buys of the same stock that has occurred within 30 days following a sell order for that stock prior, and for any time this has occurred.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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