AIF

AIF(a,e,[o])
a
array
e
expression criteria argument array
[o]
operation argument, 0 or omitted, multiplication or boolean AND ; 1, adding or boolean OR

Array Include argument for FILTER function, compact, complex calculations with boolean logic for an array of criteria. NEW!! REDUCE

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
832
Office Version
  1. 365
Platform
  1. Windows
AIF Array Include argument for Filter function, NEW!! REDUCE. Returns array of 0 and 1 useful in FILTER function as include argument. Calls AFLAT , CP
Also, as "IF" in the function name suggests, (Array IF), same function will be used in replacing COUNTIFS,SUMIFS,…all …IFS functions complex constructions. For now, will cover what the function can do as Include argument in Filter function.
The goal of the function is to be able to do compact complex boolean logic with arrays of criteria that we can write explicitly or even can live in spreadsheet ranges for dashboard constructions.
a: array
e: expression criteria argument array ( "<=3" ,3, or {">=3",4,"*ab"} )
[o]: operation argument, 0 or omitted, multiplication or boolean AND ; 1, adding or boolean OR

For every criteria found in expression criteria argument array "e", applies the comparison function CP and multiplies or adds the results , depending on "o" argument value.
If "e" array has 3 elements e1,e2,e3, and "o" is 0 or omitted :
AIF(a,e)=CP(a,e1)*CP(a,e2)*CP(a,e3)
If "o"=1 :
AIF(a,e,1)=CP(a,e1)+CP(a,e2)+CP(a,e3)
Excel Formula:
=LAMBDA(a,e,[o],LET(f,AFLAT(e),r,ROWS(f),IF(r=1,CP(a,e),LET(x,REDUCE(0,SEQUENCE(r),LAMBDA(v,i,v+CP(a,INDEX(f,i)))),IF(o,IF(x,1,0),IF(x=r,1,0))))))

CP(a,e) ComParison function. a,e arguments as in AIF

Excel Formula:
=LAMBDA(a,e,LET(x,LEFT(e,2),y,LEFT(e,1),z,SWITCH(x,"<=",x,">=",x,"<>",x,SWITCH(y,"<",y,">",y,"*",y,"!",y,"=")),w,SUBSTITUTE(e,z,"",1), v,IFERROR(--w,w),
    --SWITCH(z,"<>",a<>v,"<=",a<=v,">=",a>=v,"<",a<v,">",a>v,"*",ISNUMBER(SEARCH(v,a)),"!",ISNUMBER(FIND(v,a)),"=",a=v)
    )
)
 
Upvote 0
Will start with CP function examples, then will continue with AIF examples and a brand new ADVFLT Advanced Filter function will follow. Lot of real life examples with tables, arrays, complex criteria.
AIF.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1Introducing CP(a,e), comparison function
2a: array
3e: expression criteria (can include comparison operators, wildcard chars)
4
5What the function does is very basic --(array(operator)values) returning array of 0's and 1's
6The function can split the expression argument into operators and values. If no operator is detected "=" operation occurs
7Note: If one of the arguments, a, or e, is a 2D array, the other one should be a single element, for the result to spill.
8If both "a" and "e" are 1D arrays, the result can spill if they are perpendicular.
9
10List of operators: < , > , <= , >= , <> , * , !
11
12CP Part 1. "a" 2D array,"e" single element
13e,">=0"e,"<=-2"
14a=--(A15:C19>=0)=CP(A15:C19,">=0")a=--(M15:O19<=-2)=CP(M15:O19,"<=-2")
15214111111214000000
16-4-44001001-4-44110110
17-150011011-150000000
180-1-11001000-1-1000000
19-42-2010010-42-2101101
20
21e,"<>-4"e,-4
22a=--(A23:C27<>-4)=CP(A23:C27,"<>-4")a=--(M23:O27=-4)=CP(M23:O27,-4)
23214111111214000000
24-4-44001001-4-44110110
25-150111111-150000000
260-1-11111110-1-1000000
27-42-2011011-42-2100100
28
29CP Part 2. "a" single element, "e" 2D array
30
31ae=CP(A32,C32:E33)
323>=3<=33111
33<>4<3>3100
34
CP post 1
Cell Formulas
RangeFormula
E14,I14,I31,Q22,U22,E22,I22,Q14,U14E14=FORMULATEXT(E15)
E15:G19E15=--(A15:C19>=0)
I15:K19I15=CP(A15:C19,">=0")
Q15:S19Q15=--(M15:O19<=-2)
U15:W19U15=CP(M15:O19,"<=-2")
E23:G27E23=--(A23:C27<>-4)
I23:K27I23=CP(A23:C27,"<>-4")
Q23:S27Q23=--(M23:O27=-4)
U23:W27U23=CP(M23:O27,-4)
I32:K33I32=CP(A32,C32:E33)
Dynamic array formulas.
 
