Google Sheets: Find minimum sum in an ordered array - google-sheets

My goal is to find the smallest sum of 1 entry per column in this array.
i.e. Though column 9 has the lowest value for all rows, only one value can be used, and same for all the other columns, and I want to know what combination will produce the smallest sum.
Is there any way of doing this in Google Sheets?
Thank you!

Use this formula
=INDEX(QUERY({A3:I},"Select "&
TEXTJOIN(", ",1,ARRAYFORMULA("min(Col"&COLUMN(A3:I)&")"))),2)

Related

ARRAYFORMULA is only populating the first row

I have financial data that I am trying to summarize in a format that can be used by a line chart.
The example spreadsheet is here.
In my source data on the left, I have an entry per Date, Symbol, Account. I need to transform this data so there is a row per Date and a column for each Symbol. I will SUM Total Value regardless of account.
I found a way to pull a unique Date (see H2), and then transpose unique Symbols into columns (see I1).
I also found a way to use SUMIFS to get the aggregation I want (take a look at cell I2), but I can't figure out how to use ARRAYFORMULA to apply this value to all rows in column I.
I know I can drag my formula from I2 down to I3,I4,I.. etc, but this sheet is part of a larger project so I'd like it to auto-populate as dates are added to H.
From what I've read ARRAYFORMULA should apply the formula to multiple rows. What am I missing?
Thanks
use:
=QUERY(A1:F, "select A,sum(F) where A is not null group by A pivot B", 1)
Use formulas like this
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2, $B$2:$B, I$1)))
Add IF(H2:H="",,
Explanation
if the range is empty "" do nothing ,, else Your formula
Your Example
Cells
Formulas
I2
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2:H, $B$2:$B, I$1)))
J2
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2:H, $B$2:$B, J$1)))
K2
=ARRAYFORMULA(IF(H2:H="",,SUMIFS($F$2:$F, $A$2:$A, $H2:H, $B$2:$B, K$1)))

Finding unique numbers in Column B compared to the set in Column A: Google sheets

I have one sheet with 2 columns of ID numbers. I need a formula that will compare the numbers in Column B (about 50 of them) to the numbers in Column A (about 1000 of them). In Column C, I'd like to return the numbers that are in B but not in A.
I know I can run a duplicate search by hand, but am looking for a formula to take care of it instead. Also, highlighting is not helpful in this instance.
Thank you!
try:
=UNIQUE(FILTER(B1:B; NOT(COUNTIF(A1:A; B1:B))))

Repeating a value on the basis of count provided in another cell - Google Sheets

I'm trying to write a formula where I can generate a number n number of times where n can be the input provided by the user.
=ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(QUERY(
REPT($D2&",", $E2), ), ","))))
Ideal output
Here D2 is the value to be repeated and E2 is the number of times.
So instead of manually using this formula after each last repeated value to generate the next set of repeated values, I want to print the values in one go. I'll be really grateful, if anyone could please provide a way around to do the same. Thanks in advance.
Try this
=ARRAY_CONSTRAIN(arrayformula(query(flatten(split(rept("|"&D2:D,E2:E),"|")),"select * where Col1 is not null")),SUM(E2:E),1)
explanation
the core of the formula is
=arrayformula(iferror(split(rept("|"&D2:D,E2:E),"|")))
then, apply flatten with a limitation of rows (ARRAY_CONSTRAIN) equal to the sum of column E, and query only the rows that are not null
Try the below formula:
=ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(QUERY(
REPT(D2:D&",", E2:E), ,999^99), ","))))

Calculate Total row Averages correctly if table columns contain empty cells

I am using the following formula to calculate a Total row that calculates averages for percentages in the columns. However, since the data contains some empty values, the averages calculated is not correct.
={"TOTAL",arrayformula(transpose(query(transpose(query({(B16:M19)},"Select " & TEXTJOIN(",",true,arrayformula(if(filter($B$15:15,$B$15:15<>false),"Sum(Col" & Column($B$15:$M$15) - Column(B$15) + 1 & ")/Count(Col" & Column($B$15:$M$15) - Column(B$15) + 1 & ")"))))),"select Col2 format Col2 '0.00%'",)*100%))}
Ocassionally, I also get errors in my production spreadsheet viz.,
Unable to parse query string for Function QUERY parameter 2: AVG_SUM_ONLY_NUMERIC
or
Unable to parse query string for Function QUERY parameter 2: TypeError: Cannot read property 'get' of undefined
These go away if i coerce the range like this:
(B16:M19)+0 .... or.... N(B16:M19)
But this results in the average calculated incorrectly as the average considers the 0's that get replaced or the empty cells now. How can this be avoided and the values calculated correctly?
P.S: i had to hardcode the range as i don't know how to make this formula more dynamic. If anyone has an idea, please do share with me.
Here is the sample sheet. FTR Table
I came up with this formula, for your total values. See B23 in your Tables sheet:
=IFERROR(SUM(B16:B19)/COUNT(FILTER(B16:B19,B16:B19>0)),0)
If this tests out okay for you, I can try to see if it can be developed as an arrayformula.
It just sums each column in the table, and divides that by the count of values in the table that are greater than zero, so it excludes both blanks and zeroes in the table.

How to sum largest $n$ values in a range in Google Spreadsheet?

I have a list of values and I need to sum the largest 10 values (in a row). I found this but I can't figure it out/get it to work:
https://productforums.google.com/forum/#!topic/docs/A5jiMqkRLYE
let's say you want to sum the 10 highest values of the range E2:EP
then try:
=sumif(E2:P2, ">="&large(E2:P2,10))
and see if that works ?
EDIT: Maybe this is a better option ? This will only sum the 10 outputted by the array_constrain. Will only work in the new google sheets, though..
=sum(array_constrain(sort(transpose($A3:$O3), 1, 0), 10 ,1))
Can you see if this works ?
This works in old google sheets too:
sum(query(sort(transpose($A3:$O3), 1, false), "select * limit 10"))
Transpose puts the data in a column, sort sorts the data in a descending order and then query selects first 10 numbers.
Unfortunately, replacing sort with "order by" in a query statement does not work, because you can not reference a column in a range returned by transpose.
The sortn function seems to be just what you need.
From the documentation linked above, it "[r]eturns the first n items in a data set after performing a sort." The data set does not have to be sorted. It takes a bunch of optional parameters as it can sort on multiple columns.
SORTN(range, [n], [display_ties_mode], [sort_column1, is_ascending1], ...)
The interesting ones for your case are n, sort_column1, and is_ascending1. Specifically, your required formula would be
sum(sortn(transpose(A3:O3), 10, 0, 1, false)))
Some notes:
This assumes your data in A3:O3. You can replace it with your range.
transpose converts the data row to a data column as required by sortn.
10 is n, indicating the number of values that you require.
0 is the value for display_ties_mode. We are ignoring this value.
1 is the value of sort_column1, telling that we want to sort the first column (after transpose).
false tells sortn to sort descending and thus pick the largest values. The default is to pick the smallest.

Resources