AMERGE

=AMERGE(l,r,klf,krt)

l
left array
r
right array
klf
link "key" of left array, integer or array of integers. 2 or {3,2} . 0 or ignored will be consider 1
krt
link "key" of right array, integer or array of integers. 2 or {3,2} . 0 or ignored will be consider 1

same functionality as in Power Querry join/merge scenario called Left Outer

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
860
Office Version
  1. 365
Platform
  1. Windows
AMERGE arrays Merge, same functionality as in Power Querry join/merge scenario called Left Outer. Calls ATEXTJOIN , ARRANGE , APPEND2H
Other functions used on minisheet ARRANGE , APIVOT
Excel Formula:
=LAMBDA(l,r,klf,krt,
    LET(d,"|",kl,UNIQUE(IF(klf=0,1,klf),1),kr,UNIQUE(IF(krt=0,1,krt),1),
        cl,COLUMNS(l),ql,SEQUENCE(,cl),cr,COLUMNS(r),qr,SEQUENCE(,cr),rl,ROWS(l),sl,SEQUENCE(rl),rr,ROWS(r),sr,SEQUENCE(rr),
        ckr,COLUMNS(kr),a,INDEX(l,sl,kl),b,INDEX(r,sr,kr),ta,ATEXTJOIN(a,,,d),tb,ATEXTJOIN(b,,,d),
        x,XMATCH(ta,tb),ar,ARRANGE(INDEX(r,x,qr),kr),
        ml,OR(ISNA(XMATCH(kl,ql))),mr,OR(ISNA(XMATCH(kr,qr))),
        IF(OR(ml,mr),"check keys",APPEND2H(l,ar,ckr+1))
    )
)
LAMBDA 7.0.xlsx
ABCDEFGHIJKLMNOPQRSTUVW
1PQ name: Left Outter (Classic lookup in right array)PQ name: Right Outter (Classic lookup in left array)
2ex. 1=AMERGE(A3:C15,E3:H11,{3,2},{2,4})=AMERGE(E3:H11,A3:C15,{2,4},{3,2})
3UnitsColorProductCodeProductPriceColorUnitsColorProductCodePriceCodeProductPriceColorUnits
448RedQuadCR-rCarlota26Red48RedQuadQD-r43CR-rCarlota26Red
5156BlueQuadKK-bKiki13Blue156BlueQuadQD-b41KK-bKiki13Blue
6168RedQuadQD-rQuad43Red168RedQuadQD-r43QD-rQuad43Red48
7132BlueCarlotaSH-rSunshine19Red132BlueCarlotaSH-rSunshine19Red108
872BlueCarlotaCR-gCarlota24Green72BlueCarlotaCR-gCarlota24Green
9108RedSunshineQD-bQuad41Blue108RedSunshineSH-r19QD-bQuad41Blue156
10128BlueQuadSH-bSunshine18Blue128BlueQuadQD-b41SH-bSunshine18Blue24
1196RedGigiKK-rKiki20Red96RedGigiKK-rKiki20Red
1260RedSunshine60RedSunshineSH-r19
1324BlueSunshine24BlueSunshineSH-b18Obs:If there are duplicates of keys in
14120BlueCarlota120BlueCarlotaleft array,only first occurrence is shown
1524BlueQuad24BlueQuadQD-b41
16
17ex. 2error debug.
18InvoiceProductSalesInvoiceDiscount=AMERGE(A4:C15,E4:H11,{4,2},{2,4})
194588Carlota13045880.065check keys
204588Quad55945890.0375
214588Sunshine11445900.12(key left columns out of range)
224589Quad559
234589Sunshine209=ARRANGE(J3#,{3,4,2,5})other functions used on minisheet
244590Carlota2869ProductCodeColorPriceUnitsARRANGE
25QuadQD-rRed4348APIVOT
26=AMERGE(A18:C24,E18:F21,,)QuadQD-bBlue41156
27InvoiceProductSalesDiscountTotalQuadQD-rRed43168
284588Carlota1306.50%121.55CarlotaBlue132
294588Quad5596.50%522.665CarlotaBlue72
304588Sunshine1146.50%106.59SunshineSH-rRed19108
314589Quad5593.75%538.0375QuadQD-bBlue41128
324589Sunshine2093.75%201.1625GigiRed96
334590Carlota286912.00%2524.72SunshineSH-rRed1960
34SunshineSH-bBlue1824
35=APIVOT(A28:E33,1,2,5,)CarlotaBlue120
36(1\2) 5 vf=0CarlotaQuadSunshineGrand TotalQuadQD-bBlue4124
374588121.55522.665106.59750.805
3845890538.0375201.1625739.2
3945902524.72002524.72
40Grand Total2646.271060.7025307.75254014.725
41
AMERGE post
Cell Formulas
RangeFormula
J2,P2,A35,A26,J23,J18J2=FORMULATEXT(J3)
J3:N15J3=AMERGE(A3:C15,E3:H11,{3,2},{2,4})
P3:T11P3=AMERGE(E3:H11,A3:C15,{2,4},{3,2})
J19J19=AMERGE(A4:C15,E4:H11,{4,2},{2,4})
J24:N36J24=ARRANGE(J3#,{3,4,2,5})
A27:D33A27=AMERGE(A18:C24,E18:F21,,)
E28:E33E28=C28:C33*(1-D28:D33)
A36:E40A36=APIVOT(A28:E33,1,2,5,)
Dynamic array formulas.
 
Upvote 0
Here are the general notes addendum of the 6 Power Querry scenarios for merging/joining tables.
4 functions will cover all 6 scenarios, for any arrays not only tables, no PQ or refresh needed: AIJOIN, ALMERGE, AMERGE, AJOIN
1. Inner Join (AND logical test) , our function AIJOIN
2. Left Anti (Only in left array), our function ALMERGE
3. Left Outer (Classic Lookup in right array), our function AMERGE (most common)
4. Right Anti (Only in right array), we can use ALMERGE inverting the order of arrays in the formula
5. Right Outer (Classic Lookup in left array), we can use AMERGE inverting the order of arrays in the formula
6. Full Outer (OR logical text), our function AJOIN
The "links" for this types of joining/merging are called "keys", primary (unique) or foreign keys. They represents common column or multiple columns found in both arrays (they share the same type of data of the respective columns and should have same dimension, in most of the cases).
The functions can handle also dups of keys along with unique keys, and for that reason we can have different outcomes if we apply same functions from left to right or right to left. Plenty of examples on minisheets to reveal this behavior.
Attached picture, courtesy of Mike Girvin ExcelIsFun together with some of the sample data found on these posts.
View attachment 37624
 
2nd addendum: All the formulas except AJOIN, can handle multiple columns "keys". In most cases they should have the same dimension. Anyhow, the functions can handle dif dimension "keys", but only if we are positive that their join values are similar and can be matched. As an example, an array can have as key, 2 sep columns, first names and last names, and the other array has only a single column key for first names and last names joined with a delimiter in between. The default delimiter used in my formula is "|", did not used it as a separate argument, but can be changed inside formulas. It is variable "d" defined after LET. Or we can use our text manipulation formulas to change any delimiter with "|". It has powerful versatility but has to be handled with care.
LAMBDA 7.0.xlsx
ABCDEFGHIJKLMNOPQRSTUVW
1different keys dimensions scenarioarray 1array 2Left Anti (Only in left array)Right Anti (Only in right array)
2list 1 samplelist 2 sample,dif delimitersFirst NameLast NameUnitsNamePrice=ALMERGE(F3:H14,K3:L14,{1,2},)=ALMERGE(K3:L14,F3:H14,,{1,2})
3Sol MarroquinRaven,BeattySolMarroquin29Raven|Beatty123First NameLast NameUnitsNamePrice
4Kiera McfallRoxanna MercierKieraMcfall14Roxanna|Mercier213SolMarroquin29Roxanna|Mercier213
5Raven BeattyFanny,DenningRavenBeatty23Fanny|Denning323KieraMcfall14Fanny|Denning323
6Elinore DeesLesha NoblesElinoreDees15Lesha|Nobles211ElinoreDees15Lesha|Nobles211
7Wei LockwoodWei/LockwoodWeiLockwood19Wei|Lockwood124DonaldEldridge24Gertrudis|Fitzpatrick245
8Donald EldridgeGertrudis FitzpatrickDonaldEldridge24Gertrudis|Fitzpatrick245ClaudioBeam34Bernita|Crutcher100
9Claudio BeamAngelita PackerClaudioBeam34Angelita|Packer267ReynaLuke7Shiela|Anaya110
10Angelita PackerBeaulah WengerAngelitaPacker23Beaulah|Wenger213VivanKeeney14Yolonda|Armstead423
11Reyna LukeMalvina HamerReynaLuke7Malvina|Hamer321
12Beaulah WengerBernita\CrutcherBeaulahWenger8Bernita|Crutcher100Inner Join (And logical test)Inner Join (And logical test)
13Malvina HamerShiela AnayaMalvinaHamer11Shiela|Anaya110=AIJOIN(F3:H14,K3:L14,{1,2},)=AIJOIN(K3:L14,F3:H14,,{1,2})
14Vivan KeeneyYolonda ArmsteadVivanKeeney14Yolonda|Armstead423First NameLast NameUnitsPriceNamePriceUnits
15RavenBeatty23123Raven|Beatty12323
16functionsLeft Outter (Classic lookup in right array)Right Outter (Classic lookup in left array)WeiLockwood19124Wei|Lockwood12419
17used in minisheetformula to change all delimiters=AMERGE(F3:H14,K3:L14,{1,2},)=AMERGE(K3:L14,F3:H14,1,{1,2})AngelitaPacker23267Angelita|Packer26723
18AREMOVE=AREMOVE(C3:C14,T_CHARS(,,", /\"),"|")NameLast NameUnitsPriceNamePriceUnitsBeaulahWenger8213Beaulah|Wenger2138
19T_CHARSRaven|BeattySolMarroquin29Raven|Beatty12323MalvinaHamer11321Malvina|Hamer32111
20AMERGERoxanna|MercierKieraMcfall14Roxanna|Mercier213Inner Join has the same outcome in different structures
21ALMERGEFanny|DenningRavenBeatty23123Fanny|Denning323
22AIJOINLesha|NoblesElinoreDees15Lesha|Nobles211Due to the way the functions are constructed, AJOIN can not be used
23AJOINWei|LockwoodWeiLockwood19124Wei|Lockwood12419with different keys dimensions
24Gertrudis|FitzpatrickDonaldEldridge24Gertrudis|Fitzpatrick245=AJOIN(F3:H14,K3:L14,{1,2},1)=AJOIN(K3:L14,F3:H14,,{1,2})
25Angelita|PackerClaudioBeam34Angelita|Packer26723First NameLast NameUnitsPriceNamePriceUnits
26Beaulah|WengerAngelitaPacker23267Beaulah|Wenger2138SolMarroquin29Raven|Beatty12323
27Malvina|HamerReynaLuke7Malvina|Hamer32111KieraMcfall14Roxanna|Mercier213
28Bernita|CrutcherBeaulahWenger8213Bernita|Crutcher100RavenBeatty23123Fanny|Denning323
29Shiela|AnayaMalvinaHamer11321Shiela|Anaya110ElinoreDees15Lesha|Nobles211
30Yolonda|ArmsteadVivanKeeney14Yolonda|Armstead423WeiLockwood19124Wei|Lockwood12419
31DonaldEldridge24Gertrudis|Fitzpatrick245
32ClaudioBeam34Angelita|Packer26723
33AngelitaPacker23267Beaulah|Wenger2138
34ReynaLuke7Malvina|Hamer32111
35BeaulahWenger8213Bernita|Crutcher100
36MalvinaHamer11321Shiela|Anaya110
37VivanKeeney14Yolonda|Armstead423
38#VALUE!#VALUE!#VALUE!213Sol29
39#VALUE!#VALUE!#VALUE!323Kiera14
40#VALUE!#VALUE!#VALUE!211Elinore15
41#VALUE!#VALUE!#VALUE!245Donald24
42#VALUE!#VALUE!#VALUE!100Claudio34
43#VALUE!#VALUE!#VALUE!110Reyna7
44#VALUE!#VALUE!#VALUE!423Vivan14
45
Key dif dim post
Cell Formulas
RangeFormula
O2,T24,O24,K17,F17,T13,O13,T2O2=FORMULATEXT(O4)
F3:G14F3=ASPLIT(A3:A14," ")
K3:K14K3=AREMOVE(C3:C14,T_CHARS(,,", /\"),"|")
O4:Q10O4=ALMERGE(F3:H14,K3:L14,{1,2},)
T4:U10T4=ALMERGE(K3:L14,F3:H14,,{1,2})
O15:R19O15=AIJOIN(F3:H14,K3:L14,{1,2},)
T15:V19T15=AIJOIN(K3:L14,F3:H14,,{1,2})
C18C18=FORMULATEXT(C19)
C19:C30C19=AREMOVE(C3:C14,T_CHARS(,,", /\"),"|")
F19:I30F19=AMERGE(F3:H14,K3:L14,{1,2},)
K19:M30K19=AMERGE(K3:L14,F3:H14,1,{1,2})
O26:R44O26=AJOIN(F3:H14,K3:L14,{1,2},1)
T26:V44T26=AJOIN(K3:L14,F3:H14,,{1,2})
Dynamic array formulas.
 

Forum statistics

Threads
1,224,836
Messages
6,181,252
Members
453,028
Latest member
letswriteafairytale

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