Horizontal Lookup using Index Match on Google Sheets - google-sheets

For the following image from Google Sheets, I want to fill out column L with the value found in column A "PPID" based on values in column J "ID" and referencing the range columns B-I. For example, the value "123" should be filled out in column L for the IDs in column J that are 981296581, 682975586, etc. I tried using Index-Match formula, such as =INDEX($A$3, MATCH(J3,B3, 0)), but it only worked for the first row of data and showed an error for the remaining rows
Here is the screenshot of my Google Sheets, as well as the tables below it if you need to copy it into your Google Sheets:
SHEET 1
PPID
ID
ID
ID
ID
ID
ID
ID
ID
123
981296581
682975586
6144233
6140531
6047231
4540543
134
34
456
815220681
1532839
1141531
SHEET 2
ID
INTENDED RESULT
COLUMN TO FILL OUT (with formula)
981296581
123
682975586
123
6144233
123
6140531
123
6047231
123
4540543
123
134
123
34
123
815220681
456
1532839
456
1141531
456
Thanks!

The format isn't really conducive for a lookup. We can FLATTEN the key range and "repeat and flatten" the value range(with IF) to make it conducive for any lookup.
=ARRAYFORMULA(
XLOOKUP(
K2:K12,
FLATTEN(B2:I3),
FLATTEN(IF(COLUMN(B2:I3),A2:A3,))
)
)
ID(K1)
INTENDED RESULT
COLUMN TO FILL OUT (with formula)
981296581
123
123
682975586
123
123
6144233
123
123
6140531
123
123
6047231
123
123
4540543
123
123
134
123
123
34
123
123
815220681
456
456
1532839
456
456
1141531
456
456

try:
=INDEX(IFERROR(VLOOKUP(J3:J, IFERROR(SPLIT(FLATTEN(
IF(B3:I="",,B3:I&"×"&A3:A)), "×")), 2, 0)))

Use this
=ArrayFormula(IF(A3="",,
BYROW(B3:I3, LAMBDA(l, IF(ARRAY_CONSTRAIN(QUERY(
TRANSPOSE(REGEXMATCH(J3&"",l&"")), "Select Col1 where Col1 = TRUE"),1,1)<>TRUE,,A3)))))

Related

How do I sum the cells of one column based on unique duplicate rows of other columns?

I need column D summed wherever column A-C are identical. Column E is what I want the output to look like. I am only using google sheet functions right now and have not learned how to write script. This formula is the closest I've gotten.
=SUM(filter(D:D;COUNTIF(A2:A&B:B&C:C;A2:A&B:B&C:C)>1))
However, it does not distinguish between different text strings only sums any duplicate.
Thanks for any help!
A
B
C
D
E
papaya
10/10/2022
500
42
42
papaya
15/12/2022
550
30
59
papaya
15/12/2022
550
29
59
Pineapple
16/11/2022
400
55
55
Pineapple
09/11/2022
400
63
78
Pineapple
09/11/2022
400
15
78
use:
=QUERY(A:E; "select A,B,C,sum(D) where D is not null group by A,B,C label sum(D)''")
update
use in M2:
=INDEX(LAMBDA(bc; g; i; IFERROR(g/VLOOKUP(bc; QUERY({bc\i*1};
"select Col1,sum(Col2) where Col2 > 0 group by Col1 label sum(Col2)''"); 2; )))
(B2:B&C2:C; G2:G; I2:I))

How to combine two tables with common headers in google sheets?

I am trying to combine two tables with common headers. The formula below only properly works, if I define the exact start and end row. How can I adjust the formula below to dynamically fetch non blank rows from both the tables.
=iferror({A1:C50;ArrayFormula(hlookup(A1:C1,'Settings'!A1:C50,row(A2:A50),0))})
Sample Input:
Table 1
col 1
col2
col 3
abc
123
789
def
456
1212
Table 2
col 1
col2
col 3
abc
123
78849
jkl
256
1298
Desired Output:
Table 3
col 1
col2
col 3
abc
123
789
def
456
1212
abc
123
78849
jkl
256
1298
Use OUERY:
=QUERY({A1:C1;A2:C;E2:G},"WHERE Col1 IS NOT NULL")

Copy over data with concatenating few columns together

In my google sheets I have two sheets/tabs.
In Sheet 1 I have below columns
A
B
C
D
1
Phone#
St Address
City
Buzzer#
2
123
Street 1
Toronto
564
3
456
Street 2
Cambridge
4
523
Street 1
Guelph
5
412
Street 1
Barrie
985
In my sheet 2 I want to copy all these data as below format. I am trying to copy over column A -> Phone# Column as is and concatenate column B | Column C | Column D columns together with ,. Where if There is Buzzer number I want to append word Buzzer end of the city name otherwise leave it blank.
A
B
1
Phone#
Complete Address
2
123
Street 1, Toronto, Buzzer 564
3
456
Street 2, Cambridge
4
523
Street 1, Guelph
5
412
Street 1, Barrie, Buzzer 985
And Here is How I have tried to copy over data from Sheet1 to sheet2 and concatenate columns B,C,D together
=Query('Sheet1'!A:D,"Select A and CONCATENATE((IF(D<>"",(CONCATENATE("Buzzer-"," ",D,", ")),"")),B,", ",C,", ",D) where A is not null",1)
Obviously this didn't work.
Any suggestions or Help please?
I've made a quick sheet with the solution using:
=ARRAYFORMULA(IF(ISBLANK(Sheet1!A2:A12), "",ARRAYFORMULA({Sheet1!A2:A12,ARRAYFORMULA(Sheet1!B2:B12&", "&Sheet1!C2:C12&", "&Sheet1!D2:D12)})))
The sheet:
https://docs.google.com/spreadsheets/d/1E1lBW63CyVJAUmq1okIQgn6-FY82QHt829hpeb0yWxw/edit?usp=sharing

