FILTER Formula: Simultaneous AND and OR Conditions?

engrnoir

New Member
Joined
Mar 25, 2023
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Is it possible to make my conditions in Filter Formula with both AND and OR (the condition for OR is multiple and dynamic)?
For example, I have a table:
DescriptionYearMonthType
Item 12023MarFruit
Item 22024MarFruit
Item 32025MarFruit
Item 42024MarVegetable
Item 52023JanFruit
Item 62024JanFruit
Item 72025JanFruit
Item 82024JanVegetable

Now in this data sample, I'm trying to use the Filter Formula where I can include items using both AND conditions and OR conditions.
I know how to use Multiple AND conditions [(X="value")*(Y="value2")*...], and OR conditions [(X = "value1")+(Y = "value 2") + ...], I just haven't had the opportunity to use both at the same time.

In this example, let's say I want to filter out all fruits, but I want to filter out all fruits in varying Year and Month (my data set will possibly have same type but will have singular or multiple months/years). My reference for the varying part is a text string of years/months separated by a comma (",") (i.e., 2023,2024,2025...; Jan,Feb,Mar...).
Input Values:
Sample 1:
Year: 2023,2024,2025
Type: Fruit
Month: Jan
Result should be Item 1, Item 2, and Item 3. The OR part comes in at the Year condition, while the AND part comes in at the Month (Mar) and Type (Fruit).

Sample 2:
Year: 2023
Type: Fruit
Month: Jan,Mar

Result should be Item 1, and Item 5. The OR part comes in at the Month (January OR March), while the AND part comes in at the Year (2023) and Type (Fruit).

I apologize in advance if I'm overthinking this or something along those lines, or if I didn't make my question clear. This is the first forum I've ever been in and I already appreciate everything I have seen here before. LOL
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
As in [ISNUMBER(XMATCH(X,values_in_an_array))*...] or [COUNTIF(values_in_a_range,X)*...]?
 
Upvote 0
I'm sure someone can come up with a better way but for Sample 1 this should work.
Sample 2 is just a variation of this.
Excel Formula:
=LET(rYr,$B$2:$B$9,
FILTER($A$2:$D$9,((rYr=2023)+(rYr=2024)+(rYr=2025))*($D$2:$D$9="Fruit")*($C$2:$C$9="Jan"),""))
 
Upvote 0
I don't think your expected result is correct for Sample 1 (you want Jan but items 1, 2 and 3 are Mar) so see if this is what you're looking for.
Book1
ABCD
1DescriptionYearMonthType
2Item 12023MarFruit
3Item 22024MarFruit
4Item 32025MarFruit
5Item 42024MarVegetable
6Item 52023JanFruit
7Item 62024JanFruit
8Item 72025JanFruit
9Item 82024JanVegetable
10
11Item 52023JanFruit
12Item 62024JanFruit
13Item 72025JanFruit
14
15Item 12023MarFruit
16Item 52023JanFruit
17
Sheet1
Cell Formulas
RangeFormula
A11:D13A11=FILTER(A2:D9,((B2:B9=2023)+(B2:B9=2024)+(B2:B9=2025))*(C2:C9="Jan")*(D2:D9="Fruit"),"")
A15:D16A15=FILTER(A2:D9,(B2:B9=2023)*(D2:D9="Fruit")*((C2:C9="Jan")+(C2:C9="Mar")),"")
Dynamic array formulas.
 
Upvote 0
For 365 you could use MAP but that wouldn't work on 2021 which is also in your profile. Does it have to work in both?
 
Upvote 0
Apologies for the late reply. Weekend came and I already was out of office by the time someone replied.

Mr. Alex Blakenburg's approach is kind of what I thought at first (practically similar to kevin9999's approach as well). However, I was actually thinking of recreating this part dynamically instead of a direct input:
...((B2:B9=2023)+(B2:B9=2024)+(B2:B9=2025))... (this one was taken from kevin9999's approach).

That instead of a direct input of 2023, 2024, and 2025, it kind of summarizes that any inputs from my specific text string (i.e., "2023,2024,2025" which will be separated by a comma using TEXTSPLIT, or "2023,2024", "2023,2024,2025,2026", and so on) shall be considered in the condition. The year condition (and month) is going to both be dynamic in this setting.

For Mr. RoryA and Anonymous1378, I have yet to try your insights (I just got back to the office). I probably might take longer with the MAP formula though since I am not yet familiar with that (I have used it before, but I just do not understand it lol). I am currently using Office 365 (I had a 2021 version I think when I created this profile so no issues there).
 
Upvote 0
I am currently using Office 365 (I had a 2021 version I think when I created this profile so no issues there).
So, if you do not have 2021 version now, wouldn't it be a good idea to remove it from your profile (click your user name at the top right of the forum, then ‘Account details’ - don’t forget to scroll down & ‘Save’) so helpers know what resources you do have available?
 
Upvote 0
That instead of a direct input of 2023, 2024, and 2025, it kind of summarizes that any inputs from my specific text string (i.e., "2023,2024,2025" which will be separated by a comma using TEXTSPLIT, or "2023,2024", "2023,2024,2025,2026", and so on) shall be considered in the condition. The year condition (and month) is going to both be dynamic in this setting.
I am currently using Office 365
That leads me to something like this:

20240105 Filter And Or engrnoir.xlsx
ABCDEFGHIJKLM
1DescriptionYearMonthTypeInput ParametersResult
2Item 12023MarFruit
3Item 22024MarFruitYear:2023,2024,2025DescriptionYearMonthType
4Item 32025MarFruitType:Fruit,VegetableItem 12023MarFruit
5Item 42024MarVegetableMonth:Jan,MarItem 22024MarFruit
6Item 52023AprFruitItem 32025MarFruit
7Item 62024JanBreadItem 42024MarVegetable
8Item 72025JanFruitItem 72025JanFruit
9Item 82024JanVegetableItem 82024JanVegetable
10
11
Sheet1 (2)
Cell Formulas
RangeFormula
I4:L9I4=LET( arrYr,BYROW(IF($B$2:$B$9=VALUE(TEXTSPLIT(G3,",")),1,0),LAMBDA(YrMatch,SUM(YrMatch))), arrType,BYROW(IF($D$2:$D$9=TEXTSPLIT(G4,","),1,0),LAMBDA(TypeMatch,SUM(TypeMatch))), arrMth,BYROW(IF($C$2:$C$9=TEXTSPLIT(G5,","),1,0),LAMBDA(MthMatch,SUM(MthMatch))), FILTER($A$2:$D$9,arrYr*arrType*arrMth,""))
Dynamic array formulas.
 
Upvote 0
Solution
Okay, so I just tried Mr. Alex Blakenburg's new approach and this one worked as I intended. This actually helps me more than I intended. Thanks!
 
Upvote 0
FWIW, the MAP equivalent:

Excel Formula:
=FILTER($A$2:$D$9,MAP(B2:B9,C2:C9,D2:D9,LAMBDA(yr,mnth,frt,AND(OR(yr=VALUE(TEXTSPLIT(G3,","))),OR(mnth=TEXTSPLIT(G5,",")),OR(frt=TEXTSPLIT(G4,",")),""))))
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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