How to sum up arrays to then find the minimum - google-sheets

Here's my problem: I have 2 sheets in my document (lets call them Sheet 1 and Sheet 2). They contain similar stuff and both look like this (Names may differ, as well as values):
Column A, C, D and F contain times (in m:ss).
Column B and E both calculate the time-difference between NameX and NameY and add ">, < or ~ ~" depending on the actual difference (ignore the coloring).
Now here comes my problem: I want to find 3 minima (on Sheet 3).
Minimum 1 is easy, as I can just use this function (it automatically filters out column B and E):
MIN('Sheet 1'!A2:F2, 'Sheet 2'!A2:F2)
Minimum 2 and 3 are were I struggle.
Minimum 2: Using the example values, I want to find the minimum of (1:01+1:02), (1:02+1:05), (1:01+1:01) and (1:01+1:02) (+ whatever times are on sheet 2). Result should be 2:02.
Minimum 3: Again, using the example values, I want to find the minimum of (1:01+1:02+1:03), (1:02+1:05+0:30), (1:01+1:01+1:12) and (1:01+1:02+2:02) (+ whatever times are on sheet 2). Result should be 2:37.
I am currently using this formula (for minimum 3):
=MIN(
IFERROR(FILTER(IFERROR(ARRAYFORMULA({'Sheet 1'!A2:F2}+{'Sheet 1'!A3:F3}+{'Sheet 1'!A4:F4})),
IFERROR(ARRAYFORMULA({'Sheet 1'!A2:F2}+{'Sheet 1'!A3:F3}+{'Sheet 1'!A4:F4}))<>0)),
IFERROR(FILTER(IFERROR(ARRAYFORMULA({'Sheet 2'!A2:F2}+{'Sheet 2'!A3:F3}+{'Sheet 2'!A4:F4})),
IFERROR(ARRAYFORMULA({'Sheet 2'!A2:F2}+{'Sheet 2'!A3:F3}+{'Sheet 2'!A4:F4}))<>0))
)
Some notes: The inner IFERROR-function is needed to filter out errors that obviously occur when trying to add up column B and E. FILTER-function filters out columns that are empty (there's none in this example). The second IFERROR-function filters out FILTER-functions that return an error when they get no input at all (all columns in a sheet are empty). I want to filter of these since I don't want to get 0:00 as result
My problem is this: In my actual sheet I have 11 sheets with 16 rows to add up, but I don't want to use the formula above and create an insane monster of a formula that would x-times as long as the formula above.
So my question is: Is there an easier way to solve this problem for mimimum 3 (and therefore 4, 5, 6 ...) that I'm not seeing?

It's a little monstrous, but this might work:
=MIN(FILTER({
MMULT(SEQUENCE(1,ROWS(Sheet1!A2:F),1,0),N(Sheet1!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet2!A2:F),1,0),N(Sheet2!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet3!A2:F),1,0),N(Sheet3!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet4!A2:F),1,0),N(Sheet4!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet5!A2:F),1,0),N(Sheet5!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet6!A2:F),1,0),N(Sheet6!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet7!A2:F),1,0),N(Sheet7!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet8!A2:F),1,0),N(Sheet8!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet9!A2:F),1,0),N(Sheet9!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet10!A2:F),1,0),N(Sheet10!A2:F));
MMULT(SEQUENCE(1,ROWS(Sheet11!A2:F),1,0),N(Sheet11!A2:F))},
{1,0,1,1,0,1}))

Related

Can change shape of range with ARRAYFORMULA() in Google Sheets?

