AUNIQUE

=AUNIQUE(a,k)

a
required. array
k
required -1 vertical, 0 all vertical, 1 horizontal

AUNIQUE extracts unique values, horizontal (for each row) left aligned , vertical (for each column) top aligned , all (in a vertical array).

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
860
Office Version
  1. 365
Platform
  1. Windows
AUNIQUE extracts unique values, horizontal (for each row) left aligned , vertical (for each column) top aligned , all (in a vertical array)
calls AFLATTEN , AUNQSRT
Excel Formula:
=LAMBDA(a,k,
    LET(x,OR(k={-1,0,1}),
       af,AFLATTEN(a),
       au,UNIQUE(FILTER(af,af<>"")),
       IF(x,SWITCH(k,0,au,1,AUNQSRT(a,),-1,TRANSPOSE(AUNQSRT(TRANSPOSE(a),))),"-1 vert., 0 all vert , 1 horiz.")
   )
)
LAMBDA 5.0.xlsx
ABCDEFGH
1dd4unq. Hk=1
21ghd4
31dh1gh
4a341dh
5d44a34
633d4
7c223
8c2
9
10uniq.allk=0unq. Vk= -1
11ddd4
1241gh
131a33
14g342
15hc2
16a
173
18c
192
20
AUNIQUE post
Cell Formulas
RangeFormula
E2:G8E2=AUNIQUE(A1:C7,1)
A11:A19A11=AUNIQUE(A1:C7,)
E11:G15E11=AUNIQUE(A1:C7,-1)
Dynamic array formulas.
 
Upvote 0
Latest MrExcel YT video (8Oct2022) triggered me to write this study about all aspects of UNIQUE function and how we can enhance its functionality.
Excel Unique Across Each Individual Row - 2519
Before getting to extracting unique values byrow/bycol/byarray, that can be done with general use lambda helper functions that can spill, in more than 3 different ways, will see how we can improve main UNIQUE function's versatility.
First about UNIQUE functionality and its arguments.
AUNIQUE.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAI
1 =UNIQUE(array,[by_col],[exactly_once])
2
31. UNIQUE is case-insensitive
41.a. UNIQUE rows, "by_col" argument omitted1.b. UNIQUE columns, "by_col" argument = 1
5
6ar=UNIQUE(B7:B12)ar=UNIQUE(J7:L12)arAabBaraAbBcc
7AAaaAaaAaABbc
8abAAabB0=UNIQUE(T6:W6,1)Aa
9bcbBc00Ab
10BdBbcc0=UNIQUE(AC6:AH8,1)
11ccblanks=> 0'sabcc
12dccaB0c
13A000
14blanks=> 0's
15
162. UNIQUE "exactly_once" argument=1 (but not twice, 3 times..etc)
171.a. UNIQUE rows, "by_col" argument omitted1.b. UNIQUE columns, "by_col" argument = 1
18
19ar=UNIQUE(B20:B25,,1)ar=UNIQUE(J20:L25,,1)arAabBcdaraAbBcc
20AcaaAc00aABbc
21adAAacc0=UNIQUE(T19:Y19,1,1)Aa
22bbBblanks=> 0'scd
23BBb=UNIQUE(AC19:AH21,1,1)
24cccc
25dcc0c
2600
27blanks=> 0's
28
concept 1
Cell Formulas
RangeFormula
D6,AC23,T21,N19,D19,AC10,T8,N6D6=FORMULATEXT(D7)
D7:D10D7=UNIQUE(B7:B12)
N7:P10N7=UNIQUE(J7:L12)
T9:U9T9=UNIQUE(T6:W6,1)
AC11:AF13AC11=UNIQUE(AC6:AH8,1)
D20:D21D20=UNIQUE(B20:B25,,1)
N20:P21N20=UNIQUE(J20:L25,,1)
T22:U22T22=UNIQUE(T19:Y19,1,1)
AC24:AD26AC24=UNIQUE(AC19:AH21,1,1)
Dynamic array formulas.
 
