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

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

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)

Google Sheets - Repeat values 'n' times from a range

I am trying to execute the following in google sheets by repeating certain cell values from a range of cells (number of repetitions) and iterating it horizontally till the end of the row.
Formula used for current output:
={ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(QUERY(REPT(A1&",",A2:A4 ), ,999^99), ","))));ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(QUERY(REPT(C1&",",C2:C4 ), ,999^99), ","))));ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(QUERY(REPT(E1&",",E1:E4 ), ,999^99), ","))))}
use:
=ARRAYFORMULA(FLATTEN(SPLIT(TEXTJOIN("×", 1,
REPT({A1&"×", C1&"×", E1&"×"}, {A2:A, C2:C, E2:E})), "×")))
shorter:
=INDEX(FLATTEN(SPLIT(TEXTJOIN("×", 1,
REPT({A1, C1, E1}&"×", {A2:A, C2:C, E2:E})), "×")))
update:
=INDEX(QUERY(FLATTEN(SPLIT(QUERY(FLATTEN(QUERY(FLATTEN(
REPT("×"&{A1, C1, E1}&"×", {A2:A, C2:C, E2:E})),,9^9)),,9^9), "×")),
"where not Col1 starts with ' '"))

Joining the columns of a query result/range with a newline using sheets arrayformula

Here is the spreadsheet for testing: Click here to copy OR Click to View
I have a query result from some other source of data.
From this table I further need to merge the B and C columns with new line (char(10)) and that must be expected to look like below
But I have tried with Arrayformula and I am not successful.
=ARRAYFORMULA(query(Sheet1!A2:Z7,"Select A,B,C,D,E,F,G,H") & char(10) & query(Sheet1!A2:Z7,"select B") )
Try with an { array expression } like this:
=arrayformula(
query(
{ Sheet1!A2:H7, Sheet1!B2:B7 & char(10) & Sheet1!C2:C7 },
"select Col1, Col9, Col4, Col5, Col6, Col7, Col8",
1
)
)
...or perhaps more simply, like this:
=arrayformula(
query(
{ Sheet1!A2:A, Sheet1!B2:B & char(10) & Sheet1!C2:C, Sheet1!D2:H },
"where Col1 is not null",
1
)
)

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

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

Resources