Google sheet formula to group by variables - google-sheets

I'm trying to learn Googlesheet arrayformulas
I've created a sheet # https://docs.google.com/spreadsheets/d/1F9tuDleDn4dqg0WEkChl6dMChgWRs-KAPzQJE09tAJM/edit?usp=sharing
Please help me figure out how to:
Using ArrayFormulas:
Sort table by latest to oldest Date
In Position Column, if paranthesis exists, show paranthesis value in new column
In Position Column, If "Major" or "Minority" or "Director" exists then null, show remaining value in new column
How do I group into new table "Date", "Bonus", "Bonus Amount" column by specific Name (e.g. Johnson D. Zoe) and sort by latest date?
How do I group into new table, "Date", "Bonus", "Bonus Amount", "Name", "Position" of all records who have Bonus = Y within the Last 30 days vs. within the last Year? ("2023")

1, 2 and 3:
=SORT({A2:D13,
TRIM(REGEXREPLACE(E2:E13, "Major|Minority|Director|\(.*\)", )),
IFNA(REGEXEXTRACT(E2:E13, "\((.*)\)"))}, 1, 0)
4:
=QUERY(SORT(A2:D13, 1, 0),
"select Col1,Col2,Col3 where Col4 = 'Johnson D. Zoe'", )
5:
=QUERY(SORT({A2:D13,
TRIM(REGEXREPLACE(E2:E13, "Major|Minority|Director|\(.*\)", )),
IFNA(REGEXEXTRACT(E2:E13, "\((.*)\)"))}, 1, 0),
"where Col2 = 'Y' and year(Col1) = 2023", )
and:
=QUERY(SORT({A2:D13,
TRIM(REGEXREPLACE(E2:E13, "Major|Minority|Director|\(.*\)", )),
IFNA(REGEXEXTRACT(E2:E13, "\((.*)\)"))}, 1, 0),
"where Col2 = 'Y' and Col1 >= date '"&TEXT(TODAY()-30, "e-m-d")&"'", )

Related

Arrayformula to replace sumifs based on criteria - grouping data into categories/types

In this spreadsheet
Cols G and H give the total of each Account Type, from the data (A1:D)
If sum Dr - sum Cr > 0, then only this is shown in col G.
=if(sumifs(C:C,A:A,F2)-sumifs(D:D,A:A,F2)>0, sumifs(C:C,A:A,F2)-sumifs(D:D,A:A,F2),"")
If sum Cr - sum Dr > 0, then only this is shown in col H
=if(sumifs(D:D,A:A,F2)-sumifs(C:C,A:A,F2)>0, sumifs(D:D,A:A,F2)-sumifs(C:C,A:A,F2),"")
I am looking for an array formula which replaces these summifs formula, so that if either new Account Types (e.g. Type E, Type F etc) are added or new rows of data are added, then the formula would automatically calculate the sum Dr or sum Cr, instead of having to copy the formula down
try this in cell G2:
=BYROW(F2:F,LAMBDA(fx,IF(fx="",,LAMBDA(cx,dx,{IF(cx-dx>0,cx-dx,),IF(dx-cx>0,dx-cx,)})(SUMIF(A:A,fx,C:C),SUMIF(A:A,fx,D:D)))))
-
Use query(), like this:
=arrayformula(
lambda(
aggregated,
lambda(
account, balance,
{
"Account Type", "Dr", "Cr";
account,
if( balance >= 0, balance, iferror(1/0) ),
if( balance < 0, -balance, iferror(1/0) )
}
)(
query(aggregated, "select Col1", 0),
query(aggregated, "select Col2", 0)
)
)(
query(
A3:D,
"select A, sum(C) - sum(D)
where A is not null
group by A
label sum(C) - sum(D) '' ",
0
)
)
)
You can set a QUERY that finds both entire columns like this:
=QUERY(A2:D,"SELECT A,SUM(C)-SUM(D),SUM(D)-SUM(C) where A is not null group by A",1)
And check with LAMBDA if there are negative values and change them to null:
=LAMBDA(a,INDEX(IF(a<0,,a)))(QUERY(A2:D,"SELECT A,SUM(C)-SUM(D),SUM(D)-SUM(C) WHERE A is not null group by A",1))
If the values in your result table are just for display, you can use the QUERY format clause to hide negative values, such that the whole thing can be generated within a single QUERY:
=QUERY(A2:D,"select A,sum(C)-sum(D),sum(D)-sum(C) where A is not null group by A label sum(C)-sum(D) 'Dr',sum(D)-sum(C) 'Cr' format sum(C)-sum(D) '0;',sum(D)-sum(C) '0;'",1)

Calculate average duration between two dates in a pivot table calculated field