AIF.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1CP Part 3. "a" and "e" 1D arrays, perpendicularae
2ea
3a-3<-3>-3<=-3>=-3<>-3e-7-6-5-4-3-3-2-1012334567
4-7010101-300001100000000000
5-6010101<-311110000000000000
6-5010101>-300000011111111111
7-4010101<=-311111100000000000
8-3100110>=-300001111111111111
9-3100110<>-311110011111111111
10-2001011
11-1001011single cell in J4:=CP(J3:Z3,I4:I9)
120001011
131001011Imp. Note: Even if CP can spill, if "e" has more elements, does not do boolean logic btwn "e" elements,
142001011like AIF does, returns a single result for each element of "e"
153001011
163001011-7-6-5-4-3-3-2-1012334567
174001011-300001100000000000
185001011
196001011I designed CP to spill on 2 directions , (if they are perpendicular) because can be useful.
207001011
21
22single cell in B4:=CP(A4:A20,B3:G3)
23
CP post 2
Cell Formulas
RangeFormula
B4:G20B4=CP(A4:A20,B3:G3)
J4:Z9J4=CP(J3:Z3,I4:I9)
M11M11=FORMULATEXT(J4)
J17:Z17J17=CP(J16:Z16,I17)
E22E22=FORMULATEXT(B4)
Dynamic array formulas.
 
