Selecting few columns in Google Sheet's QUERY function - google-sheets

I am trying to select few columns in Google Sheet's QUERY function but get errors when I combine with other formula in the function.
Here is my formula. What I am trying to do? my goal is combine data (column) from different sheets that will ultimately feed into a pivot table
=QUERY({TeamData!C:C,TeamBonusData!F:F;IndividualData!M:O,IndividualBonusData!P:R},) - this does not work
=QUERY({TeamData!C:C,TeamData!F:F},) - this works.

Follow the advice Tedinoz gave and ensure that those references have a matching number of rows and columns. It may help if you visually group ranges so that they stack side-by-side or on top of each other, like this:
=lambda(
teams, teamsBonus, individuals, individualsBonus,
lambda(
numTeams, numIndividuals,
{
array_constrain(teams, numTeams, 2), array_constrain(teamsBonus, numTeams, 2);
array_constrain(individuals, numIndividuals, 2), array_constrain(individualsBonus, numIndividuals, 2)
}
)(
min(rows(teams), rows(teamsBonus)),
min(rows(individuals), rows(individualsBonus))
)
)(
TeamData!C2:D, TeamBonusData!F2:G, IndividualData!M2:O, IndividualBonusData!P2:R
)

Related

Importing values of column from other sheet based on a matching values in a different column across both sheets?

Hey Stack Overflow Sheets, I have a question regarding a use case we want to create in our Google Sheet, either with built-in functionality, or with an extension like Sheetgo.
We have 2 sheets with multiple of the same columns (properties) and rows (users). The problem is that only one of the sheets is a trusted source for one column’s data (“source sheet”), and the other sheet has empty or outdated values for the same column (“outdated sheet”), and we need both sheets to have this column match values in rows that have a matching value for another column across both sheets (in our case, an “email” column). After they’re matching, we want to change the formula to sync any changes made for that column between both sheets.
Here’s an obfuscated data example:
Source sheet:
https://docs.google.com/spreadsheets/d/1uxqC3lB15UHhKTzjZyzzVIj5tlPjhCCCZ48xHYEcm0o/edit?usp=sharing
Outdated sheet:
https://docs.google.com/spreadsheets/d/1ckoCh8gMwt2QeBRH1dB2dyFPJUukrjQ-SCgucTL8rhc/edit?usp=sharing
In the example, we’re looking for a formula that would allow us to have a “Type” column value injected into the Outdated Sheet’s Type column, based on both sheet’s matching Email column value. And then, have it so if a row’s “Type” value changes in either doc, the other doc follows.
What formula or extension would I use to go about this? Any help appreciated, thanks!
I tried to create a VLOOKUP and MATCH formula, but I couldn't yet figure out how to have the function first LOOKUP into the Source Sheet, then inject it into the Outdated Sheet based on a matched email column value. Sheetgo made the LOOKUP easier, but I still couldn't figure out how to do an exact operation.
Use importrange() and vlookup(). Put this formula in cell A1 of the target spreadsheet:
=arrayformula(
lambda(
import,
iferror(
vlookup(
C1:C,
{ index(import, 0, 3), index(import, 0, 1) },
2, false
)
)
)(
importrange("1uxqC3lB15UHhKTzjZyzzVIj5tlPjhCCCZ48xHYEcm0o", "Sheet1!A1:F")
)
)

Display COLUMN number for TRUE in rank preference list in Google Sheets