I want to calculate in a pivot table the average duration between two changes (dates) for each item.
Here is the sheet that contains datas:
Item
Changed on
item3
2023-01-25
item2
2022-10-12
item3
2022-08-15
item3
2022-03-06
item2
2021-12-18
item1
2021-06-28
I need a pivot table to calculate the average duration of each item, like this:
Item
Avg. time between two changes
item1
no data
item2
298
item3
162.5
try:
=INDEX(LAMBDA(a, b, QUERY(QUERY({a, IFNA(VLOOKUP(a&"×"&
COUNTIFS(a, a, ROW(a), "<="&ROW(a))-1, {a&"×"&
COUNTIFS(a, a, ROW(a), "<="&ROW(a)), b}, 2, )-b)},
"select Col1,avg(Col2) where Col2 is not null group by Col1"),
"offset 1", ))(A:A, B:B))
=INDEX(LAMBDA(a, b, {SORT(UNIQUE(FILTER(a, a<>""))),
IFNA(VLOOKUP(SORT(UNIQUE(FILTER(a, a<>""))),
QUERY(QUERY({a, IFNA(VLOOKUP(a&"×"&
COUNTIFS(a, a, ROW(a), "<="&ROW(a))-1, {a&"×"&
COUNTIFS(a, a, ROW(a), "<="&ROW(a)), b}, 2, )-b)},
"select Col1,avg(Col2) where Col2 is not null group by Col1"),
"offset 1", ), 2, ), "no data")})(A:A, B:B))
=INDEX(LAMBDA(a; b; {SORT(UNIQUE(FILTER(a; a<>"")))\
IFNA(VLOOKUP(SORT(UNIQUE(FILTER(a; a<>"")));
QUERY(QUERY({a\ IFNA(VLOOKUP(a&"×"&
COUNTIFS(a; a; ROW(a); "<="&ROW(a))-1; {a&"×"&
COUNTIFS(a; a; ROW(a); "<="&ROW(a))\ b}; 2; )-b)};
"select Col1,avg(Col2) where Col2 is not null group by Col1");
"offset 1"; ); 2; ); "no data")})(A:A; B:B))

How to sum with weeknum condition in google sheets

i have data and want to sum it with weeknum in every name condition, but i can't do that it always error parse or empty value.
this my query
=query(A1:D8,"select sum(D) where D='"&G2&"' and B='"&H1&"' label sum(D)'' ")
=query(A1:D8,"select sum(D) where D='"&G2&"' and weeknum(A)=weeknum('"&H1&"') label sum(D)'' ")
this my link
https://docs.google.com/spreadsheets/d/1AHnu-IrFQuh7scbtoQriB_7frhvLUYfO9gIX2yqiXBY/edit?usp=sharing
use:
=QUERY(A:D, "select C,sum(D) where B = "&H1&" group by C label sum(D)''", 0)
if you want 0 values use:
=ARRAYFORMULA({UNIQUE(FILTER(C2:C, C2:C<>"")),
IFNA(VLOOKUP(UNIQUE(FILTER(C2:C, C2:C<>"")),
QUERY(A:D, "select C,sum(D) where B = "&H1&" group by C label sum(D)''", 0), 2, 0), 0)})

Google Sheets Cartesian product with transposed columns

Is there recommended ways to convert table in A2:F6 to H2:K10?
Thanks.
Here is the sample Google Sheet https://docs.google.com/spreadsheets/d/1QOZVMIRvM_9lhvAflhQtA9H6KOpfHycPo5qgF1y-GzA/edit?usp=sharing
There is a relatively simple way to do than using FLATTEN:
={
ARRAYFORMULA(A1:C1), "Product";
ARRAYFORMULA(
VLOOKUP(
FILTER(
FLATTEN(ROW(D2:F) + 0 * ISBLANK(D2:F)),
FLATTEN(D2:F <> "")
),
FILTER(
{ROW(A2:A), A2:C},
A2:A <> ""
),
{2, 3, 4},
0
)
),
FILTER(
FLATTEN(D2:F),
FLATTEN(D2:F <> "")
)
}
Not sure why ARRAYFORMULA(ROW(D2:F)) gives row wise row numbers and not a 2d matrix with the row numbers. Could've remove 0 * ISBLANK(D2:F) otherwise...
Please use the following query formula:
=QUERY(QUERY({A:D;A2:C,E2:E;A2:C,F2:F},"where Col1 is not null order by Col1"), "where Col4 is not null")
Functions used:
QUERY

Sum of matrix values, conditional, with infinite rows and columns

I have 2 sheets: "Planning" and "Utilization" (example)
Planning: Employees assigned to projects for each week. Some projects are not fixed but need to be simulated (checkbox = true).
Utilization: Shows the utilization of each employee for each week. Only rows with:
a) no checkbox in Planning!A2:A
b) rows with checkbox checked
c) rows with project name in Planning!B2:B are to be considered.
I'd like to have a formula in Utilization!B2 that would calculate the sums for Utilization!B2:E4. With infinite rows and columns in Planning sheet.
try:
=QUERY(QUERY({Planning!A:H};
"select Col3,sum(Col5),sum(Col6),sum(Col7),sum(Col8)
where not Col1 = FALSE
and Col3 is not null
group by Col3"; 0);
"offset 1"; 0)
UPDATE:
=ARRAYFORMULA(QUERY(QUERY({Planning!A:Z};
"select Col3,"&
TEXTJOIN(","; 1; IF(Planning!E1:1="";;"sum(Col"&COLUMN(E:Z)&")"))&"
where not Col1 = FALSE
and Col3 is not null
group by Col3"; 0);
"offset 1"; 0))
FIX for 'get' error:
=ARRAYFORMULA(QUERY(QUERY({Planning!A:D\Planning!E:Z*1};
"select Col3,"&
TEXTJOIN(","; 1; IF(Planning!E1:1="";;"sum(Col"&COLUMN(E:Z)&")"))&"
where not Col1 = FALSE
and Col3 is not null
group by Col3"; 0);
"offset 1"; 0))
For infinite rows and Columns, I like to use OFFSET() so you might try this formula in A1 on a new tab.
=ARRAYFORMULA({QUERY(QUERY({Planning!A:D\N(OFFSET(Planning!E1;;;ROWS(Planning!E:E);COUNTA(Planning!E1:1)))};"select Col3, "&TEXTJOIN(",";TRUE;"SUM(Col"&SEQUENCE(COUNTA(Planning!E1:1);1;5)&")")&" where Col2 is not null group by Col3";0);"offset 1";0)})

Resources