Query with date Pivot and date order - google-sheets

I have this simple table and query:
https://docs.google.com/spreadsheets/d/1MwdP08WWmkG13fGmKjB5hpL-D_uqgAu0lsXrO8WcE1M
the problem is, I cannot get the columns to be ordered by date. I've tried transpose+sort+transpose, but it doesn't work as it puts "2019-10-1" before "2019-9-1" (because it interprets date as text). Is there any way to get this sorted?

you can do:
=ARRAYFORMULA(QUERY(QUERY(A:D;
"select B,sum(C),sum(D)
where B is not null
group by B
pivot A");
"select Col1,"&TEXTJOIN(","; 1;
{"Col"&ROW(INDIRECT("A2:A"&COUNTUNIQUE(A2:A)+1))\
"Col"&ROW(INDIRECT("A"&2+COUNTUNIQUE(A2:A)&":A"&COUNTUNIQUE(A2:A)*2+1))})))
and if you want to remove that sum:
=ARRAYFORMULA(REGEXREPLACE(TO_TEXT(QUERY(QUERY(A:D;
"select B,sum(C),sum(D)
where B is not null
group by B
pivot A ");
"select Col1,"&TEXTJOIN(","; 1;
{"Col"&ROW(INDIRECT("A2:A"&COUNTUNIQUE(A2:A)+1))\
"Col"&ROW(INDIRECT("A"&2+COUNTUNIQUE(A2:A)&":A"&COUNTUNIQUE(A2:A)*2+1))})));
" sum "; " "))
where can I add a date filter where A >= date '2019-07-01'. I've tried adding it in the first block, but I keep getting the error: "Error Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: ColXX"
=ARRAYFORMULA(REGEXREPLACE(TO_TEXT(QUERY(QUERY(A:D;
"select B,sum(C),sum(D)
where B is not null
and A >= date '2019-7-1'
group by B
pivot A ");
"select Col1,"&TEXTJOIN(","; 1;
{"Col"&ROW(INDIRECT("A2:A"&COUNTUNIQUEIFS(A2:A; A2:A; ">="&DATE(2019;7;1))+1))\
"Col"&ROW(INDIRECT("A"&2 +COUNTUNIQUEIFS(A2:A; A2:A; ">="&DATE(2019;7;1))&":A"&
COUNTUNIQUEIFS(A2:A; A2:A; ">="&DATE(2019;7;1))*2+1))})));
" sum "; " "))

Related

Google Sheet Filter - exclude the first match associated to a column

I have this Google Sheet that I ran a Filter in E2 to show all other stock-id's associated with each ID (in column A). What I want to achieve is to exclude in the result the data in the 1st match (in column F) because that is already in the stock-id column. I am using this formula FILTER(E1:E,A1:A=A2) that I just Transpose.
https://docs.google.com/spreadsheets/d/1Hp3TmTPlUTEkrhN3iEMLtlMbDZoapvJb5T8oYr2SmB0
Thank you for your help.
In column F, put
=transpose(FILTER(E$2:E,A$2:A=A2,E$2:E<>E2))
and spread.
filter() can accept multiple conditions.
btw, if you happen to be wondering how to make a single formula so you dont have to spread the formula, the better recommendation is: don't.
Option 01
Paste it in F2 and drag it down
=IF(COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2)))<=1,TRANSPOSE(FILTER(E1:E,A1:A=A2)),TRANSPOSE(ArrayFormula(QUERY( {SORT( ArrayFormula(SORTN( { QUERY( {TRANSPOSE( { SEQUENCE(1,COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2))),1,1); TRANSPOSE(FILTER(E1:E,A1:A=A2))})}, " Select Col1 "), QUERY( {TRANSPOSE( { SEQUENCE(1,COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2))),1,1); TRANSPOSE(FILTER(E1:E,A1:A=A2))})}, " Select Col2 ") } ,IF(( COUNTA(QUERY( {TRANSPOSE( { SEQUENCE(1,COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2))),1,1); TRANSPOSE(FILTER(E1:E,A1:A=A2))})}, " Select Col2 ")))<1,1,( COUNTA(QUERY( {TRANSPOSE( { SEQUENCE(1,COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2))),1,1); TRANSPOSE(FILTER(E1:E,A1:A=A2))})}, " Select Col2 "))-1)),,1,0)),1,1)}, " Select Col2"))))
Option 02
Paste it in F2 and drag it down
=IF(COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2)))<=1,TRANSPOSE(FILTER(E1:E,A1:A=A2)),TRANSPOSE(ArrayFormula(QUERY( {SORT( ArrayFormula(SORTN( { QUERY( {TRANSPOSE( { SEQUENCE(1,COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2))),1,1); TRANSPOSE(FILTER(E1:E,A1:A=A2))})}, " Select Col1 "), QUERY( {TRANSPOSE( { SEQUENCE(1,COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2))),1,1); TRANSPOSE(FILTER(E1:E,A1:A=A2))})}, " Select Col2 ") } ,IF(( COUNTA(QUERY( {TRANSPOSE( { SEQUENCE(1,COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2))),1,1); TRANSPOSE(FILTER(E1:E,A1:A=A2))})}, " Select Col2 ")))<1,1,( COUNTA(QUERY( {TRANSPOSE( { SEQUENCE(1,COUNTA(TRANSPOSE(FILTER(E1:E,A1:A=A2))),1,1); TRANSPOSE(FILTER(E1:E,A1:A=A2))})}, " Select Col2 "))-1)),,1,0)),1,1)}, " Select Col2"))))
To exclude the first match you get from filter(), use query(), like this:
=transpose( iferror( query( filter(E$2:E, A$2:A = A2), "offset 1", 0 ) ) )
But apparently what the OP really needs is not that. To exclude a value that is already present in the same row, add another criteria in the filter() instead. See the other answer.

