Hstack Problem

pto160

Active Member
Joined
Feb 1, 2009
Messages
482
Office Version
  1. 365
Platform
  1. Windows
I am trying to add an extra column using the Hstack function. The third column I need to use a Text join using the delimiter ":" for the refunds who are duplicate customers and the other ones just the single refund. Here is the data.

Book1
ABC
1NameAmountVoucher
2John Smith5010020
3Paul Rudd10010021
4John Smith20010022
5Jane Smith50010023
6
7=HSTACK(UNIQUE(A2:A5),SUMIFS(B2:B5,A2:A5,UNIQUE(A2:A5)))
8
9What I want:
10NameAmountVoucher
11John Smith25010020:10022
12Paul Rudd10010021
13Jane Smith50010023
Sheet1
 
Another formula that should work...
Excel Formula:
=VSTACK(A1:D1,DROP(REDUCE("",UNIQUE(A2:A5),LAMBDA(a,x,LET(f,FILTER(A2:D5,A2:A5=x),VSTACK(a,HSTACK(TAKE(f,1,2),SUM(INDEX(f,,3)),TEXTJOIN(":",,INDEX(f,,4))))))),1))
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
VBA Code:
=VSTACK({"Name","Name","Amount","Voucher"},LET(u,UNIQUE(A2:A5),w,UNIQUE(B2:B5),v,SUMIF(A2:A5,u,C2:C5),HSTACK(u,w,v,MAP(u,LAMBDA(x,TEXTJOIN(":",1,IF(A2:A5=x,D2:D5,"")))))))
In your headers, you listed the first one as "Name" instead of "ID". With that said, you can eliminate that array constant (and shorten your formula) by using the headers that are already in the existing data...
Excel Formula:
=VSTACK(A1:D1,LET(u,UNIQUE(A2:A5),w,UNIQUE(B2:B5),v,SUMIF(A2:A5,u,C2:C5),HSTACK(u,w,v,MAP(u,LAMBDA(x,TEXTJOIN(":",1,IF(A2:A5=x,D2:D5,"")))))))
 
Upvote 0
Thanks, Rick! I appreciate all your knowledge, insight, and critique!
 
Upvote 0
Rick, thank you so much. The formula works great. :) (y) This will save me a lot of time.
 
Upvote 0
Once again thanks Stephen_IV''s for the formula. Still trying to wrap my head around the formula. You can do some magical thing using the MAP function with the LAMDA function.
 
Upvote 0
Once again thanks Stephen_IV''s for the formula. Still trying to wrap my head around the formula. You can do some magical thing using the MAP function with the LAMDA function.
Have you ever programmed in VBA? If so, MAP (and REDUCE, SCAN, BYROW, BYVAl to some extent) is basically equivalent to a FOR EACH loop... each cell (if MAP's first argument is a range) or element (if it is an array) is fed one at a time into the variable assigned to the LAMBDA which you can then manipulate in the rest of the LAMBDA. MAP is more flexible than a simple FOR loop though in that you can pass multiple ranges, arrays or combinations of them into the LAMBDA (and provide multiple variables inside the LAMBDA to receive them), but for understanding purposes, you do not need to worry about this now. A simple approach that works a lot of the time (but unfortunately not all the time) is to work out the logic (without MAP or LAMBDA) directly on a single cell or value from an array and once you have your formula successfully processing that cell or element, then wrap the formula in a MAP/LAMBDA housing and replace the cell or element with the declared LAMBDA variable. Perhaps approaching it this way will ease your path into using the MAP/LAMBDA functions.
 
Upvote 0
Rick, thanks for sharing that knowledge about the MAP/LAMDA functions. I will also look look into the other functions that you referenced as well. :)
 
Upvote 0
A single REDUCE also works:
工作簿1.xlsx
ABCDEFG
1NameAmountVoucherNameAmountVoucher
2John Smith5010020John Smith25010020:10022
3Paul Rudd10010021Paul Rudd10010021
4John Smith20010022Jane Smith50010023
5Jane Smith50010023
Sheet1
Cell Formulas
RangeFormula
E1:G4E1=REDUCE(A1:C1,A2:A5,LAMBDA(x,y,IF(OR(y=x),x,VSTACK(x,HSTACK(y,SUMIF(A:A,y,B:B),TEXTJOIN(":",,FILTER(C:C,A:A=y)))))))
Dynamic array formulas.

1688022002691.png
 
Upvote 0
Wow. That reduce function is pretty powerful when combined with lamda. Thanks so much shaowu459. I will use that formula as well. :)
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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