Added functionality to the following 2 functions UNQROWS and UNQCOLS.
- occurrence argument "oc"
- case sensitive, "ex" (exact argument)
UNQROWS(ar,[oc],[ex]) Unique Rows
ar: array
[oc]: occurrence argument
- if oc=-1, occurrences distribution next to each unique row
- if oc omitted or 0, unique rows
- if oc>0 unique rows that correspond with occurrence value "oc", if not found=>NA()
[ex]: exact argument
- if ex omitted or 0, case-insensitive
- if ex=1 or <>0, case-sensitive (triggers EXACT function)
Fun fact: concept based only on counting occurrences/exact occurrences, not a single UNIQUE is used
Note: If initial array "ar" has errors we can use as input array IFERROR(ar,...) or anybody can alter the function as they wish. I took care of the blanks though.
Excel Formula:
=LAMBDA(ar, [oc], [ex],
    LET(
        a, IF(ar = "", "", ar),
        b, BYROW(a, LAMBDA(x, CONCAT(x))),
        s, SCAN(
            0,
            SEQUENCE(ROWS(a)),
            LAMBDA(v, i,
                LET(
                    x, INDEX(b, i),
                    y, INDEX(b, SEQUENCE(i)),
                    SUM(--IF(ex, EXACT(x, y), x = y))
                )
            )
        ),
        f, FILTER(b, s = 1),
        r, FILTER(a, s = 1),
        m, MAP(f, LAMBDA(x, SUM(--(IF(ex, EXACT(x, b), x = b))))),
        SWITCH(oc, -1, HSTACK(r, m), 0, r, FILTER(r, m = oc, NA()))
    )
)
AUNIQUE.xlsx
ABCDEFGHIJKLMNO
11. ex, omitted2. ex,1
2oc,omittedoc,1oc,omittedoc,1
3ar=UNQROWS(B4:B20)=UNQROWS(B4:B20,1)=UNQROWS(B4:B20,,1)=UNQROWS(B4:B20,1,1)
4AAeAC
5AbFad
6acbD
7ad=UNIQUE(B4:B20,,1)Be
8aeecF
9bFFC
10bdoc,3
11B=UNIQUE(B4:B20)oc,5D=UNQROWS(B4:B20,3,1)
12BA=UNQROWS(B4:B20,5)ea
13BbAFB
14ccb
15cdoc,-1oc,2
16Ceoc,2=UNQROWS(B4:B20,-1,1)=UNQROWS(B4:B20,2,1)
17dF=UNQROWS(B4:B20,2)A2A
18Dda3b
19eoc,-1b2c
20F=UNQROWS(B4:B20,-1)oc,3B3
21A5=UNQROWS(B4:B20,3)c2oc,4
22b5cC1=UNQROWS(B4:B20,4,1)
23c3d1#N/A
24d2oc,4D1
25e1=UNQROWS(B4:B20,4)e1
26F1#N/AF1
27
UNQROWS 1
Cell Formulas
RangeFormula
D3,G3,G25,M22,G21,D20,G17,J16,M16,G12,M11,D11,G7,J3,M3D3=FORMULATEXT(D4)
D4:D9D4=UNQROWS(B4:B20)
G4:G5G4=UNQROWS(B4:B20,1)
J4:J13J4=UNQROWS(B4:B20,,1)
M4:M8M4=UNQROWS(B4:B20,1,1)
G8:G9G8=UNIQUE(B4:B20,,1)
D12:D17D12=UNIQUE(B4:B20)
M12:M13M12=UNQROWS(B4:B20,3,1)
G13:G14G13=UNQROWS(B4:B20,5)
J17:K26J17=UNQROWS(B4:B20,-1,1)
M17:M19M17=UNQROWS(B4:B20,2,1)
G18G18=UNQROWS(B4:B20,2)
D21:E26D21=UNQROWS(B4:B20,-1)
G22G22=UNQROWS(B4:B20,3)
M23M23=UNQROWS(B4:B20,4,1)
G26G26=UNQROWS(B4:B20,4)
Dynamic array formulas.
 
AUNIQUE.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
11. ex, omitted2. ex,1
2
3ar=UNQROWS(B4:D20)=UNQROWS(B4:D20,-1)=UNQROWS(B4:D20,,1)=UNQROWS(B4:D20,1,1)
4aaAaaAaaA7aaAaaa
5AAabbbb7AAaAAA
6aaacc1aaabb
7AAAcccc1AAABB
8aaAcccccc1bbc
9aaABBcc
10AAa=UNIQUE(B4:D20)=UNQROWS(B4:D20,1)bBccc
11bbaaAcBb
12BBbb0ccc=UNQROWS(B4:D20,2,1)
13bBc00cccccAAa
14Bbcc0cccBb
15bBccc=UNIQUE(B4:D20,,1)
16bBc00=UNQROWS(B4:D20,-1,1)=UNQROWS(B4:D20,3,1)
17Bbcc0aaA3aaA
18ccccAAa2bB
19ccaaa1
20ccc=UNQROWS(B4:D20,7)AAA1
21aaAbb1
22bbBB1
23bB3
24=UNQROWS(B4:D20,3)Bb2
25#N/Ac1
26cc1
27ccc1
28
UNQROWS 2
Cell Formulas
RangeFormula
F3,K24,K20,Q16,V16,K15,V12,K10,F10,Q3,V3,K3F3=FORMULATEXT(F4)
F4:H8F4=UNQROWS(B4:D20)
K4:N8K4=UNQROWS(B4:D20,-1)
Q4:S14Q4=UNQROWS(B4:D20,,1)
V4:X10V4=UNQROWS(B4:D20,1,1)
F11:H15F11=UNIQUE(B4:D20)
K11:M13K11=UNQROWS(B4:D20,1)
V13:X14V13=UNQROWS(B4:D20,2,1)
K16:M18K16=UNIQUE(B4:D20,,1)
Q17:T27Q17=UNQROWS(B4:D20,-1,1)
V17:X18V17=UNQROWS(B4:D20,3,1)
K21:M22K21=UNQROWS(B4:D20,7)
K25K25=UNQROWS(B4:D20,3)
Dynamic array formulas.
 
