Using Countif, Vlookup and Importrange - google-sheets

I'm currently struggling with using the Countif, Vlookup and Importrage formula on Google Sheets...
I need to pull data from the raw data sheet to the data set that I'm using and can't get it right.
Formula that I have is as follows:
=countif(vlookup(A2,IMPORTRANGE("URL","Data!A2:I940"),9,false),"supplier")
I need to look up the date and then get the count for how many "Suppliers" we had on that specific dates..
Anyone having the same issue or is my brain just over worked???

I think a better choice would be to use a query formula instead.
Please follow the logic of this given formula
=QUERY({B1:D12},"select count(Col1)
where Col3=date'"&TEXT(F1,"yyyy-mm-dd")&"' and Col2='"&F2&"'
label count(Col1) '' ")
(Do adjust ranges to your needs)

Related

Only apply complex arrayformula() to rows with certain value in dataset

I have a quite complext formula (i mean that is complex to me) that Tom Sharpe helped me building to aggregate values and ordering them by months in a row(you can find the details in the original post but i think you'll only need the final formula which is:
=ArrayFormula(mmult(sequence(1,counta(A2:A),1,0), if((C2:index(C:C,counta(C:C))<=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0)))* (D2:index(D:D,counta(D:D))>=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0))),E2:index(E:E,counta(E:E)),0)))
and here is the result -> [J1:U1]
Now, what i would need to do as the final step is to be able to group data by a certain label (John or Jane in the example) on separate rows, but mantaining the order/aggregate by month on the row. On the example, this would mean having one row with only 'John' data and below, one with 'Jane' values.
I am struggling to understand how to adapt the formula to do so.
I have tried:
Using another array to first return a list of these labels with query(unique()) or something like that, but then i struggle looping in it with the other formula.
A bit more simplistic but it could work after all: on the 1st row (the cell next to where the data will be returned) writing 'John', on row 2 'Jane' and then using filter() to only pull data that matches. The 'John, Jane' value is for the example but the real labels won't be that many, the list of labels don't need to be dynamic.
The thing with these solutions is that they work when used separately, but i can't figure out how to nest this in the first arrayformula() that Tom helped me with...As i am just beginning with the google sheets queries.
I don't really need necessarily the complete formula/code but maybe just directions or tips to visualize the way i could solve this.
Thanks to all who might contribute
With hindsight I might have done better to go down the route of using a query to calculate the sums on my previous answer rather than Mmult.
This uses the same method as before to create a 2d array of amounts vs dates (going across) and individuals (going down). Then it uses Textjoin to generate a query to group by name with the required number of columns.
=ArrayFormula(query({A2:A,if((C2:C<=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0)))* (D2:D>=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0))),E2:E,0)},
"select Col1,sum(Col"&textjoin("),sum(Col",,sequence(1,datedif(G2,H2,"M")+1,2))&") where Col1 is not null group by Col1"))
This is the generated query
select Col1,sum(Col2),sum(Col3),sum(Col4),sum(Col5),sum(Col6),sum(Col7),sum(Col8),sum(Col9),sum(Col10),sum(Col11),sum(Col12),sum(Col13) where Col1 is not null group by Col1
Ideally there should be an extra section saying label sum(Col2) '' etc. to suppress the 'Sum' headers.
=ArrayFormula(query({A2:A,if((C2:C<=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0)))* (D2:D>=eomonth(G2,sequence(1,datedif(G2,H2,"M")+1,0))),E2:E,0)},
"select Col1,sum(Col"&textjoin("),sum(Col",,sequence(1,datedif(G2,H2,"M")+1,2))&") where Col1 is not null group by Col1 label sum(Col" & textjoin(") '', sum(Col",,sequence(1,datedif(G2,H2,"M")+1,2)) & ") ''"))

How do I get Row Totals using QUERY() in Google Sheets?

I'm working with the following Google Sheet.
Sheet2 uses the following QUERY() function to retrieve data from Sheet1
=QUERY(IMPORTRANGE("1s8krJ7rbZ1DMblZ3vdLcG5pySVM3ESCBy1o7R5Zv4LM", "Sheet1!B3:D"))
Is it possible to return the Row Totals (For Example: B4+C4+D4 for Row 4) using the above QUERY() function?
Please Advise.
My Query and Expected Output are Outlined on the Google Sheet.
You should be able to do something like this:
=QUERY(IMPORTRANGE("1s8krJ7rbZ1DMblZ3vdLcG5pySVM3ESCBy1o7R5Zv4LM", "Sheet1!B3:D"), "Select Col1+Col2+Col3 label Col1+Col2+Col3 ''")
Note that importing from another tab in the same spreadsheet doesn't require importrange. In that case, this should also work:
=QUERY(Sheet1!B3:D, "Select B+C+D label B+C+D ''")
Another way, to achieve the same result would be
=ArrayFormula(if(len(Sheet1!B3:B), Mmult(--Sheet1!B3:D, transpose(column(Sheet1!B2:D2)^0)),))
Be aware that while a simple QUERY can return the results, it will actually take up the entire column if you don't limit it. In other words, all the null rows from Sheet1! B:D will also come over with the QUERY. If you want only the results that have numbers, try something like this:
=QUERY(Sheet1!B3:D, "Select B+C+D WHERE B+C+D is not null label B+C+D ''")
Or you could use MMULT like this:
=MMULT(FILTER(Sheet1!B3:D,Sheet1!B3:B<>""),SEQUENCE(3,1,1,0))
The results may look the same whether limited or not. But in a QUERY or MMULT without limitations, you won't be able to use the space below the visible results for anything. I only mention this because, currently in your sheet, you do have data (a TRANSPOSE formula) below the main results. If you won't in your real sheet and don't care about the are below the visible results being inaccessible to other data or formula entry, then you don't need to limit.