Google Sheet IFS combine with FILTER Function

I have the following Google Seet Table
Old New New2
W01
W02 W04
W03 W05 W06
I want to create a formular that transforms the table to this one
Old New
W02 W04
W03 W05
W05 W06
So any time a switch from Old to New or New to New2 happens should be displayed.
I wrote the following formular but i always get an error:
= IFS(B1 = "";""; AND(NOT(B1 = ""); NICHT(C1 = ""));FILTER({A1\ B1}; NICHT(A1=""));NICHT(B1 = "");FILTER({B1\ C1}; NICHT(B1="")))
Has anybody an idea?
Concatenate the results of two Query calls:
={
QUERY(A1:B4,
"select A,B where B<>''");
QUERY(B1:C4,
"select B,C where C<>'' label B '', C ''", 1)
}
or in German locale:
={
QUERY(A1:B4;
"select A,B where B<>''");
QUERY(B1:C4;
"select B,C where C<>'' label B '', C ''"; 1)
}
The label statements in the second query are necessary to suppress the column labels since you want to treat certain columns in New1 as Old.

Google Sheets SUMIF Text Formula

Ok so, I will arleady say that im not that good with excel and even worse on google sheets.
I would like to know if there is a formula that checks for the name of someone and then sums the points said person got to his total. Thank you very much!
https://docs.google.com/spreadsheets/d/1D4M8Dlao8yFHJulNDff-i2jZJeVGGY_gejbAfzWdhFo/edit?usp=sharing is the link if you want to check it out.
I assume you want Punti tot. (R:R) column to have the sum of all TA Brawl (column C:C), Notte (L:L) and 50 Shades (H:H; in this case any of the two players should be checked, right?) for every player.
You can use put this in R2 (do not forget to remove everything below R2):
={
"Punti tot.";
ARRAYFORMULA(
IF(
P3:P = ""; ;
SUMIF(A3:A; P3:P; C3:C)
+ SUMIF(E3:E; P3:P; H3:H)
+ SUMIF(F3:F; P3:P; H3:H)
+ SUMIF(J3:J; P3:P; L3:L)
)
)
}
Update: here is another solution using QUERY which is also sorts players by total points:
=QUERY(
{
FILTER({A3:A\ C3:C}; A3:A <> "");
FILTER({E3:E\ H3:H}; E3:E <> "");
FILTER({F3:F\ H3:H}; F3:F <> "");
FILTER({J3:J\ L3:L}; J3:J <> "")
};
"
select Col1, SUM(Col2)
group by Col1
order by SUM(Col2) desc,
Col1
label Col1 'Player',
SUM(Col2) 'Punti tot.'
";
-1
)
And you might want to use this in O2 for ranking:
={
"Posizioni Finali";
ARRAYFORMULA(
RANK(
FILTER(Q3:Q; Q3:Q <> "");
FILTER(Q3:Q; Q3:Q <> "")
)
)
}
This way same amount of points gives players the same place. Otherwise hysen would get 9th place and Imurshh would be 10th just because h goes before i.