One of the lambda helper functions that can spill results, Row Byrow : RBYROW
Similar to SPILLBYROWS only without the extensive error management like filtering out errors or ignoring blanks. This can be done through lambda helper argument like we do with all the other built in lambda helper functions. Minimalistic approach.
RBYROW(ar,fn) Row BYROW lambda helper function
ar: array
fn: lambda helper argument
Excel Formula:
=LAMBDA(ar, fn,
    LET(
        a, IF(ar = "", "", ar),
        c, --(COLUMNS(a) = 1),
        r, REDUCE(0, SEQUENCE(ROWS(a)), LAMBDA(v, i, VSTACK(v, fn(INDEX(a, i, c))))),
        DROP(IFNA(r, ""), 1)
    )
)
AUNIQUE.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACAD
1Some RBYROW examples. (pattern's paradise)
2The only lambda helper function that can expand rows
3=RBYROW(SEQUENCE(5),LAMBDA(x,SEQUENCE(,x)))
41rpt rows=RBYROW(SEQUENCE(3),LAMBDA(x,SEQUENCE(INDEX(P5:P7,x))^0*x))
512Ax31
6123By41=INDEX(N5:O7,R5#,{1,2})
71234Cz21Ax
8123452Ax
92Ax
10=RBYROW(SEQUENCE(5,,5,-1),LAMBDA(x,SEQUENCE(,x)))2By
11123452By
1212343By
131233By
1412Cz
151Cz
16
17=RBYROW(SEQUENCE(4,2),LAMBDA(x,SEQUENCE(,MAX(x))))
1812
191234=RBYROW(SEQUENCE(5),LAMBDA(x,SEQUENCE(3,x)))
201234561
21123456782=RBYROW(SEQUENCE(5),LAMBDA(x,SEQUENCE(x,x)))
2231
231212
243434
2556123
26123456
27456789
287891234
2912345678
3056789101112
31910111213141516
321234512345
33678910678910
3411121314151112131415
351617181920
362122232425
37
RBYROW 1
Cell Formulas
RangeFormula
B3,U21,N19,B17,B10,T6,R4B3=FORMULATEXT(B4)
B4:F8B4=RBYROW(SEQUENCE(5),LAMBDA(x,SEQUENCE(,x)))
R5:R13R5=RBYROW(SEQUENCE(3),LAMBDA(x,SEQUENCE(INDEX(P5:P7,x))^0*x))
T7:U15T7=INDEX(N5:O7,R5#,{1,2})
B11:F15B11=RBYROW(SEQUENCE(5,,5,-1),LAMBDA(x,SEQUENCE(,x)))
B18:I21B18=RBYROW(SEQUENCE(4,2),LAMBDA(x,SEQUENCE(,MAX(x))))
N20:R34N20=RBYROW(SEQUENCE(5),LAMBDA(x,SEQUENCE(3,x)))
U22:Y36U22=RBYROW(SEQUENCE(5),LAMBDA(x,SEQUENCE(x,x)))
Dynamic array formulas.
 
AUNIQUE.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACAD
1RBYROW with UNIQUETask: extract unique positive nr., ascending order
2
3ar=RBYROW(B4:K23,LAMBDA(x,UNIQUE(x,1)))=RBYROW(B4:K23,LAMBDA(x,SORT(UNIQUE(FILTER(x,x>0),1),,,1)))
40-32213-2-2320-3213-2123
523-1-232-201-223-1-201123
6-33-12-3-12-11-2-33-121-2123
71-221013-13-21-2203-1123
8011-3312-22-101-332-2-1123
90-310-2-1-3-2000-31-2-11
102303-2-2-1-330230-2-1-323
111-210-1-3-1-1-101-20-1-31
12-31-1-2-22-3-1-3-2-31-1-2212
13-3-13003-11-1-3-3-130113
141-21-2020-31-11-202-3-112
15-1-310-2-233-20-1-310-2313
16-12-11-120-101-121012
1712001-20000120-212
18-3-3-3-231-20-2-3-3-231013
193-10-2-1-1-2-1-133-10-23
202-2-32023-3302-2-30323
2103-22-3-3-123003-22-3-123
221-311-202-1-1-11-3-202-112
2303-310-21-30103-31-213
24
RBYROW 2
Cell Formulas
RangeFormula
M3M3=FORMULATEXT(N4)
V3V3=FORMULATEXT(V4)
N4:T23N4=RBYROW(B4:K23,LAMBDA(x,UNIQUE(x,1)))
V4:X23V4=RBYROW(B4:K23,LAMBDA(x,SORT(UNIQUE(FILTER(x,x>0),1),,,1)))
Dynamic array formulas.
 
AUNIQUE.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAI
1RBYROW with UNIQUE, various
2Unique Unique numbers, vertical array (expanding rows), ascending order byrow
3ar=RBYROW(B4:L7,LAMBDA(x,UNIQUE(x,1)))=RBYROW(B4:L7,LAMBDA(x,SORT(TOCOL(UNIQUE(FILTER(x,ISNUMBER(x)),1)))))
4bb22aaa133cb2a13c1
5dacccddc233dac232
678998877566789563
7zyyxx443355zyx4352
83
9Unique text only5
10=RBYROW(B4:L7,LAMBDA(x,UNIQUE(FILTER(x,ISTEXT(x),""),1)))6
11bac7
12dac8
139
14zyx3
154
16Unique numbers, ascending order5
17=RBYROW(B4:L7,LAMBDA(x,SORT(UNIQUE(FILTER(x,ISNUMBER(x),""),1),,,1)))
18123
1923
2056789
21345
22
RBYROW 3
Cell Formulas
RangeFormula
N3,V3N3=FORMULATEXT(N4)
N4:S7N4=RBYROW(B4:L7,LAMBDA(x,UNIQUE(x,1)))
V4:V16V4=RBYROW(B4:L7,LAMBDA(x,SORT(TOCOL(UNIQUE(FILTER(x,ISNUMBER(x)),1)))))
J10J10=FORMULATEXT(N11)
N11:P14N11=RBYROW(B4:L7,LAMBDA(x,UNIQUE(FILTER(x,ISTEXT(x),""),1)))
H17H17=FORMULATEXT(N18)
N18:R21N18=RBYROW(B4:L7,LAMBDA(x,SORT(UNIQUE(FILTER(x,ISNUMBER(x),""),1),,,1)))
Dynamic array formulas.
 
I like the UNQROWS function, @Xlambda.

Pretending to be the user who wants to use this function. I would expect [oc] is the minimum occurrence number instead of finding the exact number of occurrence. Perhaps another flag parameter to define if I want to find the exact occurrence or otherwise.

Nice function. Thank you!
 
I like the UNQROWS function, @Xlambda.

Pretending to be the user who wants to use this function. I would expect [oc] is the minimum occurrence number instead of finding the exact number of occurrence. Perhaps another flag parameter to define if I want to find the exact occurrence or otherwise.

Nice function. Thank you!
Thank you Suat!! 🙏
The name Excel uses for third argument,"exactly_once", made me extend the logic to "exactly_twice", "exactly_3 times", and so on.
If we have total count, we can go anywhere we want with FILTER.
So, which one you think is more relevant, min(oc), max(oc) or for a given oc, <oc, <=oc, >oc, >=oc, <>oc ?
Or could it be intervals v1<oc<=v2 ?
Let me know and all this can be done easily with an extra argument.
 
Thank you Suat!! 🙏
The name Excel uses for third argument,"exactly_once", made me extend the logic to "exactly_twice", "exactly_3 times", and so on.
If we have total count, we can go anywhere we want with FILTER.
So, which one you think is more relevant, min(oc), max(oc) or for a given oc, <oc, <=oc, >oc, >=oc, <>oc ?
Or could it be intervals v1<oc<=v2 ?
Let me know and all this can be done easily with an extra argument.
Actually, I am amazed to see how possibilities are almost endless by using lambda functions! I remember the first time I met array formulas in Excel when I was trying to solve a 600x600 matrix problem, it was for my final thesis more than two decades ago! Glad Excel was not improved like today because if it was, I wouldn't have needed to learn to program in other languages back then!! I really can't decide if the new functions look make things easy just because I am a programmer or if they are just easy to use for everyone! However, one thing is for sure, they are amazing.

I don't like using string parameters, otherwise, I could say something like =COUNTIF(A1:A5,"<=3") where the string parameter can define the criteria. Instead of that, I would use one extra enum parameter that will represent three conditions to make this function more powerful:
-1 : min(oc)
0 : exact(oc)
1: max(oc)
 

Forum statistics

Threads
1,223,604
Messages
6,173,315
Members
452,510
Latest member
RCan29

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