Dynamically changing Reference for Query funciton on Google Sheets - google-sheets

I've got a Spreadsheet where on the sheet "Dados de Cadastro e Resumo" on column K (from roll 7 to 29) I'm using the formula =IF(J7="";"";SUM(QUERY({Caminhoneiro!G:I;Centralizado!G:I;Djoko!G:I};"Select Col2 where Col1 = '"&J7&"'";))) to sum up the hours from the sheets "Caminhoneiro","Djoko" and "Centrealizado".
The thing is, I want to make it so I don't have to edit the formula every time to accommodate for new sheets. My ideia was to use the names on column E (from roll 7 to 29) and make the reference for the Query based on that.
So far I've got the string done (on M6), but haven't been successful on turning that on to a valid array reference (using INDIRECT, which I later learned doesn't deal with multiple intervals.

Related

Query importrange HUGE multiple sheets and turns out error

so I have 5 separate google sheets, each one with the same title and format enter image description here.
There are 8000-10000 rows each so far, as it's a daily sales updating data, yearly may finish with 20000-40000 rows each.
So what I want to do is to merge all of them into another single sheet, and I've tried the following formulas:
enter image description here
Formula A
={IMPORTRANGE(B2,C2);IMPORTRANGE(B3,C3);IMPORTRANGE(B4,C4);IMPORTRANGE(B5,C5);IMPORTRANGE(B6,C6)}
Results:
#VALUE!
"In ARRAY_LITERAL, an Array Literal was missing values for one or more rows."
Formula B (reduced the data to only one month)
=QUERY({IMPORTRANGE(B2,C2);IMPORTRANGE(B3,C3);IMPORTRANGE(B4,C4);IMPORTRANGE(B5,C5);IMPORTRANGE(B6,C6)},"SELECT * Where Col2 > DATE'2022-5-1' AND Col2 <= DATE'2022-5-31' ",0)
Result:
#VALUE!
"In ARRAY_LITERAL, an Array Literal was missing values for one or more rows."
Formula C (check if is the problem of the code)
={IMPORTRANGE(B2,C2);IMPORTRANGE(B3,C3)}
Results:
It works.
So my question is that, there is anyway via google sheet let me merge all the 5 sheets in one (5sheets*40000rows into one sheet) , or it's not the place to manage that volumn of data?
every importrange needs to be run separately before you use it in your joint formula. the array error you are getting is a result of some importranges importing those 7 columns and those importranges that are not connected output one single cell. make sure you allow access to all your importranges.

Google sheets- How to process and dynamically update the data like in MS Access queries?

community!
I have a table of 3 columns in Google Sheets: Date, Name, Amount.
I want to process data from it, so for every date in the list, there will be a full list of participants and amounts.
If there is no amount for specific date and person, it will be "0", or just leave it blank.
In addition, would like to count the percent of the sum of the amount for each person till the date in the corresponding row.
And it should be dynamically updated, so if a new row added with new name or date or both, so new participant will be added to every date in the output table and new full list of participants for the new date created.
For better understanding, here is the example of the data and output tables
What I've tried?
In MS access query it could be done not so hard.
Here I tried to create 2 new lists of unique dates and participants and connect them through use of CONCATENATE, ARRAYFORMULA, then SPLIT and some QUERY. All this through use of helping column of text, which should be edited manually for every new row...
The code in helping column:
=concatenate(arrayformula(if(isblank($F$3:$F),,";"&$E3&","&$F$3:$F)))
Then split code:
=query(arrayformula(split(transpose(split(TEXTJOIN(";",true,$G$3:$G),";",true,true)),",",true,true)),"Select Col1,Col2 where Col2 is not null order by Col1 ASC",0)
But here I stuck...
Want to pull the amounts for every corresponding date and participant, but...
FILTER reaches first, not existent in the original table combination, and doesn't proceed.
QUERY doesn't fetch the data fully automatically for all the list in the output table.
VLOOKUP gives only one row or complete mess, when use it recursively, or error...
So, how can I do this (if it's possible at all) in google sheets, and so all the output will be fully automatically updated?
Thank you very much!
Update - based on comments
I could not figure out a way to make this through one formula. If this works, you can have a hidden column (say H here) that totals the amount for the person till that date using the formula =QUERY(FILTER($E$3:$G, $F$3:$F = F3, $E$3:$E <= E3), "select sum(Col3) label sum(Col3) ''", 0) in H3.
Then in I3 you can find the % of total till date using the formula =QUERY(FILTER($E$3:$G, $F$3:$F = F3, $E$3:$E <= E3), "select sum(Col3) label sum(Col3) ''", 0)/SUMIF($E$3:$E, E3, $H$3:$H).
Both of the formulae in H3 and I3 need to be dragged down unlike the one in E3. Perhaps someone will be able to offer a better solution.
Previous answer
You can try something like this:
In E3 you can have the formula =ArrayFormula({(FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦"))), (TRANSPOSE(SPLIT(REPT(JOIN("", FILTER(UNIQUE(B3:B)&"◼︎", UNIQUE(B3:B)<>"")), (COUNTA(UNIQUE(A3:A)))), "◼︎"))), (IFNA(VLOOKUP({(FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦")))&(TRANSPOSE(SPLIT(REPT(JOIN("", FILTER(UNIQUE(B3:B)&"◼︎", UNIQUE(B3:B)<>"")), (COUNTA(UNIQUE(A3:A)))), "◼︎")))}, {A3:A&B3:B, C3:C}, 2, 0), 0))}). It is a little long but fills Columns E through G dynamically.
Then in H3 you can have the formula =ArrayFormula(IFERROR(((IFNA(VLOOKUP({(FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦")))&(TRANSPOSE(SPLIT(REPT(JOIN("", FILTER(UNIQUE(B3:B)&"◼︎", UNIQUE(B3:B)<>"")), 3), "◼︎")))}, {A3:A&B3:B, C3:C}, 2, 0), 0))/(SUMIF((FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦"))), (FLATTEN(SPLIT(REPT(FILTER(UNIQUE(A3:A)&"✦", UNIQUE(A3:A)>0), COUNTA(UNIQUE(B3:B))), "✦"))), G3:G))), "")). This one is dynamic as well.
I tried with your data and added a row on my own and it works.

