Unique number of dates for certain criteria - google-sheets

In the below screenshot I would like to get column E as my output. I need the unique number of dates where campaign/Ad group budget (col D) = campaign name (col B)
I tried using the following query "query(A2:E9,"select A, count(A) where E=B group by A")" and iterating it using Apps Script for loop. However, that did not work.
Is there any other way to find this output?

I think this formula will get you both the date and the count that are unique.
=unique(filter({A:A,E:E},B:B=D:D,B:B<>""))
You could then leverage an index function to get just on column. So this would give you just the count:
=Index(unique(filter({A:A,E:E},B:B=D:D,B:B<>"")),,2)
while this would get you the date
=Index(unique(filter({A:A,E:E},B:B=D:D,B:B<>"")),,1)

try:
=QUERY(A2:E9, "select A,count(A) where D=B group by A")

Related

Counting the number of times a value appears more than once in a column AND where another conditon is met

Any help in figuring this out would be appreciated. I would like a forumla to calculate the number of times a code number appears more than once AND where type is A.
A sample set of data looks like the following:
In this case the forumla should return 1 as there is one case of a repeated code number (1) where type is (A) - first row and last row in this case.
Would the forumla be any different if I also had a third column and wanted that to be a certain value as well? Again with the test data below I would want this to return 1 in the case that I wanted to measure the number of times any code number appeared more than once where type=A and subtype=C:
.
Ihave started with the following which identifies the number of unique combinations in columns A and B, but I can't seem to add any way to only return where a particular combination appears more than once:
=COUNTUNIQUE(IFERROR(FILTER(A2:A,B2:B="A"),""))
I have tried the following but it doesn't return correctly:
=COUNTUNIQUE(IFERROR(FILTER(A2:A,B2:B="A",COUNTIF(A2:A,A2:A)>1)))
Been trying to figure this one out for a while with no success.
Thank you
You can try this (TABLE = the range corresponding to your dataset, including the header row):
=query(query(transpose(query(transpose(TABLE),,9^9)),"select Col1,count(Col1) where Col1 contains 'A' group by Col1",1),"select Col2-1 where Col2>1 label Col2-1 ''")
What we are doing is to concatenate the Code number & type columns into one using the TRANSPOSE/QUERY/TRANSPOSE...9^9 hack, querying it again to make a temporary table of each group against its count for those groups which meet the criteria, then finally subtracting one from each group count and only returning an answer if there were groups with count>1 to begin with. You will get multiple results if multiple groups satisfy the count>1 criteria.
To add the subtype column to the formula as per the second question, change TABLE to suit, then change the inner QUERY to:
"select Col1,count(Col1) where Col1 contains 'A' and Col1 contains 'c' group by Col1"
Note that the if your 'real' type & subtype categories share characters then the where/contains approach in the QUERY will fail and a different approach will be needed.
Assume that you place you data at A1:B10, what this function do is:
FILTER B1:B10 by type, which is "A" in this example, and return an array which is filtered A1:B10.
Use INDEX to extract only the 1st column, which is the code column of the filtered array, and name it 'DATA' with LAMBDA function.
Use BYROW to iterate 'DATA', and check each code with COUNTIF, if it counts more than one of this code in the filter result, return that code, else return "".
Use UNIQUE to get rid of duplicate results. (since we are looking for code which have more than 1 repeats, so the return array will sure have duplicates.)
Use query to get rid of the extry empty rows.
=QUERY(UNIQUE(
LAMBDA(DATA,
BYROW(DATA,LAMBDA(ROW,
IF(COUNTIF(DATA,ROW)>1,ROW,"")
))
)(INDEX(FILTER(A1:B10,B1:B10="A"),,1))
),"WHERE Col1 IS NOT NULL")
Just noticed that the INDEX function is not necessary, FLITER can directly returns A1:A10 according the compare results of B1:B10.
=QUERY(UNIQUE(
LAMBDA(DATA,
BYROW(DATA,LAMBDA(ROW,
IF(COUNTIF(DATA,ROW)>1,ROW,"")
))
)(FILTER(A1:A10,B1:B10="A"))
),"WHERE Col1 IS NOT NULL")

Google sheet complex query?

Hello I need help with named range in google sheet...
=QUERY(Data,"SELECT C,D,E,SUM(G),L,SUM(G*M) WHERE A = '"&$A$1&"' AND E = '"&$B$1&"' GROUP BY C,D,E,L",1)
I'd like to calculate columnG*columnM and than SUM this results when grouping.
This works fine before I start grouping table. I could do columnG*columnM get result and use another QUERY but don't like the idea of using many helpers. Is it even possible?
Could you provide a link to your project ? I make a simplified example : if you need the sum of ColumnB x ColumnC, you have to add to your data an extra column as following
=query({A2:C,arrayformula(B2:B*C2:C)},"select SUM(Col4) group by Col1 ")
and then usie ColX instead of A,B,C and the extra column D.

