Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

google sheets - Filter by array with 2 fields if text contains (filter or query)

I need help as I don't know how to make this formula. I tried multiple variations with FILTER and QUERY functions, but still no success.

I have 2 sheets:

  1. USERS - contains user email, and 2 location columns: city and state

  2. LOCATIONS - contains 2 columns: city and state - it's a list of locations

I need a third sheet that would list all users whose location is listed in LOCATIONS sheet. Each user should be in its own row.

Conditions:

  1. Extracted users must match both city and state columns to those in LOCATIONS sheet, to avoid getting users from multiple locations like Portland, OR, and Portland, TX, when I need just one of them

  2. City column in USERS might have multiple cities separated by ", " inside a single cell if the user is in multiple locations, so city needs to be filtered by "if text contains" condition

Here's a copy of an example sheet: https://docs.google.com/spreadsheets/d/1XruYIMq0nklFInqcGtzN7nd26rXTNnudsZNMI70uG4I/copy


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try:

=ARRAYFORMULA(IFNA(VLOOKUP(E2:E&"?"&F2:F; 
 REGEXREPLACE(TRIM(SPLIT(FLATTEN(QUERY(TRANSPOSE(QUERY(QUERY(TRIM(SPLIT(
 FLATTEN(IF(IFERROR(SPLIT(USERS!B2:B; ","))="";; 
 SPLIT(USERS!B2:B; ",")&"?"&USERS!C2:C&"??"&USERS!A2:A&",?"&USERS!A2:A)); "?")); 
 "select Col1,max(Col2) where Col2 is not null group by Col1 pivot Col3"); 
 "offset 1"; 0));;9^9)); "?")); ",$"; ); 2; 0)))

enter image description here


update:

=ARRAYFORMULA(QUERY({USERS!A:C, TRIM(FLATTEN(QUERY(TRANSPOSE(
 IF(IFERROR(SPLIT(USERS!B:B, ","))="",, 
 SPLIT(USERS!B:B, ",")&"?"&USERS!C:C)),,9^9)))}, 
 "select Col1,Col2,Col3 where Col4 matches '.*"&TEXTJOIN(".*|.*", 1,
 IF(LOCATIONS!A2:A="",,LOCATIONS!A2:A&"?"&LOCATIONS!B2:B))&".*'", 1))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...