Query particular row + remove X columns + and sum the rest in one formula?

I have a CSV file that I'm pulling from a database. It's in an awkward layout so I need to reorganise it and display the result in a separate sheet.
Here is a dummy example of the data structure I get.
https://docs.google.com/spreadsheets/d/1sTfjr-rd0vMIeb3qgBaq9SC8felJ1Pb4Vk_fMNXQKQg/edit?usp=sharing
It looks like that. The database grows every day by date and sometimes countries so I need to account to that in my formula.
I need to pull data per each country and display it by date.
I don't need data from Column A, C and D. And when there are multiple states I need to sum them up in one column.
It should look like this and keep growing downwards. I'm gonna use this table for a graph chart
What I've tried so far
=TRANSPOSE(QUERY(IMPORTRANGE("url_to_a_separate_sheet_where_I_importing_a_row_csv_file", "CSV-source-sheet!A1:500"), "SELECT * WHERE Col2='Germany'"))
This works, kinda. But pulls in unnecessary columns and I can't figure out how to sum countries with multiple states. When I add select sum(*) it gives me a big and long error. I assume it might be because of unnecessary columns that the formula cant sum up and I don't know how to omit them. I'm stuck
I tried offset and skipping no luck. Any ideas?
try:
=ARRAYFORMULA(TRANSPOSE(QUERY({Sheet2!B:B, Sheet2!E:BE},
"select Col1,"&TEXTJOIN(",", 1,
"sum(Col"&ROW(INDIRECT("Sheet2!A2:A"&COUNTA(Sheet2!1:1)-5))&")")&"
where Col1 is not null
group by Col1
label Col1'Date'", 1)))
spreadsheet demo

Query and ImportRange with Dynamic Values

I have 2 sheets which are connected.
Sheet-1: Has ID column and Status column (Status field is manually updated by the team)
Sheet-2: Has ID column and Status column (+ many other fields)
Requirement: Status column in Sheet 2 to be updated from Sheet-1 if ID column in sheet-2 = ID column in Sheet-1
I wrote a Query and ImportRange, but I could find Query working only when I compare it against a static value (for e.g. Yes). What I want is it to check against the value in ID column from both the sheets and import only for rows that match.
=QUERY(IMPORTRANGE("1ZkPaYb1IIIkcbVerdmZ-Ru1vxFu1YMWj74QNQj2jles", "Ops Action Sheet!B2:B10000"),"select Col2 where Col1 = 'Yes'")
This doesn't directly answer your question about using QUERY, but I believe that that function might not be the best tool for what you're trying to do.
I write below a solution using the alternative function VLOOKUP.
I am assuming here that by "Sheet-1" and "Sheet-2", you actually mean entirely different spreadsheets, judging by your use of IMPORTRANGE.
If you don't mind having a copy of Sheet-1's contents living within Sheet-2, here is what you can do:
On a new sheet (tab) in Sheet-2, copy this function (fill in sheet_1_url):
=IMPORTRANGE("sheet_1_url", "Ops Action Sheet!$A:$B")
You should see a copy of the data from Sheet-1 in that tab. You can hide this tab or protect it if you wish.
On your main data tab within Sheet-2, copy this formula in your Status column (assuming that your ID is in A2:
=IFERROR(VLOOKUP($A2,range_from_step_1,2,),)
Copy that formula down as much as you need.
However, if you don't want to have a copy of Sheet-1's data in Sheet-2 for whatever reason, you can simply skip steps 1-2 above, going straight to step 3, with the following formula in Sheet-2:
=IFERROR(VLOOKUP($A2,IMPORTRANGE("sheet_1_url", "Ops Action Sheet!$A:$B"),2,),)
Not that I recommend it, since you'd technically be importing your data virtually within the formula N number of times, which will be a huge performance hit.
You can see the references for the formulas used above here:
IMPORTRANGE
VLOOKUP
IFERROR
edit to reflect and paste in spreadsheet 2:
=ARRAYFORMULA(IFERROR(VLOOKUP(A2:A, IMPORTRANGE("ID", "Sheet!A:C"), {2, 3}, 0)))
A2:A - column of IDs in spreadsheet 2
Sheet!A:C - column A of this range hosts IDs
{2, 3} - brings column B and C from spreadsheet 1 into spreadsheet 2

Google Sheets Query returning results that don't exist in the source list

I am creating a budget tracker within Google Sheets. I am having trouble with Google Query Language returning results that do not exist in the source data
I have a Google Form into which expenditure can be entered, which populates an associated Sheet in my Google Drive. My Master budget tracker also sits within my Google Drive. I use IMPORTRANGE to pull the data into the Master from the Responses spreadsheet, and then Query to separate this out into different 'expenditure' categories on sheets within the workbook (one for each month). February worked perfectly - all expenditure was found and summed correctly and then the February sheet was duplicated for each month of the year, the formulae or look up terms updated. But March is acting strangely - it is returning a value for March that doesn't exist - and not only does the summed value not exist but the word 'March' also does not exist either, so shouldn't be matching. I have tweaked the code, tried refreshing and rewriting the formula into the cell to force a refresh, re-imported the range from the external spreadsheet, I have tried various parentheses placements (I'm not a SQL or Google Query Language expert, so am feeling my way a bit) as I thought it was something to do with the AND/OR clauses not being 'collated', but none has produced even a different result, it's always the same false value being returned
The query code is as follows:
=query(spend, "select sum(B) where H = '"&$H$1&"' and E='Leisure' or E='Tickets' or E='Parking' or E='Other' label sum(B) ''", -1)
'spend' is the range containing the data imported from the Responses spreadsheet, which includes several post form completion formulae coding the row with a day and a month. Right now, there are only values coded as 'February' in H - nothing else. Cell H1 contains the month name (written in, not formula). This formula works perfectly within the 'February' sheet, and if I update cell H1 to read 'February' in the March sheet it shows the accurate values for February, however, if I enter 'March' in H1 I am getting the odd outcome
I am expecting a £0 result for March, but instead, I am getting a value of £19.46. As previously described - the source list 'spend' only contains values coded as February in H, and the value £19.46 does not appear singularly in the list (and doesn't appear to be made by any values when 'summed'). I am at a loss as to what is happening, and no answers seem to address the appearance of mystery values, so I hope I'm not repeating old ground - please do correct me if I am, and many thanks in advance for any guidance
you picked 19.46 because even if H1 wasn't found in Query, Query continued to evaluate for AND and OR statements and sum up a bunch of nonsense
=IFERROR(IF(QUERY(spend,
"select count(H)
where H='"&$H$1&"'
label count(H)''", 0)>0,
QUERY(spend, "select sum(B)
where (H='"&$H$1&"')
AND (E='Leisure')
OR (E='Tickets')
OR (E='Parking')
OR (E='Other')
label sum(B)''", -1), ), )
I think you might just want to try this:
=query(spend, "select sum(B) where H = '"&$H$1&"' AND (E='Leisure' OR E='Tickets' OR E='Parking' OR E='Other') label sum(B) ''", -1)
EDIT: This will cause an error since it will return an empty query, so try this instead.
=iferror(query(spend, "select sum(B) where H = '"&$H$1&"' AND (E='Leisure' OR E='Tickets' OR E='Parking' OR E='Other') label sum(B) ''", -1),0)

Resources