I would like the option to toggle the display of compiled rank order responses in a grid between "response indicated" and "response indicated by rank number". I'm hoping for a formula solution rather than using Apps Script.
The included screenshot displays the current and desired outcome—with current as the example for "response indicated" (link to sheet below).
The grid pulls from responses on a separate sheet as shown in the included screenshot.
NOTE:
While the choice order on the input sheet is ordered 1-5, the display grid does not list the options in sort order.
There is a sort checkbox on map!C2 that informs whether the display grid uses the same order as input or sorts alphabetically.
The display grid uses separate formulas (B4 & C4—that do not reference one another) to populate.
The closest I've been able to generate is the first row toggling to the accurate ranking (using various combinations of QUERY, VLOOKUP, INDEX, MATCH, COLUMN)—but unable to generate an array output so that each row can create a specific response.
current C1 formula (uses named ranges):
=ArrayFormula(
IF(
REGEXMATCH(
IF(autoSort,
TRANSPOSE(QUERY(
TRANSPOSE(QUERY(
QUERY(inputTable,"where A is not null order by A",0),
"select "&ARRAYFORMULA(JOIN(",","Col"&SEQUENCE(1,5,2))),)),,
COLUMNS(QUERY(
QUERY(inputTable,"where A is not null order by A",0),
"select "&ARRAYFORMULA(JOIN(",","Col"&SEQUENCE(1,5,2))),)))),
TRANSPOSE(QUERY(
TRANSPOSE(QUERY(
QUERY(inputTable,"where A is not null",0),
"select "&ARRAYFORMULA(JOIN(",","Col"&SEQUENCE(1,5,2))),)),,
COLUMNS(QUERY(
QUERY(inputTable,"where A is not null",0),
"select "&ARRAYFORMULA(JOIN(",","Col"&SEQUENCE(1,5,2))),))))),
autoOptions),
CHAR(10003),))
link to example sheet: https://docs.google.com/spreadsheets/d/1eRaRf-0n-VQ2zljqUpk38sMrFlCh19JAcv7IFMeFMw0/edit?usp=sharing
just a MattKing's mod with a checkmark switch:
=INDEX(IFNA(VLOOKUP(B4:B&C3:K3,
SPLIT(FLATTEN(input!A2:A&input!B2:F&"|"&COLUMN(input!B1:F1)-1&"|"&CHAR(10003)), "|"),
IF(C1=TRUE, 3, 2), )))
This should do it:
=ARRAYFORMULA(IFERROR(VLOOKUP(B4:B&C3:K3,SPLIT(FLATTEN(input!A2:A&input!B2:F&"|"&COLUMN(input!B1:F1)-1),"|",0,0),2,0)))

Google Sheets Query Search Box | Search criteria

so basically I have a searchbox in my sheet that searches and pulls up data. For a reference see this image: https://i.imgur.com/MVTUCSw.png. So basically in cell A4 I put the data that I am looking for, but my formula restricts me to only looking up stuff in 1 row. For example, data starting with the word MELD, but I would like to be able to also look up data based on for example the someone their name.
The formula I use for the searchbox: =QUERY({'Pallets & Locaties'!A2:G;Voorraadverschillen!A2:G}, "SELECT * WHERE Col1 "&Opzoeken!B4&" '"&A4&"'")
The data that I want to be able to look up is stored in 2 sheets: Pallets & Locaties - https://i.imgur.com/qV7h2tz.png and in Voorraadverschillen - https://i.imgur.com/foqLkKa.png.
The searchbox is only able to lookup data in row, but I just want to be able to search for any kind of stored data in any of the sheets.
Here is my sheet for reference: https://docs.google.com/spreadsheets/d/10wmnxV16JUiD_b_54abkiLPwTqLITWRcUw3gsqvXoBE/edit?usp=sharing
I'd recommend you add more rows for the lookup criteria and add a column for what column it would search for.
Sheet modification:
Formula:
=QUERY({'Pallets & Locaties'!A2:G;Voorraadverschillen!A2:G}, "SELECT * WHERE "&TEXTJOIN(" AND ", TRUE, ARRAYFORMULA(IF(ISBLANK(A4:A10), "", A4:A10&" "&B4:B10&" '"&C4:C10&"'"))))
Test Sheet
Note:
The above formula will allow you to search on other columns with their own words and criteria to search.
Only rows with Kolom values will be included in the criteria. If you only need Col1 criteria, make sure to leave other rows blank.
This does use an AND search, meaning all of the criteria should be true and match the row. Feel free to use OR in the TEXTJOIN function if you only want to search all rows matching any of the criteria.
This will only search on sheets Pallets & Locaties and Voorraadverschillen. Add the necessary sheets if you need them.
EDIT:
Cleaned up the formula to not be so repetitive.
=IF(A4<>"",(QUERY({'Pallets & Locaties'!A2:G;Voorraadverschillen!A2:G},"Select * WHERE "&textjoin(" OR ", true, arrayformula("Col"&ROW(1:7)&" "&B4&" '"&A4&"'")))),(QUERY({'Pallets & Locaties'!A2:G;Voorraadverschillen!A2:G},"Select * WHERE Col1 IS NOT NULL")))
This searches every column for the data, as long as data is not identical in two columns you won't have issues. An example would be the search criteria "MELD" being in both Column A and B. If that were the case, only the results from the first matching column would populate.