Create dynamic Year-Month headers based on a start Year - Google Query

Is there a way to create a single header row of 12 dynamic Year-Month columns (formatted yyyy-mmm) using Google Query? These dates should change based on a Year selection in say, Cell C1. e.g. C1 could have values like 2016,2017,2018,2019,2020.
Example with formulas e.g.
=arrayformula(EDATE(date(C1,1,1),SEQUENCE(1,12,)))
or
=Arrayformula(EOMONTH(date(C1,1,1),SEQUENCE(1,12,)))
i was able to formulate the following:
=ARRAYFORMULA(TRANSPOSE(QUERY(TRANSPOSE(QUERY(EDATE(date(C1,1,1),SEQUENCE(1,12,)))),"SELECT Col1 LABEL Col1 'DATES' FORMAT Col1 'yyyy-mmm'")))
anyone has a shorter way, do let me know.
Edit:
Alternatively, a shorter version, suggested by Tom Sharpe:
=ArrayFormula(date(C1,sequence(1,12),1))

Is there a way to make a query with an importrange to mimic a sumif formula from another Google sheet?

I am encountering a capacity issue with one of my spreadsheets but certain information in the sheet still needs to be available to cross-reference. The original spreadsheet contains this formula:
=sumif(A:A,A2,N:N)=O2
I tried to do a direct transfer to the new spreadsheet by adding importranges like this:
=SUMIF(IMPORTRANGE("URL","Sheet1!A2:A"),IMPORTRANGE("URL","Sheet1!A2"),IMPORTRANGE("URL","Sheet1!N2:N")=IMPORTRANGE("URL","Sheet1!O2")
The equation keeps putting out an error saying "argument must be a range". Then I tried to write it out as a query like this:
=QUERY(AND(IMPORTRANGE("URL","Sheet1!A:A"),IMPORTRANGE("URL","Sheet1!N:N"),"SELECT SUM(CASE WHEN Col1=A2 THEN Col14)=O2"))
But the equation is showing FALSE as the result when it should say TRUE. I have attached an example spreadsheet so that these equations make more sense.
I also need the equation to take into account any new information that is placed on the new spreadsheet while also comparing it with the old info. I tried doing this to start with but then slowly realized the initial equation itself doesn't work.
=SUMIF(IMPORTRANGE("URL","Sheet1!A2:A"),IMPORTRANGE("URL","Sheet1!A2"),IMPORTRANGE("URL","Sheet1!N2:N"))=IMPORTRANGE("URL","Sheet1!O2")+SUMIF(A:A,A2,N:N)=O2
Any help is appreciated.
https://docs.google.com/spreadsheets/d/1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo/edit#gid=0
use this formula instead of SUMIF:
=ARRAYFORMULA(IF(LEN(
IMPORTRANGE("1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo","Sheet1!C2:C")),
IF(IFERROR(VLOOKUP(
IMPORTRANGE("1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo","Sheet1!C2:C"),
QUERY(IMPORTRANGE("1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo","Sheet1!A2:N"),
"select Col3,sum(Col14) where Col1 is not null group by Col3 label sum(Col14)''"),2,0))=
IMPORTRANGE("1q1cWh58p23dcQulQ4rz6v4iiDywEFHvcrvASao61xxo","Sheet1!O2:O"), TRUE, FALSE),))

How to use absolute references in google sheets query formula or query by column names for multiple sheets?

I have the following problem. I'm trying to write query formula in order to put all values in one sheet by typing yes in one column, however, I have the following problem. Whenever I add a new column, references in query formula (multiple sheets) are changing and the formula doesn't work. How can I prevent this?
Or is there any way to query by a column name in multiple sheets?
I have tried locking relative references by putting sign $
I have tried to use an indirect formula to take references (data set in Helper TAB, cell E2) - nothing worked
I'm out of the ideas, for now, anyone knows how to fix this?
=QUERY({'Sheet 1'!$A:$Z;'Sheet 2'!$A:$Z;'Sheet 3'!$A:$Z;'Sheet 4'!$A:$Z}, "select * where Col1 ='yes'",0)
Here is the file I did, you can see query formula in Master sheet:
https://docs.google.com/spreadsheets/d/1XD-CECy5W5-HM5EkBFJQKDtLlykQq8Cj8ZOvDUXkd1s/edit?usp=sharing
A solution to the problem is to use a formula that searches for the column address based on a reference.
=SUBSTITUTE(ADDRESS(1;COLUMN(A1);4);"1";"")
This formula will return the value A, which is the column of address A1.
It can be used in query functions like this:
=QUERY(A:D;"SELECT "&SUBSTITUTE(ADDRESS(1;COLUMN(A1);4);"1";"")&"")
This function is equivalent to the function:
=QUERY(A:D;"SELECT A")

Resources