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

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))

Related

Google sheet formula to group by variables

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")&"'", )

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)

Is there a way to use MAX() with automated multiple range values?

I need the range values to go from A:A, B:B, ..., [Current Column]. it needs to be dynamic for multiple columns, i.e Column D be =MAX(A:A, B:B, C:C), while Column E will be =MAX(A:A, B:B, C:C, D:D).
=MAX(COUNTIF(A:A, “*Text*”), COUNTIF(B:B, “*[Text String]*”), ..., [until current COLUMN() - 1])
Or something like
=MAX(
Loop ( COLUMN() - 1 )
{
COUNTIF(
INDIRECT(
ADDRESS(3, [Loop Iteration]) & ":" & ADDRESS([Dynamic Number], [Loop Iteration])
)
, “*[Text String]*” )
}
)
dragging solution:
=MAX(INDIRECT(ADDRESS(3, 1)&":"&ADDRESS(ROWS(A3:A), COLUMN()-1)))
non-dragging automated solution:
=INDEX(QUERY(QUERY(SPLIT(FLATTEN(COLUMN(A:E)&"×"&IFNA(HLOOKUP(
IF(FLATTEN(COLUMN(A:E)-1)>=COLUMN(A:E),,COLUMN(A:E)-(FLATTEN(COLUMN(A:E))-1)),
QUERY(SPLIT(FLATTEN(IF(A3:E="",, COLUMN(A:E)&"×"&A3:E)), "×"),
"select max(Col2) pivot Col1")*1, 2, 0))), "×"),
"select max(Col2) pivot Col1"), "offset 1", 0))

Import function using QUERY + REGEXMATCH doesn't work with excluding & select parameters

I would like to import a sheet by 2 different filter function. The first one need to include every row where the value of column 14 is equal to 1, the second one need to exclude rows where column H contains one of those strings "alc" "alcool" "vin". I've seen it in previous questions here google-sheet-query-matches-function-doesnt-exclude-strings. However when i try to add the first filter into my previous formula it doesn't work, here is my formula :
=FILTER(
QUERY(
IMPORTRANGE("URL"; "Sheet!A:BE");
"SELECT Col1, Col3, Col4, Col26, Col8, Col30, Col40, Col41, Col44, Col45, Col49 WHERE Col14 = "1""
);
NOT(REGEXMATCH(IMPORTRANGE("URL"; "Sheet!H:H"); "(?i)alc|vin|alcool"))
)
I try both using and not using "" as the values of my column are numbers but none of them work
use:
=FILTER(QUERY(IMPORTRANGE("1RV1-mxrd4wjVBw8hNu1yQAOZvlaO0jD5Ar5AArg-7QE";
"wc-product-export-27-11-2020-1606486738987!A:BE");
"select Col1,Col3,Col4,Col26,Col8,Col30,Col40,Col41,Col44,Col45,Col49
where Col14 = 1");
NOT(REGEXMATCH(QUERY(IMPORTRANGE("1RV1-mxrd4wjVBw8hNu1yQAOZvlaO0jD5Ar5AArg-7QE";
"wc-product-export-27-11-2020-1606486738987!A:BE");
"select Col8
where Col14 = 1"); "(?i)alcool|vin|alc")))
or shorter:
=INDEX(QUERY(IMPORTRANGE("1RV1-mxrd4wjVBw8hNu1yQAOZvlaO0jD5Ar5AArg-7QE";
"wc-product-export-27-11-2020-1606486738987!A:BE");
"select Col1,Col3,Col4,Col26,Col8,Col30,Col40,Col41,Col44,Col45,Col49
where Col14 = 1
and not lower(Col8) contains "&JOIN(" and not lower(Col8) contains "; "'"&SPLIT(
"alcool|vin|alc"; "|")&"'")))

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