My intention is to convert a single line of data into rows consist of a specific number of columns in Google Sheets.
For example, starting with the raw data:
A
B
C
D
E
F
1
id1
attr1-1
attr2-1
id2
attr2-1
attr2-2
And the expected result is:
(by dividing columns by three)
A
B
C
1
id1
attr1-1
attr1-2
2
id2
attr2-1
attr2-2
I already know that it's possible a bit manually, like:
=ARRAYFORMULA({A1:C1;D1:F1})
But I have to start over with it every time the target range is moved OR the subset size needs to be changed (in the case above it was three)!
So I guess there will be a much more graceful way (i.e. formula does not require manual update) to do the same thing and suspect ARRAYFORMULA() is the key.
Any help will be appreciated!
I added a new sheet ("Erik Help") where I reduced your manually entered parameters from two to one (leaving only # of columns to be entered in A2).
The formula that reshapes the grid:
=ArrayFormula(IFERROR(VLOOKUP(SEQUENCE(ROUNDUP(COUNTA(7:7)/A2),A2),{SEQUENCE(COUNTA(7:7),1),FLATTEN(FILTER(7:7,7:7<>""))},2,FALSE)))
SEQUENCE is used to shape the grid according to whatever is entered in A2. Rows would be the count of items in Row 7 divided by the number in A2 (rounded to the nearest whole number); and the columns would just be whatever number is entered in A2.
Example: If there are 11 items in Row 7 and you want 4 columns, ROUNDUP(11/4)=3 rows to the SEQUENCE and your requested 4 columns.
Then, each of those numbers in the grid is VLOOKUP'ed in a virtual array consisting of a vertical SEQUENCE of ordered numbers matching the number of data pieces in Row 7 (in Column 1) and a FLATTENed (vertical) version of the Row-7 data pieces themselves (in Column 2). Matches are filled into the original SEQUENCE grid, while non-matches are left blank by IFERROR
Though it's a bit messy, managed to get it done thanks to SEQUENCE() function anyway.
It constructs a grid by accepting number of rows/columns input, and that was exactly I was looking for.
For reference set up a sheet with the sample data here:
https://docs.google.com/spreadsheets/d/1p972tYlsPvC6nM39qLNjYRZZWGZYsUnGaA7kXyfJ8F4/edit#gid=0
Use a custom formula
Although you already solved this. If you are doing this kind of thing a lot, it could be beneficial to look into Apps Script and custom formulas.
In this case you could use something like:
function transposeSingleRow(range, size) {
// initialize new range
let newRange = []
// initialize counter to keep track
let count = 0;
// start while loop to go through row (range[0])
while (count < range[0].length){
// add a slice of the original range to the new range
newRange.push(
range[0].slice(count, count + size)
);
// increment counter
count += size;
}
return newRange;
}
Which works like this:
The nice thing about the formula here is that you select the range, and then you put in a number to represent its throw, or how many elements make up a complete row. So if instead of 3 attributes you had 4, instead of calling:
=transposeSingleRow(A7:L7, 3)
you could do:
=transposeSingleRow(A7:L7, 4)
Additionally, if you want this conversion to be permanent and not dependent on formula recalculation. Making it in run fully in Apps Script without using formulas would be neccesary.
Reference
Apps Script
Custom Functions

Converting formula to ARRAYFORMULA issues with SUM and INDEX

I have a scoring spreadsheet for a competition I'm working on. Competitors' place/rank are converted into points towards the overall series based on a chart of corresponding values. For ties, the sum of the points covered by all of the tied places are split evenly among the tied competitors (i.e. 2-way tie for 3rd; if 3rd usually gets 10 points and 4th usually gets 8, these competitors will receive (10+8)/2 (2 being the # of tied competitors), so they each receive 9 points).
I have a formula which does this exact calculation:
=IFERROR(IF(ISBLANK($A4:$A),,SUM(INDEX(SeriesPoints, E4:E):INDEX(SeriesPoints, MIN(E4:E + COUNTIF(E$4:E, E4:E) - 1, ROWS(SeriesPoints)))) / COUNTIF(E$4:E, E4:E), 0))
Where 'SeriesPoints' is a 2 column array; column 1 is the places/ranks (1:125) and column 2 is their corresponding point values. Column 'E' is the competitors' rank from the competition.
I have been unable to convert this formula to an ARRAYFORMULA() so I can avoid dragging it down the entire sheet (possibly up to 1000+ competitors over the series).
I'm mildly proficient with MMULT(), so I understood that would be a good approach for switching out SUM(), however, I haven't been able to create a matrix of the values to be summed.
INDEX():INDEX() doesn't work with ARRAYFORMULA() so I've tried switching to VLOOKUP(). With VLOOKUP() I've been able to produce the start and end values of the range of values for a tie, but not the full list. For example, if there is a 3-way tie for 4th, I can produce the respective points for 4th and 6th (the bounds of the tie).
In an attempt to list out even just the numbers from 4:6, I've hit a wall converting what would be a simple ROW() or SEQUENCE() formula to a matrix/array.
The following formula produces an array of the upper and lower bounds of ties or the single place should there be no tie, although the single place gets repeated.
=ARRAYFORMULA(IF(COUNTIF(E$4:E,E4:E)=1,E4:E,{E4:E,E4:E+COUNTIF(E$4:E,E4:E)-1}))
I'm assuming if I can get VLOOKUP({#:#}) to fill properly, I'll be where I need to be.
From here, I feel confident in my abilities to wrap a VLOOKUP() for the actual point values, an MMULT() to sum across these rows for the total, then a simple division to produce the correct point value.
Spreadsheet: https://docs.google.com/spreadsheets/d/1lpNewR3p4i7ZHmlFGLlG1tLuxgO-6onSeH8mWTeclBw/edit?usp=sharing
Currently, my workspace is off to the right. The original formula is in F4 and my test codes are working on column G instead of E.
So for sample placements of 1,1,3,3,3,6,7,8 and sample points values of 1000, 850,738,663,633,603,573,550 I expect the output to be 925 for the two 1st place tied competitors, 678 for the tied 3rd places, 603 for 6th, 573 for 7th, and 550 for 8th.
I'd appreciate any and all help!
=ARRAYFORMULA(IFERROR(IFERROR(VLOOKUP(G4:G, QUERY({INDIRECT("G4:G"&counta(A4:A)+3),
VLOOKUP(ROW(INDIRECT("A1:A"&COUNTA(A4:A))), SeriesPoints, 2, 0)},
"select Col1,sum(Col2) group by Col1 label sum(Col2)''", 0), 2, 0))/
IFERROR(VLOOKUP(G4:G, QUERY(G4:G,
"select G,count(G) where G is not NULL group by G label count(G)''", 0), 2, 0))))

Google Sheets - Embedded arrays with multiple columns under each other

I'd like to insert 2 column wide fields under each other. I tried with embedded arrays but was not successful.
So basically from:
a 1 e 5
b 2 f 6
c 3
I would like to get:
a 1
b 2
c 3
e 5
f 6
I tried with
={{A:A,B:B};{C:C,D:D}}
but could not get it working, however
={{A:A,B:B},{C:C,D:D}}
put the columns the same as they were so its intresting that with ; its not working.
The blocks are always 2 column wide but the rows are different length
Thanks for your help in advance!
Try:
=filter({A:B;C:D},{A:A;C:C}<>"")
This will return rows where Columns A or C are not blank.
Just under c assuming a is in A1:
=ArrayFormula(C1:D2)
You're not going to find a clean built-in formulaic solution to this one that doesn't utilize some sort of built-in magic auto expansion (like pnuts's answer). Here is my approach using OFFSET that will also work in Microsoft Excel.
In two columns, copy this formula.
=OFFSET($A$1,(ROW()-ROW($G$1))/2,IF(MOD(ROW()-ROW($G$1),2)=1,2,0))
In the second column, modify the formula, adding 1 to the column offset parameter:
=OFFSET($A$1,(ROW()-ROW($G$1))/2,1+IF(MOD(ROW()-ROW($G$1),2)=1,2,0))
where $A$1 is replaced with the address of the top left of your range and $G$1 is the starting location of your output range. This should be resistant to auto-update of formulas from range insertions and deletions (which I despise butchering my formulae and conditional formatting rules) by using only the bare number of references, which are all absolute.
This works by dividing the row offset from your starting position by 2 and rounding down (via an implicit cast to integer when used as a parameter to the OFFSET function) to get the row number of your input range. Then it shifts over 2 columns on every odd row to get data from the second column pair.
Note this is not a size-aware function, so it interweaves the second column pair:
a 1
e 5
b 2
f 6
c 3

Google Query Language for sum of result using cell reference in query

Hello and thanks for your help. I'm new to GQL but have good SQL experence and think I may be missing something small.
I have 2 sheets i'm working with
Main sheet
Colum G
InstanceID
i-554532f4693fc6186
i-09554fcda5f2f3262
i-0047551ae514412d5
-
Data Sheet
Colum A Colum B
i-554532f4693fc6186 10.12
i-554532f4693fc6186 12.12
i-554532f4693fc6186 13.12
i-554532f4693fc6186 17.12
i-554532f4693fc6186 30.12
I am trying to write a query that will find all the rows that match the Instance ID in column G against the datasheet Column A and return the AVG of all the matches in column B, the top 5 max, and top 5 min.
I'm finding that I can't point the query to a cell for referencing the instance ID. Is there a way?
I'm using this to try to get the max and it works for 1 but I ned the top 5 or any number.
=sort(query('HeC-Metrics'!A:B,"select max(B) Where A = 'i-044532f4693fc6186'"))
I'm OK needing to do different queries for each of the required results, AVG, min, max. I would also like to reference the cell in the G column so I don't have to manually enter the InstanceID.
Thanks your time.
Stephen
So it's just a case of getting the right syntax to use a cell value as a match in the query
=query(Sheet2!A:B,"select avg(B) where A='"&G2&"' group by A label avg(B) ''",1)
Note that you don't really need the group by if you already have a list of distinct ID's to compare against, but you can't have an aggregate like avg without it.
To get the bottom 5, you can use filter & sortn
=transpose(sortn(filter(Sheet2!B:B,Sheet2!A:A=G2),5))
(I have transposed the result to get it in a row (row 2) instead of a column)
or you could use a query
=transpose(query(Sheet2!A:B,"select B where A='"&G2&"' order by B limit 5 label B '' ",1))
Similarly to get the top 5 you could use
=transpose(sortn(filter(Sheet2!B:B,Sheet2!A:A=G2),5,,1,false))
or
=transpose(query(Sheet2!A:B,"select B where A='"&G2&"' order by B desc limit 5 label B '' ",1))
This begs the question of whether you could get these results (a) without needing a list of distinct values and (b) in a single array formula without copying down.
You could certainly get the distinct ID's and averages straight away from a query. Getting the top or bottom n values from a number of groups is much more difficult. I have attempted it in a previous question, but it requires a long and unwieldy formula.

Count selected elements for each line and create an arrayformula that groups by number of counts

We have asked users:
What to do with the money?
[ ] paint the bridge
[ ] rebuild the school
[ ] keep the money
[ ] Other : [____________________]
Here is the spreadsheet with their answers:
A B
1 Name Choices
2 Lilia paint the bridge, rebuild the school, keep the money
3 Paul rebuild the school, paint the bridge, do something else
4 Margerite keep the money, I don't know, do what you want
5 John paint the bridge
...
800
I want a formula that output the number of official choices (excluding other) picked per user.
With the first 4 rows of data, the formula would output this table:
D E
Nbr of choices a user made Frequency (Nbr of users who made these choices)
0 0
1 2
2 1
3 1
Couldn't find a way to get this right from a single formula. For a starter, I wanted to split each line (of B2: B) by "," but couldn't find a way to apply a fn (split) to each line in an formula...
Even with 800 rows of data (B2:B), the resulting table (D2:E5) would always be 4 rows long plus titles (and two column wide)
I could do this in C2, and replicate manually with the "+" corner icon...
=countif(B2;"*rebuild the school*")+countif(B2;"*keep the money*")+countif(B2;"*paint the bridge*")
And then do in E2:
=arrayformula(countif(C2:C;D2:D5))
But I'd like to generate the table of frequencies in one formula, without any manual action (without C column).
So I am looking for a way to "map" the first function to each row, put this in the second fn.
ANSWER by Akshin Jalilov EXPLAINED
This is the answer by Akshin Jalilov, but shorter (and with international notations)
=ARRAYFORMULA(COUNTIF(ARRAYFORMULA(IF(B2:B="";;COUNTIF(ARRAYFORMULA
(IFERROR(IF(FIND("paint the bridge";B2:B);Row(B2:B);0)));"="&row(B2:B))
+COUNTIF(ARRAYFORMULA(IFERROR(IF(FIND(
"rebuild the school";B2:B);Row(B2:B);0)));"="&row(B2:B))
+COUNTIF(ARRAYFORMULA(IFERROR(IF(FIND(
"keep the money";B2:B);Row(B2:B);0)));"="&row(B2:B))));"="&D2:D5))
Step1:
IF(FIND("rebuild the school";B2:B);Row(B2:B);0)
This means, for each row (B2:B) find "rebuild the school". If you find it, return the number of the row, otherwise, return 0.
Step2:
=ARRAYFORMULA(IFERROR(Step1))
Wrap this in an ARRAYFORMULA so that you return the results for each row.
I think IFERROR is there to prevent an error from stopping the process.
Step3:
=ARRAYFORMULA(IF(B2:B="";;COUNTIF(ARRAYFORMULA(IFERROR(IF(FIND("paint the bridge";B2:B);Row(B2:B);0)));"="&row(B2:B))+countif(Step2)+countif(ARRAYFORMULA(IFERROR(IF(FIND("keep the money";B2:B);Row(B2:B);0)));"="&row(B2:B))))
This will count valid votes made by each users. This is equivalent to C2 formula referred in my manual process. But is it now part of a single global formula.
Step4:
Lastly, the rest of the formula counts frequencies of each voting count possibilities.
I know this formula is large but this is the closest I got to what you want.
Now to make it easy, name your responses range "Responses". I assume it is B2:B.
Here is the formula:
=ARRAYFORMULA(Countif(ARRAYFORMULA(IF(Responses="",,COUNTIF(VLOOKUP(row(Responses),({ARRAYFORMULA(Row(Responses)),ARRAYFORMULA(IFERROR(IF(FIND("paint the bridge",Responses),Row(Responses),0))),ARRAYFORMULA(IFERROR(IF(FIND("rebuild the school",Responses),Row(Responses),0))),ARRAYFORMULA(IFERROR(IF(FIND("keep the money",Responses),Row(Responses),0)))}),2),"="&row(Responses))+COUNTIF(VLOOKUP(row(Responses),({ARRAYFORMULA(Row(Responses)),ARRAYFORMULA(IFERROR(IF(FIND("paint the bridge",Responses),Row(Responses),0))),ARRAYFORMULA(IFERROR(IF(FIND("rebuild the school",Responses),Row(Responses),0))),ARRAYFORMULA(IFERROR(IF(FIND("keep the money",Responses),Row(Responses),0)))}),3),"="&row(Responses))+COUNTIF(VLOOKUP(row(Responses),({ARRAYFORMULA(Row(Responses)),ARRAYFORMULA(IFERROR(IF(FIND("paint the bridge",Responses),Row(Responses),0))),ARRAYFORMULA(IFERROR(IF(FIND("rebuild the school",Responses),Row(Responses),0))),ARRAYFORMULA(IFERROR(IF(FIND("keep the money",Responses),Row(Responses),0)))}),4),"="&row(Responses)))),"="&D2:D5))
Here is an example if how it works. I am not sure which one exactly you wanted so added both

Resources