How to get name and top N values from range

I'm trying to create a small leaderboard with columns of names and scores in the following format:
Name 1, Name 2
Value, Value
Value, Value
Value, Value
I would like to grab the N largest values,and pair them with their name like so:
Name 2, Value 1st Largest
Name 1, Value 2nd Largest
Name 2, Value 3rd Largest
I've found that the command LARGE allows you to grab the nth largest value in a selected range, but I can't figure out how to pair that with the name at the top of the column. I suspect I can use Query but I am unsure how I would form that command.
Assuming your Name 1 and Name 2 are in columns A and B like this:
Try this in cell D1:
=arrayformula(query({array_constrain({if(A2:A<>"",A$1&", "&A2:A,),A2:A},MAX(IF(A:B<>"",ROW(A:A))),2);if(B2:B<>"",B$1&", "&B2:B,),B2:B},"select Col1 order by Col2 desc limit 3",0))
Notes
As long as there is a value in A:A, the array {} gets the value in cell A1, then a comma then the value in each row of column A, then a new column with just the value in each row of column A:
{if(A2:A<>"",A$1&", "&A2:A,),A2:A}
Then an ARRAY_CONSTRAIN fixes the result to as many rows as there is data in column A: MAX(IF(A:B<>"",ROW(A:A))), and 2 columns.
As part of the array, ; return the data, then adds if(B2:B<>"",B$1&", "&B2:B,),B2:B which is the value in cell B1, then a comma then the value in each row of column B, then a new column with just the value in each row of column B.
The date therefore looks like this:
Name1, 202 202
Name1, 564 564
Name1, 269 269
Name1, 33 33
Name1, 455 455
Name1, 32 32
Name1, 9870 9870
Name2, 65 65
Name2, 322 322
Name2, 4566 4566
Name2, 422 422
Name2, 45 45
Name2, 5633 5633
A QUERY around this data select Col1 and orders by Col2 descending. The results in the query are limited to 3 records.
The ARRAYFORMULA works down the sheet.
Alternative formula
If you want the results in two columns, then use this:
=arrayformula(query({array_constrain({if(A2:A<>"",A$1,),A2:A},max(counta(A:A),MAX(IF(A:B<>"",ROW(A:B)))),2);if(B2:B<>"",B$1,),B2:B},"order by Col2 desc limit 3",0))

Using ArrayFormula on Query with Multiple and Bounded Criteria

This question is concerning summarizing a database in Google spreadsheet using a predetermined criteria.
I have a data table like so in range A1:B8
A B
-------------
310 890.00
210 875.00
100 849.00
80 845.00
70 842.00
61 842.00
60 841.00
53 825.50
I also have a criteria table which specifies the boundaries for the ranges to be used in the merged table. The criteria table looks like this.
START END
------------
210 310
95 200
69 90
53 65
The criteria table is derived independently from the data table and as you can see, not all the values in the criteria table are present in the data table.
How can I use ArrayFormula such that the final table will have the following data.
START END MAX MIN START VALUE END VALUE
210 310 890 875 875 890
95 200 849 849 849 849
69 90 845 842 842 845
53 65 842 825.5 825.5 842
Here are the starting formulas used to calculate the MAX, MIN, START VALUE, and END VALUE
MAX FORMULA
INDEX(
query(
{ ARRAYFORMULA(VALUE(table1!$A$1:$A$8)),
ARRAYFORMULA(VALUE(table1!$B$1:$B$8)) },
" select max(Col2)
where Col1>="& VALUE(table2!A2) &" and Col1<=" & VALUE(table2!B2) &
" label max(Col2) ''"
),
1,
1
)
MIN FORMULA
INDEX(
query(
{ ARRAYFORMULA(VALUE(table1!$A$1:$A$8)),
ARRAYFORMULA(VALUE(table1!$B$1:$B$8)) },
" select min(Col2)
where Col1>="& VALUE(table2!A2) &" and Col1<=" & VALUE(table2!B2) &
" label min(Col2) ''"
),
1,
1
)
START VALUE FORMULA
INDEX(
query(
{ ARRAYFORMULA(VALUE(table1!$A$1:$A$8)),
ARRAYFORMULA(VALUE(table1!$B$1:$B$8)) },
" select Col2
where Col1>="& VALUE(table2!A2) &" and Col1<=" & VALUE(table2!B2) &
" order by Col1 asc"
),
1,
1
)
END VALUE FORMULA
INDEX(
query(
{ ARRAYFORMULA(VALUE(table1!$A$1:$A$8)),
ARRAYFORMULA(VALUE(table1!$B$1:$B$8)) },
" select Col2
where Col1>="& VALUE(table2!A2) &" and Col1<=" & VALUE(table2!B2) &
" order by Col1 desc"
),
1,
1
)
Here is the link to the publicly editable google sheet with the sample data on it.
How can I use ARRAYFORMULA in Google Sheets such that the details will be autopopulated for a very large dataset.
I have looked at FILTER and VLOOKUP. But since not all the values in the criteria table are actually in the data table, I am having trouble utilizing their ability to work well with ARRAYFORMULA. Please enlighten me. Thanks.
Not exactly what you want but very much easier:
E1 to F9 is named ArStart and used for ColumnC in:
=ArrayFormula(vlookup(A2:A,ArStart,2))
Output is by pivot table.

Resources