AIF.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1CP Part 4. Handling datesData obtained with =RANDARRAY(10,10,"1-1-18","31-12-21",1)a,a , e,>=1-1-21CP can handle dates if they are in any proper date format.
2a=CP(A3:J12,">=1-1-21")=CP(A3:J12,">=1/Jan/21")a,a, e,>=1/Jan/21
320-04-1902-01-1808-12-1913-04-2026-12-2103-01-2112-02-1826-09-2119-12-1926-09-1800001101000000110100
416-11-1909-01-1902-11-2003-11-1908-05-1808-02-1910-04-1922-09-2023-03-2102-02-1800000000100000000010
512-02-2101-03-2005-11-2121-10-1819-04-1918-09-1924-08-2129-01-2113-02-2130-10-2010100011101010001110
624-11-2120-12-2022-09-1924-12-1828-11-1920-11-2012-06-2102-02-1817-09-1812-04-2010000010001000001000
724-08-1908-06-2122-03-1914-01-2005-11-1915-03-2128-01-2030-10-2021-12-1805-03-2101000100010100010001
803-03-2123-11-2006-11-2109-04-1830-08-2015-03-2025-02-2107-04-2013-02-2125-11-1810100010101010001010
917-09-1830-01-1923-01-1927-07-2003-09-2021-03-1903-01-1920-11-2111-02-2006-02-2100000001010000000101
1006-08-1802-02-2128-08-2030-09-2107-06-2117-01-1808-08-1801-03-1909-01-1824-05-1801011000000101100000
1130-04-2018-11-1911-05-2102-03-1801-06-2124-02-2019-09-1814-10-2015-12-2007-05-1900101000000010100000
1215-10-2007-02-1907-10-1908-03-1813-07-2129-04-2012-09-1805-08-1808-04-1820-03-2000001000000000100000
13"a" argument can handle array calculations, something that range arguments in COUNTIFS or SUMIFS can not.
14a,YEAR(a), e,>=2021check
15a=CP(YEAR(A16:J25),">=2021")=DAY(A29:J38)
1620-04-1902-01-1808-12-1913-04-2026-12-2103-01-2112-02-1826-09-2119-12-1926-09-18000011010020281326312261926
1716-11-1909-01-1902-11-2003-11-1908-05-1808-02-1910-04-1922-09-2023-03-2102-02-18000000001016923881022232
1812-02-2101-03-2005-11-2121-10-1819-04-1918-09-1924-08-2129-01-2113-02-2130-10-201010001110121521191824291330
1924-11-2120-12-2022-09-1924-12-1828-11-1920-11-2012-06-2102-02-1817-09-1812-04-2010000010002420222428201221712
2024-08-1908-06-2122-03-1914-01-2005-11-1915-03-2128-01-2030-10-2021-12-1805-03-21010001000124822145152830215
2103-03-2123-11-2006-11-2109-04-1830-08-2015-03-2025-02-2107-04-2013-02-2125-11-1810100010103236930152571325
2217-09-1830-01-1923-01-1927-07-2003-09-2021-03-1903-01-1920-11-2111-02-2006-02-21000000010117302327321320116
2306-08-1802-02-2128-08-2030-09-2107-06-2117-01-1808-08-1801-03-1909-01-1824-05-18010110000062283071781924
2430-04-2018-11-1911-05-2102-03-1801-06-2124-02-2019-09-1814-10-2015-12-2007-05-19001010000030181121241914157
2515-10-2007-02-1907-10-1908-03-1813-07-2129-04-2012-09-1805-08-1808-04-1820-03-200000100000157781329125820
26
27a,MONTH(a), e,"<=6"a,DAY(a), e,">20"
28a=CP(MONTH(A29:J38),"<=6")=CP(DAY(A29:J38),">20")
2920-04-1902-01-1808-12-1913-04-2026-12-2103-01-2112-02-1826-09-2119-12-1926-09-1811010110000000100101
3016-11-1909-01-1902-11-2003-11-1908-05-1808-02-1910-04-1922-09-2023-03-2102-02-1801001110110000000110
3112-02-2101-03-2005-11-2121-10-1819-04-1918-09-1924-08-2129-01-2113-02-2130-10-2011001001100001001101
3224-11-2120-12-2022-09-1924-12-1828-11-1920-11-2012-06-2102-02-1817-09-1812-04-2000000011011011100000
3324-08-1908-06-2122-03-1914-01-2005-11-1915-03-2128-01-2030-10-2021-12-1805-03-2101110110011010001110
3403-03-2123-11-2006-11-2109-04-1830-08-2015-03-2025-02-2107-04-2013-02-2125-11-1810010111100100101001
3517-09-1830-01-1923-01-1927-07-2003-09-2021-03-1903-01-1920-11-2111-02-2006-02-2101100110110111010000
3606-08-1802-02-2128-08-2030-09-2107-06-2117-01-1808-08-1801-03-1909-01-1824-05-1801001101110011000001
3730-04-2018-11-1911-05-2102-03-1801-06-2124-02-2019-09-1814-10-2015-12-2007-05-1910111100011000010000
3815-10-2007-02-1907-10-1908-03-1813-07-2129-04-2012-09-1805-08-1808-04-1820-03-2001010100110000010000
39Which days are weekends?
40a,WEEKDAY(a,2), e,">=5"check
41a=CP(WEEKDAY(A42:J51,2),">=5")=TEXT(A42:J51,"ddd")
4220-04-1902-01-1808-12-1913-04-2026-12-2103-01-2112-02-1826-09-2119-12-1926-09-181010110100SatTueSunMonSunSunMonSunThuWed
4316-11-1909-01-1902-11-2003-11-1908-05-1808-02-1910-04-1922-09-2023-03-2102-02-181001010001SatWedMonSunTueFriWedTueTueFri
4412-02-2101-03-2005-11-2121-10-1819-04-1918-09-1924-08-2129-01-2113-02-2130-10-201111100111FriSunFriSunFriWedTueFriSatFri
4524-11-2120-12-2022-09-1924-12-1828-11-1920-11-2012-06-2102-02-1817-09-1812-04-200110011101WedSunSunMonThuFriSatFriMonSun
4624-08-1908-06-2122-03-1914-01-2005-11-1915-03-2128-01-2030-10-2021-12-1805-03-211010000111SatTueFriTueTueMonTueFriFriFri
4703-03-2123-11-2006-11-2109-04-1830-08-2015-03-2025-02-2107-04-2013-02-2125-11-180010110011WedMonSatMonSunSunThuTueSatSun
4817-09-1830-01-1923-01-1927-07-2003-09-2021-03-1903-01-1920-11-2111-02-2006-02-210000000101MonWedWedMonThuThuThuSatTueSat
4906-08-1802-02-2128-08-2030-09-2107-06-2117-01-1808-08-1801-03-1909-01-1824-05-180010000100MonTueFriThuMonWedWedFriTueThu
5030-04-2018-11-1911-05-2102-03-1801-06-2124-02-2019-09-1814-10-2015-12-2007-05-190001000000ThuMonTueFriTueMonWedWedTueTue
5115-10-2007-02-1907-10-1908-03-1813-07-2129-04-2012-09-1805-08-1808-04-1820-03-200000000111ThuThuMonThuTueWedWedSunSunFri
52
CP dates
Cell Formulas
RangeFormula
L2,W41,L41,W28,L28,W15,L15,W2L2=FORMULATEXT(L3)
L3:U12L3=CP(A3:J12,">=1-1-21")
W3:AF12W3=CP(A3:J12,">=1/Jan/21")
L16:U25L16=CP(YEAR(A16:J25),">=2021")
W16:AF25W16=DAY(A29:J38)
L29:U38L29=CP(MONTH(A29:J38),"<=6")
W29:AF38W29=CP(DAY(A29:J38),">20")
L42:U51L42=CP(WEEKDAY(A42:J51,2),">=5")
W42:AF51W42=TEXT(A42:J51,"ddd")
Dynamic array formulas.
 