Add 2 sparlines in a google sheet to create a new sparkline

I have 2 sparklines. Both are created based on historical share prices of stocks from a year ago to today.
I want to create a new sparkline that is the sum of these 2 charts. Is this possible given that the have the same number of data points i.e 365.
e.g
=SPARKLINE(GOOGLEFINANCE("AMZN","price",TODAY()-365,TODAY(),"daily"),{"charttype","line";"linewidth",1;"color","#5f88cc"})
=SPARKLINE(GOOGLEFINANCE("MSFT","price",TODAY()-365,TODAY(),"daily"),{"charttype","line";"linewidth",1;"color","#5f88cc"})
You can join them as a single array and then use query to add them:
=SPARKLINE(
QUERY(
{
GOOGLEFINANCE("AMZN","price",TODAY()-365,TODAY(),"daily"),
GOOGLEFINANCE("MSFT","price",TODAY()-365,TODAY(),"daily")
},
"select Col1, Col2+Col4",
1
)
)
Rundown
We first join the two arrays. To do so we use the array syntax {A,B} to join them row by row, making a 4 column array:
={
GOOGLEFINANCE("AMZN","price",TODAY()-365,TODAY(),"daily"),
GOOGLEFINANCE("MSFT","price",TODAY()-365,TODAY(),"daily")
}
Now we can apply a query to get the values we want:
=QUERY(
{
GOOGLEFINANCE("AMZN","price",TODAY()-365,TODAY(),"daily"),
GOOGLEFINANCE("MSFT","price",TODAY()-365,TODAY(),"daily")
},
"select Col1, Col2+Col4",
1
)
The query is making 2 rows:
The date column. Since it should be the same for both, no need to change anything
The sum of the 2 Close columns (2 and 4)
After that we can simply wrap everything to the SPARKLINE function, and we have our final result.
If you don't like having the whitespace characters, you can remove them without any problem.
Reference
Using arrays in Google Sheets (Google Docs Editors Help)
QUERY function (Google Docs Editors Help)

Data reduction via IMPORTRANGE

I am trying to do some data reduction in my Google Sheets by using the following IMPORTRANGE formula:
=query(importrange("https://docs.google.com/a/ap.averydennison.com/spreadsheets/d/1xz1lXY-w5Ii_aWqVAhHgRCmeoes9ltSUtibE4kzhMHA/edit#gid=2051232966","SF_Flex_Rel!a:l"),
"select * where Col1 = '"&text(B1,"###")&"'",1)
The 'source' sheet has a whole lot of sales data records. What I am trying to do in the new sheet via this formula is only bring in the sales records from the source sheet that match the customer number specified in cell B1.
It seems to work OK if I limit the IMPORTRANGE to only query about 10,000 rows. Once I go over around 20,000 rows the screen will briefly flash up the records, then a small progress bar shows in the top right corner of the sheet and the records disappear. The cell with the formula just shows #ERROR! with no other comments to tell me why.
Is there something wrong with my formula syntax?
Is there a better way to achieve this data reduction?
Is there some undocumented data limitation on IMPORTRANGE function (I am using 'new' Google Sheets)?
try like my example :
=QUERY( // data
IMPORTRANGE(
"Spreadsheet Key", // spreadsheet key
"DATA!A:C" // datarange
),
"SELECT Col1 WHERE Col2=" & "'" & B2 & "'" // query
)
I had the same problem. This answer helped me find a workaround : https://productforums.google.com/forum/#!topic/docs/RxVUFGWQ2Y4
In my example :
1) In the spreadsheet where the data is I have added a few empty columns (E to H) in order to display 4 columns of data in 5 maximum rows.
=Query(Sheet1!A:D,"select * Where A contains 'KEYWORD' limit 5",1)
2) Then in the other spreadsheet:
=ImportRange("https://docs.google.com/spreadsheets/d/ss_key_here/", "'Sheet1'!E1:H5")

Resources