Average of last X transactions by customer

BloodyBill

New Member
Joined
Oct 5, 2020
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a growing data set of 24K+ transactions made by more than 3,200 customers. I want to see an average number of units purchased by each customer. Easy. A pivot table with data summarized by average does the trick.

Except, I want to see the average based on recency. In other words, if Customer A has 35 transactions in the data set, what is the average of his last 4 transactions? His last 5? His last 3? I don't want to filter by date, because that might eliminate infrequent customers whose data is spread out over time.

Also, I need one more thing: If I'm averaging all the customers' most recent 5 transactions, but new Customer Q only has 3 or 4 transactions, I need the average of whatever I can get.

The dummy data below is pretty close to what I have: A transaction date. A unique customer identifier. And number of products at each transaction. (BTW, never more than one transaction per day.) The only difference is that I have 24K lines of data (transactions) and 3,200 customer identifiers.

Any help? (Especially non-VBA solutions).

Thanks!!

DateCustomerProducts
purchased
5/15/2024A2
5/15/2024B2
5/14/2024C1
4/30/2024A3
3/3/1901B1
11/17/1900C1
11/11/1900A3
3/5/2024B2
2/26/2024C3
2/15/2024A2
2/14/2024B1
2/2/2024C1
1/31/2024A3
1/31/2024B1
1/27/2024C1
1/28/2024A3
1/12/2024B2
1/2/2024C3
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This is a little clunky, i may have some extraneous arguments. But, it seems to work:

Book1
ABCDEFG
1DateCustomerProducts purchasedTranDepth4
22024-05-15A2A2.50
32024-05-15B2B1.50
42024-05-14C1C1.50
52024-04-30A3
61901-03-03B1
71900-11-17C1
81900-11-11A3
92024-03-05B2
102024-02-26C3
112024-02-15A2
122024-02-14B1
132024-02-02C1
142024-01-31A3
152024-01-31B1
162024-01-27C1
172024-01-28A3
182024-01-12B2
192024-01-02C3
20
Sheet3
Cell Formulas
RangeFormula
F2:F4F2=SORT(UNIQUE($B$2:$B$19))
G2:G4G2=LET(AD,$A$2:$C$19,D,$A$2:$A$19,C,$B$2:$B$19,P,$C$2:$C$19, CustData,SORT(FILTER(AD,C=$F2),1,-1),N,$G$1,Cdates,CHOOSECOLS(CustData,1),NthDate,LARGE(Cdates,N), NthProds,FILTER(AD,((D>=NthDate)*(C=$F2)),""),AVERAGE(CHOOSECOLS(NthProds,3)) )
Dynamic array formulas.
 
Upvote 0
N.B. I used Text to Columns to create real dates and I sorted on the Date field.

T202405.xlsm
ABCDEF
1DateCustomerProducts purchasedLast 4
211-Nov-00A3A2.50
317-Nov-00C1C1.50
403-Mar-01B1B1.50
502-Jan-24C3
612-Jan-24B2
727-Jan-24C1
828-Jan-24A3
931-Jan-24A3
1031-Jan-24B1
1102-Feb-24C1
1214-Feb-24B1
1315-Feb-24A2
1426-Feb-24C3
1505-Mar-24B2
1630-Apr-24A3
1714-May-24C1
1815-May-24A2
1915-May-24B2
1g
Cell Formulas
RangeFormula
E2:E4E2=UNIQUE(B2:B19)
F2:F4F2=AVERAGE(TAKE(FILTER($C$2:$C$19,$B$2:$B$19=E2),-$F$1))
Dynamic array formulas.
 
Last edited:
Upvote 0
T202405.xlsm
ABCDEF
1DateCustomerProducts purchasedLast 4
215-May-24A2A2.75
315-May-24B2B1.50
414-May-24C1C2.00
530-Apr-24A3
603-Mar-01B1
717-Nov-00C1
811-Nov-00A3
905-Mar-24B2
1026-Feb-24C3
1115-Feb-24A2
1214-Feb-24B1
1302-Feb-24C1
1431-Jan-24A3
1531-Jan-24B1
1627-Jan-24C1
1728-Jan-24A3
1812-Jan-24B2
1902-Jan-24C3
1g
Cell Formulas
RangeFormula
E2:E4E2=UNIQUE(B2:B19)
F2:F4F2=AVERAGE(TAKE(FILTER($C$2:$C$19,$B$2:$B$19=E2),-$F$1))
Dynamic array formulas.
I knew it could be much more succinct!
 
Upvote 0
Solution

Forum statistics

Threads
1,216,723
Messages
6,132,317
Members
449,718
Latest member
Marie42719

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