AIF.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1CP Part 5. Strings and wildcard chars behavior, SEARCH/FIND functionality,* triggers SEARCH, "!" triggers FIND, use of tilde (~) char
2
3=CP(A4:A8,"a")=--(A4:A8="a")=CP(A4:A8,"A")=--(A4:A8="A")
4a1111
5Ab0000
6abc0000
7Abcd0000
8A1111
9
10=CP(A11:A15,"*a")=SEARCH("a",A11:A15)=CP(A11:A15,"*A")=SEARCH("A",A11:A15)
11a1111
12Ab1111
13abc1111
14Abcd1111
15A1111
16
17=CP(A18:A22,"!a")=--ISNUMBER(FIND("a",A18:A22))=CP(A18:A22,"!A")=--ISNUMBER(FIND("A",A18:A22))
18a1100
19Ab0011
20abc1100
21Abcd0011
22A0011
23
24=CP(A25:A35,"*ab?")=--ISNUMBER(SEARCH("ab?",A25:A35))=CP(A25:A35,"!AB?")=--(ISNUMBER(FIND("AB?",A25:A35)))
25ab0000
26Ab0000
27AB0000
28aB0000
29abc1100
30aBc1100
31ABc1100
32abcd1100
33Abcd1100
34aBcd1100
35ABcd1100
36
37Imp note: FIND does not work with wildcard chars
38
39=CP(A40:A47,"*?ab")=--ISNUMBER(SEARCH("?ab",A40:A47))=CP(A40:A47,"*?ab?")=--ISNUMBER(SEARCH("?ab?",A40:A47))
40ab0000
41cab1100
42Cab1100
43cAbC1111
44CaBc1111
45CABC1111
46AB0000
47aB0000
48
49=CP(A50:A54,"*~*a")=--ISNUMBER(SEARCH("~*a",A50:A54))=CP(A50:A54,"*~?a")=--ISNUMBER(SEARCH("~?a",A50:A54))
50a0000
51*a*1100
52*a1100
53?a0011
54?a?0011
55
56=CP(A57:A61,"*~*a?")=--ISNUMBER(SEARCH("~*a?",A57:A61))=CP(A57:A61,"*~?a?")=--ISNUMBER(SEARCH("~?a?",A57:A61))
57a0000
58*a*1100
59*a0000
60?a0000
61?a?0011
62
63=CP(A64:A70,"*~*a~*")=--ISNUMBER(SEARCH("~*a~*",A64:A70))=CP(A64:A70,"*~?a~?")=--ISNUMBER(SEARCH("~?a~?",A64:A70))
64a0000
65*a*1100
66*a0000
67*ab0000
68?a0000
69?a?0011
70?ab0000
71
CP wild cards
Cell Formulas
RangeFormula
C3,G3,C63,H63,P63,U63,C56,H56,P56,U56,C49,H49,P49,U49,C39,H39,P39,U39,C24,H24,P24,U24,C17,G17,N17,R17,C10,G10,L10,P10,K3,O3C3=FORMULATEXT(C4)
C4:C8C4=CP(A4:A8,"a")
G4:G8G4=--(A4:A8="a")
K4:K8K4=CP(A4:A8,"A")
O4:O8O4=--(A4:A8="A")
C11:C15C11=CP(A11:A15,"*a")
G11:G15G11=SEARCH("a",A11:A15)
L11:L15L11=CP(A11:A15,"*A")
P11:P15P11=SEARCH("A",A11:A15)
C18:C22C18=CP(A18:A22,"!a")
G18:G22G18=--ISNUMBER(FIND("a",A18:A22))
N18:N22N18=CP(A18:A22,"!A")
R18:R22R18=--ISNUMBER(FIND("A",A18:A22))
C25:C35C25=CP(A25:A35,"*ab?")
H25:H35H25=--ISNUMBER(SEARCH("ab?",A25:A35))
P25:P35P25=CP(A25:A35,"!AB?")
U25:U35U25=--(ISNUMBER(FIND("AB?",A25:A35)))
C40:C47C40=CP(A40:A47,"*?ab")
H40:H47H40=--ISNUMBER(SEARCH("?ab",A40:A47))
P40:P47P40=CP(A40:A47,"*?ab?")
U40:U47U40=--ISNUMBER(SEARCH("?ab?",A40:A47))
C50:C54C50=CP(A50:A54,"*~*a")
H50:H54H50=--ISNUMBER(SEARCH("~*a",A50:A54))
P50:P54P50=CP(A50:A54,"*~?a")
U50:U54U50=--ISNUMBER(SEARCH("~?a",A50:A54))
C57:C61C57=CP(A57:A61,"*~*a?")
H57:H61H57=--ISNUMBER(SEARCH("~*a?",A57:A61))
P57:P61P57=CP(A57:A61,"*~?a?")
U57:U61U57=--ISNUMBER(SEARCH("~?a?",A57:A61))
C64:C70C64=CP(A64:A70,"*~*a~*")
H64:H70H64=--ISNUMBER(SEARCH("~*a~*",A64:A70))
P64:P70P64=CP(A64:A70,"*~?a~?")
U64:U70U64=--ISNUMBER(SEARCH("~?a~?",A64:A70))
Dynamic array formulas.
 