How can I combine multiple functions with ArrayFormula

I've the below formula using ImportRange and Query along with Join and Split working correctly:
=join(" / ", QUERY(IMPORTRANGE("Google-Sheet-ID","RawData!A:AC"),"select Col25 where Col1 = " & JOIN(" OR Col1 = ", split(V2:V,"+")), 0))
Also, I've the below ArrayFormula with Split function working smoothly:
=ARRAYFORMULA(if(len(V2:V)=0,,split(V2:V,"+")))
But When I tried combining them together using the below formula:
=ARRAYFORMULA(if(len(V2:V)=0,,join(" / ", QUERY(IMPORTRANGE("Google-Sheet-ID","RawData!A:AC"),"select Col25 where Col1 = " & JOIN(" OR Col1 = ", split(V2:V,"+")), 0))))
It failed, and gave me the below error:
Error
Function SPLIT parameter 1 value should be non-empty.
Here is my sheet for your testing.
UPDATE
I changed it to:
=ARRAYFORMULA(if(len(C2:C)=0,,JOIN(" OR Col1 = ", ARRAYFORMULA(if(len(C2:C)=0,,split(C2:C,"+"))))))
So my full formula is:
=ARRAYFORMULA(
if(
len(C2:C)=0,,
join(" / ",
QUERY(
IMPORTRANGE("14iNSavtvjRU0XipPWIMKyHNwXTA85P_CafFTsIPHI6c","RawData!A:AC"),"select Col25 where Col1 = " &
ARRAYFORMULA(
if(len(C2:C)=0,,
JOIN(" OR Col1 = ",
ARRAYFORMULA(
if(
len(C2:C)=0,,split(C2:C,"+")
)
)
)
)
),
0
))))
And now getting the error:
Error
JOIN range must be a single row or a single column.
I believe this formula on the tab called MK.Testing will pull the info you're hoping for.
=QUERY(IMPORTRANGE("14iNSavtvjRU0XipPWIMKyHNwXTA85P_CafFTsIPHI6c","RawData!A:AC"),"select Col25 where Col1="&TEXTJOIN(" or Col1=",TRUE,A2:A))
I think you might have been overcomplicating things? This formula just forms a text string out of the shipment IDs to use in a query. one thing that may be tripping you up is that query() is very particular about the type of data in a column. Your shipment IDs can be numbers, or they can be number letter combos, but not both. That is, if you have some shipment IDs that contain letters and others that don't, it will be more difficult to get a query that works. (though not impossible). For the sake of helping you though, it's important that your sample IDs reflect the real ones in this way as accurately as possible.
How about doing this with Apps Script? You can get the values from the Sheet2, Shipment Ids, and the Ids from MK.Testing and compare them. If they coincide, the you copy the ETA into the Column C of MK. Testing:
function myFunction() {
var sprsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = sprsheet.getSheetByName("Sheet2");
var mkTesting = sprsheet.getSheetByName("MK.Testing");
var shipmentId = sheet2.getRange("A2:A").getValues();
var idList = mkTesting.getRange("A2:A").getValues();
for (var i = 0; i < shipmentId.length; i++){
for (var j = 0; j < idList.length; j++){
if (idList[j][0] == ""){break;} //Stops if there is an empty cell in Mk.Testing's column A
if (idList[j][0] === shipmentId[i][0]){
var eta = sheet2.getRange("E"+(i+2)).getValue();
mkTesting.getRange("C"+(j+2)).setValue(eta);
}
}
}
}
References:
SpreadsheetApp Class
Range Class

Select 'x' returns "x"()

I want to return 3 columns, A(description), 'D'(hard coded value of D), Q(date)
=query('Detailed Plan'!$A$2:$Q, "select A,'D',Q where D = date)
It returns the following results. Rows 2 and greater are exactly what I want and would be perfect if I didn't get the first row. How do I get a hard coded value into a column without "D"() showing up in the first row?
blank, "D"(), blank
Description, D, date
Description, D, date
Description, D, date
Thanks so much for any help that is provided.
You can use 'label' in the query string.
=query('Detailed Plan'!$A$2:$Q, "select A, D, Q where D = date label A 'Description', D 'Some value', Q 'Date' ", 0)
EDIT: if you don't need headers at all, try
=query('Detailed Plan'!$A$2:$Q, "select A, D, Q where D = date ", 0)

Resources