Looking for unique values then count values from another column with index and match and countif

I've been struggling with an index and Match formula together with countif to figure out how to look in column B (clacc_no) to look for unique values and then to count the number of times order_no (column A) that relate to the unique value in column B. In the image below you will notice clacc_no 84242 is shown twice but there are two order_no that relate to this, so I would like to receive this as the count.
In addition, I would then separately like to sum up the order_total_sell (column C) related to these unique values in Column B.
Can anyone point me in the right direction as Index and Match with countif and sum are not working for me unless I'm just doing something wrong?
Many thanks for reading.
try:
=QUERY(B2:C,
"select B,count(B),sum(C),avg(C)
where B is not null
group by B
label count(B)'No. of Orders',sum(C)'Total Value',avg(C)'Average Value'")

How do I GROUP BY for only one column, while still being able to sort by and select other columns?

I have a sheet holding raw data that I would like to filter and sort in another sheet.
Raw data sheet
The goal of the sheet is:
Show only the highest Rank per Name at Level 12 or below
Sort the results from highest to lowest Damage
I show and sort all items with a Level of 12 or below using ORDER BY:
=ARRAYFORMULA(QUERY(RAW!2:1000, "select A,B,D where C<=12 and A<>'' order by D desc"))
Sorted by Damage
But this still shows too many entries. I can filter the entries using MAX() and GROUP BY:
=ARRAYFORMULA(QUERY(RAW!2:1000, "select A,max(B) where C<=12 and A<>'' group by A label max(B) ''"))
Filtered to only show highest rank
However, I cannot sort by DAMAGE or else I get this error:
Unable to parse query string for Function QUERY parameter 2:
COL_IN_ORDER_MUST_BE_IN_SELECT: `D`
And attempting to add that column to the SELECT clause gives this error:
Unable to parse query string for Function QUERY parameter 2:
ADD_COL_TO_GROUP_BY_OR_AGG: D
The error means that I cannot include fields that are not either aggregate formulas (like MAX) or are not a part of the GROUP BY (which DAMAGE is not).
My goal is this:
Filtered to only show highest rank and sorted by damage
How would I go about querying the first sheet to end up with the above sheet?
Here is the actual sheet. Please feel free to fork if you'd like to test an solution.
https://docs.google.com/spreadsheets/d/1XG3eTSc-8eYRh-6ekq_2BjZgleUVCz3v45PLozXe8Xo/edit
If you work from your query (arrayformula is not needed)
=QUERY(Raw!2:500, "select A,B,D where C<=12 and A<>'' order by D desc")
You can filter the query results to get your desired output:
=FILTER(A2:C,MMULT((A2:A=TRANSPOSE(A2:A))*(B2:B<TRANSPOSE(B2:B)),SIGN(ROW(B2:B)))=0)
I have not had success combining the two.
This is possibly a trivial answer, but might help to clarify your requirements. In the case of your test data where max(damage) is in the same row as max(rank), you can just put
=ARRAYFORMULA(QUERY(Raw!2:1000, "select A,max(B),max(D) where C<=12 and A<>'' group by A order by max(D) desc label max(B) '', max(D) ''"))
If what you want is to sort in general by 'the value of damage that corresponds to the highest rank' then the answer is more complicated!

Address lookup after filter

I have a Google form and a spreadsheet that is supposed to process it. The spreadsheet needs to filter the responses by date and get their respective data after that.
E.g. Consider:
With the columns being A and B and the rows being 1-4.
How do I filter it by month in ascending order followed by adding the amount next to it?
So it would look something like:
I understand how to filter by month in ascending order. It is done using =SORT(FILTER(A:A, MONTH(A:A)=1)) (for Jan). But how do you get the data next to it? I have tried =INDIRECT(ADDRESS(SORT(FILTER(A:A, MONTH(A:A)=1)), 2)) but there is no error and it is blank.
You can do SORT for whole A:B instead of doing only for A:A.
Eg:
SORT(FILTER(A:B, MONTH(A:A)=1),1,TRUE) for Jan
SORT(FILTER(A:B, MONTH(A:A)=2),1,TRUE) for Feb
Please refer link here and see whether this is what you expect.
try this:
=QUERY(A:B,"select month(A), A, sum(B) where not A is null group by month(A), A")
or this:
=QUERY(A:B,"select day(A), sum(B) where not A is null group by day(A) pivot month(A)")
or you could use your formula =SORT(FILTER(A:A, MONTH(A:A)=1)) twice and use custom date format for first column to label it as JAN, FEB...

Resources