AIF.xlsx
ABCDEFGHIJKLMNOPQ
1AIF. Concept
2
3If we have an array and 2 criteria in boolean OR (adding) relation, will get a possible array of 0,1, or 2's
42's - when both conditions are true (TRUE,TRUE)
51's - when only one of the two condition is true (TRUE,FALSE or FALSE,TRUE)
60's - if none of the conditions is true. (FALSE, FALSE)
7To change the criteria relation into boolean AND (multiplication) we can use the same OR calculation with an extra condition.
8When the result delivered with criteria in OR relation, is equal with total nr of criteria (2 in our case), means that will get the result for criteria like if they were in AND relation.
9That's why AIF uses only one REDUCE lambda helper function, in OR relation for both scenarios:
10
11….x,REDUCE(0,SEQUENCE(r),LAMBDA(v,i,v+CP(a,INDEX(f,i))))…...
12
13criteria 1>=-2AIF(a,e,[o])
14criteria 2<=21st criteria2nd criteriaOR relationAND relationchecko,omitted => AND relation
15ae=A16:A22>=-2=A16:A22<=2=E16#+G16#=IF(I16#=2,1,0)=E16#*G16#=AIF(A16:A22,C13:C14)
16-3FALSETRUE1000
17-2TRUETRUE2111
18-1TRUETRUE2111
190TRUETRUE2111
201TRUETRUE2111
212TRUETRUE2111
223TRUEFALSE1000
23
24=CP(A16:A22,C13)+CP(A16:A22,C14)=CP(A16:A22,C13)*CP(A16:A22,C14)
2510
2621
2721
2821
2921
3021
3110
32
Sheet4
Cell Formulas
RangeFormula
E15,G15,I15,K15,M15,O15,I24,M24E15=FORMULATEXT(E16)
E16:E22E16=A16:A22>=-2
G16:G22G16=A16:A22<=2
I16:I22I16=E16#+G16#
K16:K22K16=IF(I16#=2,1,0)
M16:M22M16=E16#*G16#
O16:O22O16=AIF(A16:A22,C13:C14)
I25:I31I25=CP(A16:A22,C13)+CP(A16:A22,C14)
M25:M31M25=CP(A16:A22,C13)*CP(A16:A22,C14)
Dynamic array formulas.
 
AIF.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1AIF. The function
2 =LAMBDA(a,e,[o],LET(f,AFLAT(e),r,ROWS(f),IF(r=1,CP(a,e),LET(x,REDUCE(0,SEQUENCE(r),LAMBDA(v,i,v+CP(a,INDEX(f,i)))),IF(o,IF(x,1,0),IF(x=r,1,0))))))
3
4…f,AFLAT(e)…Chose to use AFLAT for the "e" expression criteria argument array , because can flatten any array, constant explicit array, of any shape or dimension, with or without blanks.
5
6criteria OR relationo,1
7ae=AIF(B8#,I8:K11,1)
8-18-17-16-15-14-13<=-16-9-5111000
9-12-11-10-9-8-705000100
10-6-5-4-3-2-1>14-2010010
1101234573100101
1267891011010000
13121314151617000111
14
15...r,ROWS(f)….r, rows nr. represents nr. of criteria we have to deal with
16
17...IF(r=1,CP(a,e),LET(…If we deal only with 1 criteria (r=1) function exits with calculation of CP(a,e), no need to get into REDUCE and last part calculations
18
19...LET(x,REDUCE(0,SEQUENCE(r),LAMBDA(v,i,v+CP(a,INDEX(f,i))))….
20REDUCE part, covered in previous post
21
22….IF(o,IF(x,1,0),IF(x=r,1,0))…..last part, based on "o" value, keeps the result of REDUCE, that delivers OR relation if o=1, or changes it into an AND relation , if o=0 or omitted
23o, operation argument
24x, what REDUCE returns (OR relation only)
25r, nr.criteria
261. if o<>0 (1) (OR operation) => IF(x,1,0) since x (REDUCE) does only OR relation (adding), x can be an array with 0's 1's, 2's...etc. This parts converts positive nrs. into 1's
272. if o=0 (AND operation) => IF(x=r,1,0) this part converts OR relation to AND relation, only values=nr. criteria are changed into 1's, rest of values will be changed into 0's
28
29Task: Filter sample for strings than contain letter"a" or letter "b"Task: Filter for strings that have both letters "a" and "b"
30
31a=CP(A32:A39,"*a")+CP(A32:A39,"*b")=AIF(A32:A39,{"*a","*b"},1)=FILTER(A32:A39,J32#)a=AIF(A32:A39,{"*a","*b"})
32c00axc0
33ax11abax0
34ab21aBab1=FILTER(A32:A39,W32#)
35aB21baaB1ab
36ba21bxba1aB
37bx11xbbx0ba
38xb11xb0
39d00d0
40
41Note: As we see above AIF converts 1's and 2's into 1's
42FILTER include argument cares only for positive or negative nr.
43
AIF post 2
Cell Formulas
RangeFormula
M7,Z34,W31,C31,J31,O31M7=FORMULATEXT(M8)
B8:G13B8=SEQUENCE(6,6,-18)
M8:R13M8=AIF(B8#,I8:K11,1)
C32:C39C32=CP(A32:A39,"*a")+CP(A32:A39,"*b")
J32:J39J32=AIF(A32:A39,{"*a","*b"},1)
O32:O37O32=FILTER(A32:A39,J32#)
W32:W39W32=AIF(A32:A39,{"*a","*b"})
Z35:Z37Z35=FILTER(A32:A39,W32#)
Dynamic array formulas.
 
AIF.xlsx
ABCDEFGHIJKLMNOPQRSTUV
1CP. ComParison function
2 =LAMBDA(a,e,LET(x,LEFT(e,2),y,LEFT(e,1),z,SWITCH(x,"<=",x,">=",x,"<>",x,SWITCH(y,"<",y,">",y,"*",y,"!",y,"=")),w,SUBSTITUTE(e,z,"",1), v,IFERROR(--w,w),--SWITCH(z,"<>",a<>v,"<=",a<=v,">=",a>=v,"<",a<v,">",a>v,"*",ISNUMBER(SEARCH(v,a)),"!",ISNUMBER(FIND(v,a)),"=",a=v)))
3
41.-first part….x,LEFT(e,2),y,LEFT(e,1),z,SWITCH(x,"<=",x,">=",x,"<>",x,SWITCH(y,"<",y,">",y,"*",y,"!",y,"=")),w,SUBSTITUTE(e,z,"",1)….
5This part is responsible for splitting all elements of "e" in an array of operators(z) and an array of values (w)
6Has 2 LEFT (x,y), a nested SWITCH(…SWITCH(...)) (z) and a SUBSTITUTE (w) functions
7Comparison operators can have double chars ( <=, >=, <>) single chars (<, >) and the "silent" "="
8Plus our customs ones "*" to trigger SEARCH, and "!" to trigger FIND
9First part of SWITCH checks if first 2 chars of "e" (x,LEFT(e,2) ) are any of double ones. If yes, exits with "x" value, (z=x)
10if not,2nd SWITCH, checks if first char of "e", (y,LEFT(1) ) is any of single op. or custom ones, if yes, exits with "y" value (z=y)
11if not (none of double or single operators are found) z will return "=" (z="=")
12Note: I could have used for "z" :...z,IF(OR(x="<=",x=">=",x="<>"),x,IF(OR(y="<",y=">",y="*",y="!"),y,"="))
13Looks that does same thing, but the problem is, OR delivers only a single result, can not spill, and I wanted z to be able to spill. SWITCH spills.
14Now, "z" is created, we need the "values" array (w) , these will be whatever is left of "e" if we remove the operators array.
15…w,SUBSTITUTE(e,z,"",1)…Here, the final 1 is important, we substitute with "" only the First instance of "z" found in "e"
16
17"e" example:=SUBSTITUTE(A19:A25,O19:O25,"",1)
18exy1st SWITCH2nd SWITCHexit nested SWITCHzw
19>=-3>=>>=>=-3
20abcdaba==abcd
21<25<2<<<25
22<>100<><<><>100
2389898==89
24**ab******ab(only 1st instance "removed")
25!!abc!!!!!!abc(only 1st instance "removed")
26operatorsvalues(bcs of SUBSTITUTE,all in text format)
27
282. middle part …v,IFERROR(--w,w)…"w", delivers text, "v" changes text numeric values into numeric values, including dates in proper text format
29
303. last part..--SWITCH(z,"<>",a<>v,"<=",a<=v,">=",a>=v,"<",a<v,">",a>v,"*",ISNUMBER(SEARCH(v,a)),"!",ISNUMBER(FIND(v,a)),"=",a=v)
31Nothing to say here, very straight forward, another SWITCH that triggers the corresponded operation for each operator
32
CP function
Cell Formulas
RangeFormula
Q17Q17=FORMULATEXT(Q19)
C19:C25C19=LEFT(A19:A25,2)
E19:E25E19=LEFT(A19:A25,1)
Q19:Q25Q19=SUBSTITUTE(A19:A25,O19:O25,"",1)
Dynamic array formulas.
 
AIF.xlsx
ABCDEFGHIJKLMNOPQRST
1Examples 1. AIF with comparison operators <,>,<=,>=,<>,=
2kind of 2D filter
3aAND relation=AIF(A4#,G4:G7)=IF(I4#,A4#,"")
412345>=7e00000 
5678910<=1401110789
61112131415<>1001110121314
71617181920<>1100000
8
9aOR relation=AIF(A10#,G10:G15,1)=IF(I10#,A10#,"")
1012345<=3e111011235
11678910>=18000109
12111213141550100012
131617181920161011116181920
149
1512
16OR(e1)+AND(e2)
17aOR=AIF(A18:E21,G18:G19,1)+AIF(A18:E21,G22:G23)
1812345<3e11100012
19678910>18001118910
20111213141511100111213
211617181920AND000111920
22>7e2=IF(I18#,A18:E21,"")
23<14
24
25Note: regarding comparison operator "="
26AIF was designed to trigger "=" operation whenever none of the other operators are found
27Also, it will not be a problem if the "=" operator is written explicitly
28
29aOR relation=AIF(A30#,G30:G35,1)=IF(I30#,A30#,"")
3012345<=3e111011235
31678910>=18000109
32111213141550100012
331617181920=161011116181920
349
35=12
36
Examples 1
Cell Formulas
RangeFormula
I3,O29,I29,I17,O9,I9,O3I3=FORMULATEXT(I4)
A4:E7,A30:E33,A10:E13A4=SEQUENCE(4,5)
I4:M7I4=AIF(A4#,G4:G7)
O4:S7,O30:S33,O10:S13O4=IF(I4#,A4#,"")
I10:M13,I30:M33I10=AIF(A10#,G10:G15,1)
I18:M21I18=AIF(A18:E21,G18:G19,1)+AIF(A18:E21,G22:G23)
O18:S21O18=IF(I18#,A18:E21,"")
O22O22=FORMULATEXT(O18)
Dynamic array formulas.
 
AIF.xlsx
ABCDEFGHIJKLMNOPQR
1Filter by a List of itemsData, courtesy of Mike Girvin, ExcelIsFun
2Desired Records:=AREPORT(I4#,,Dp[#Headers])
3DepOrderNoCatAmountCat=FILTER(Dp,AIF(Dp[Cat],F4:F11,1))DepOrderNoCatAmount
4Legal65256611$1,265.232101Finance65497121$4,874.14Finance65497121$4,874.14
5Operations65266571$3,868.734301Operations65724301$4,417.44Operations65724301$4,417.44
6Finance65272111$2,015.307001Maintenance65732101$3,719.51Maintenance65732101$3,719.51
7Human Resources65283211$4,283.203251Maintenance65766801$2,619.46Maintenance65766801$2,619.46
8Legal65291021$2,949.477061Human Resources65912101$206.19Human Resources65912101$206.19
9Finance65306571$4,496.667121Human Resources66016801$190.64Human Resources66016801$190.64
10Human Resources65312131$2,238.964271Human Resources66254301$393.94Human Resources66254301$393.94
11Human Resources65326891$1,814.546801Accounting66637061$462.54Accounting66637061$462.54
12Finance65336481$3,849.74
13Human Resources65346841$3,342.17=AREPORT(I15#,,Dp[#Headers])
14Accounting65357051$3,509.01DepAmount=FILTER(Dp,AIF(Dp[Dep],F15:F17,1)*AIF(Dp[Amount],G15:G16))DepOrderNoCatAmount
15Operations65366991$3,480.03Legal>=1000Legal65256611$1,265.23Legal65256611$1,265.23
16Maintenance65376551$3,617.99Finance<=2000Human Resources65326891$1,814.54Human Resources65326891$1,814.54
17Legal65383241$349.00Human ResourcesHuman Resources65476561$1,224.23Human Resources65476561$1,224.23
18Human Resources65396521$141.95Legal65636691$1,089.11Legal65636691$1,089.11
19Maintenance65406601$4,230.50Finance65706581$1,537.84Finance65706581$1,537.84
20Finance65416591$4,985.17Human Resources65944361$1,582.50Human Resources65944361$1,582.50
21Human Resources65426501$4,585.62other functionsLegal66066861$1,390.73Legal66066861$1,390.73
22Finance65436971$4,405.14AREPORTHuman Resources66175391$1,734.93Human Resources66175391$1,734.93
23Operations65444321$653.37Human Resources66416501$1,809.08Human Resources66416501$1,809.08
24Maintenance65456611$4,138.50Finance66436891$1,199.69Finance66436891$1,199.69
25Accounting65466581$2,422.38Human Resources66586521$1,773.56Human Resources66586521$1,773.56
26Human Resources65476561$1,224.23Legal66706811$1,791.55Legal66706811$1,791.55
27Maintenance65486941$4,259.00Human Resources66716871$1,268.30Human Resources66716871$1,268.30
28Finance65497121$4,874.14Finance66722111$1,817.58Finance66722111$1,817.58
29Accounting65507211$759.52Legal66732131$1,143.45Legal66732131$1,143.45
30Maintenance65513211$4,147.95
31Human Resources65522121$2,709.42Using AIF to filter horizontaly
32Maintenance65536701$3,181.79=AIF(Dp[#Headers],F14:G14,1)
33Accounting65546881$2,540.321001
34Finance65553201$2,503.21
35Human Resources65567071$3,315.10=FILTER(N14#,AIF(Dp[#Headers],F14:G14,1))
36Maintenance65573191$978.88DepAmount
37Accounting65585381$1,608.27Legal$1,265.23
38Human Resources65594321$2,634.92Human Resources$1,814.54
39Finance65606951$245.76Human Resources$1,224.23
40Maintenance65616661$934.88Legal$1,089.11
41Accounting65626671$818.15Finance$1,537.84
42Legal65636691$1,089.11Human Resources$1,582.50
43Accounting65647181$3,077.98Legal$1,390.73
44Human Resources65657101$4,885.00Human Resources$1,734.93
45Maintenance65666481$1,279.55Human Resources$1,809.08
46Operations65675391$1,982.58Finance$1,199.69
47Accounting65684261$4,998.69Human Resources$1,773.56
48Operations65697101$1,695.86Legal$1,791.55
49Finance65706581$1,537.84Human Resources$1,268.30
50Accounting65717141$3,156.93Finance$1,817.58
51Operations65724301$4,417.44Legal$1,143.45
52Maintenance65732101$3,719.51
53Finance65744351$3,590.71
54Maintenance65757151$3,140.17
55Maintenance65766801$2,619.46
56Accounting65776671$2,246.14
57Operations65783231$4,029.57
58Accounting65793201$1,321.97
59Operations65806751$3,026.18
60Operations65816601$3,419.71
61Legal65826911$776.53
62Operations65836681$1,768.00
63Maintenance65846701$1,148.60
64Operations65856771$265.40
65Legal65865411$367.18
66Operations65876561$1,061.33
67Human Resources65886821$4,500.55
68Accounting65891001$942.39
69Maintenance65906731$4,476.57
70Human Resources65912101$206.19
71Accounting65926951$326.85
72Maintenance65934281$4,172.97
73Human Resources65944361$1,582.50
74Operations65954361$3,929.85
75Human Resources65963171$955.69
76Operations65977031$777.09
77Maintenance65986501$2,904.57
78Finance65993221$788.51
79Finance66006551$434.96
80Human Resources66016801$190.64
81Legal66026641$3,734.56
82Maintenance66033221$937.29
83Maintenance66046981$2,517.09
84Finance66057091$2,573.43
85Legal66066861$1,390.73
86Operations66072131$569.00
87Operations66082121$919.67
88Maintenance66096941$2,940.34
89Legal66106621$2,220.63
90Finance66117051$903.36
91Operations66126551$1,243.96
92Legal66136791$2,084.00
93Finance66143181$516.86
94Human Resources66157161$3,063.06
95Human Resources66165431$2,770.24
96Human Resources66175391$1,734.93
97Operations66186661$4,755.49
98Finance66194281$3,207.31
99Finance66206951$638.38
100Maintenance66212131$4,676.85
101Maintenance66226491$1,696.54
102Accounting66236971$3,744.77
103Operations66246761$3,805.21
104Human Resources66254301$393.94
105Accounting66262111$502.53
106Accounting66271021$2,764.06
107Operations66285421$4,276.44
108Maintenance66295391$438.72
109Maintenance66307181$2,948.21
110Operations66316721$369.69
111Legal66323231$417.73
112Maintenance66337071$4,537.48
113Maintenance66344351$1,646.13
114Finance66357211$4,705.00
115Finance66366621$4,531.84
116Maintenance66377211$3,560.45
117Legal66385381$584.22
118Maintenance66396751$429.12
119Accounting66406691$102.79
120Human Resources66416501$1,809.08
121Maintenance66427131$4,066.42
122Finance66436891$1,199.69
123Legal66446461$3,956.52
124Maintenance66457071$3,052.45
125Accounting66465371$202.18
126Accounting66476871$3,952.80
127Accounting66484311$4,192.83
128Accounting66496871$2,772.62
129Human Resources66507051$965.17
130Human Resources66512131$3,270.53
131Maintenance66527011$1,932.48
132Finance66536841$547.82
133Finance66546991$2,185.79
134Operations66555381$1,476.24
135Human Resources66567171$2,851.43
136Operations66573211$2,788.30
137Human Resources66586521$1,773.56
138Maintenance66597111$2,161.50
139Finance66607161$2,979.32
140Legal66617111$2,377.74
141Human Resources66622121$731.41
142Accounting66637061$462.54
143Maintenance66646841$1,713.13
144Human Resources66656711$552.88
145Operations66666601$1,282.53
146Maintenance66676451$3,444.59
147Finance66686991$526.92
148Operations66694291$4,239.64
149Legal66706811$1,791.55
150Human Resources66716871$1,268.30
151Finance66722111$1,817.58
152Legal66732131$1,143.45
153Operations66742121$4,278.78
154Accounting66756571$1,104.68
155Legal66763151$4,089.90
156Operations66776631$250.38
157Operations66786771$221.97
158Legal66796691$3,588.85
159Finance66803191$3,396.70
160
Examples 2
Cell Formulas
RangeFormula
N2,I35,I32,I14,N13,I3N2=FORMULATEXT(N3)
N3:Q11,N14:Q29N3=AREPORT(I4#,,Dp[#Headers])
I4:L11I4=FILTER(Dp,AIF(Dp[Cat],F4:F11,1))
I15:L29I15=FILTER(Dp,AIF(Dp[Dep],F15:F17,1)*AIF(Dp[Amount],G15:G16))
I33:L33I33=AIF(Dp[#Headers],F14:G14,1)
I36:J51I36=FILTER(N14#,AIF(Dp[#Headers],F14:G14,1))
Dynamic array formulas.
 

Forum statistics

Threads
1,215,459
Messages
6,124,946
Members
449,198
Latest member
MhammadishaqKhan

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