Grouping Data with SUM of Value No Greater than a Specific Number (Google Sheets) - google-sheets

I have series of numbers as you can see in the following table in a Google Spreadsheet:
I want each name to form a group with other names next to it with sum of value no more than 26. If a sum is greater than 26, then it will be just forced to form to another group with another name.
So that it might look like this:
In this example, A and B can form a group together since the total sum of A and B is no more than 26. C is forced to form a group just by itself since if we add D with C, the sum will be more than 26. D is also forced to form a group just by itself since if we add D with E, the sum will be more than 26.
How can I create a table with Array Formula so it looks like that?
This is the link for the sample case:
https://docs.google.com/spreadsheets/d/1BXfJrfbtdsGP4fr9dsKxFq54nM4PnKLHauAOy5UAPbY/edit#gid=0
The sample case is in Sheet2
Column A and B are given condition
Column D to G are the desired outcome
Edited: sample case is changed from Sheet1 to Sheet2 so that the problem can be seen more clearly. Sheet1 is hidden to prevent confusion.

You can do it using two formulas:
for group in col G =ArrayFormula(IF(B2:B,INT(MMULT(TRANSPOSE((ROW(B2:B)<=TRANSPOSE(ROW(B2:B)))*B2:B),SIGN(B2:B))/26)+1,))
for group sum in col F =ArrayFormula(IF(LEN(B2:B),SUMIF(G2:G,G2:G,B2:B),))
or using one formula in col F which will be slower:
=ArrayFormula(IF(LEN(B2:B),{SUMIF(INT(MMULT(TRANSPOSE((ROW(B2:B)<=TRANSPOSE(ROW(B2:B)))*B2:B),SIGN(B2:B))/26)+1,INT(MMULT(TRANSPOSE((ROW(B2:B)<=TRANSPOSE(ROW(B2:B)))*B2:B),SIGN(B2:B))/26)+1,B2:B),INT(MMULT(TRANSPOSE((ROW(B2:B)<=TRANSPOSE(ROW(B2:B)))*B2:B),SIGN(B2:B))/26)+1},))

Related

Count of items in column A not in column B (Google sheets)

I have two columns of numbers in Google sheets. I’m trying to find a formula to give me a count of items in column A that are not in column B. The numbers in the columns are descending and unique in each column but can be duplicated across columns. The columns can also have different amounts of items in them.
Column A has 5 4 3 1
Column B has 4 2 1
The answer is this case would be 2 as the numbers 5 and 3 in column A are not in column B.
I’ve tried using sum, if and countif but can’t come up with a solution. Also not sure if this would be an array formula or not.
Without a helper column you can use reduce and lambda functions.
=reduce(0, A:A, LAMBDA(prev, a_value, prev + if(not(a_value = ""), iferror(min(0, match(a_value, B:B, false)), 1), 0)))
Fill column c with this formula:
=iferror(min(0,match(A1,B:B,false)),1)
If A1 is in column b the match function will return an value, which is then reduced to 0 by the min function. Otherwise match will return an error, and the iferror will return 1.
Then you just need to sum column c.

Google Sheets formula to lookup value for all rows in another sheet by column names and returning value by column name

I have a Sheet1 with data like this:
one
two
three
four
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
I have Sheet2 with data like this:
alpha
value 1
c
k
g
c
For each row in Sheet2, I want to look up Sheet2.A2:A in Sheet1 according to a lookup column name in Sheet1 and returning values from the associated row in Sheet1 by return column name in Sheet1.
So, a few examples:
Lookup Sheet2.A2:A in Sheet1.three and return Sheet1.one
Lookup Sheet2.A2:A in Sheet1.two and return Sheet1.four
The idea is the formula would specify the lookup column name and return column name and I'd just change it for each lookup I need to do.
Imagine the formula was something like:
=ARRAYFORMULA(
SOMEFORMULA(
A2:A, # lookup this value
GETCOLUMN(Sheet1, "three"), # in this column in Sheet1
GETCOLUMN(Sheet1, "one") # and return the value from this column in Sheet1
)
)
So, the expected result is:
alpha
value
c
a
k
i
g
e
c
a
I can use the new Google Sheet formulas they just released -- except named ranges. I feel like there is some clever trick using them, but I can't come up with it.
Give a try on below formula:
=BYROW(A2:INDEX(A2:A,COUNTA(A2:A)),LAMBDA(x,INDEX(Sheet1!A:D,MATCH(x,INDEX(Sheet1!A:D,,MATCH("three",Sheet1!A1:D1,0)),0),MATCH("one",Sheet1!A1:D1,0))))
By XLOOKUP() function.
=BYROW(A2:INDEX(A2:A,COUNTA(A2:A)),LAMBDA(x,XLOOKUP(x,FILTER(Sheet1!A2:D,Sheet1!A1:D1="three"),FILTER(Sheet1!A2:D,Sheet1!A1:D1="one"),"")))
Sample Sheet Link.
Use HLOOKUP to the get the correct column in Sheet1 and XLOOKUP to get the corresponding column A value:
=ARRAYFORMULA(
XLOOKUP(
A2:INDEX(A2:A,COUNTA(A2:A)),
HLOOKUP("three",Sheet1!A1:Z,SEQUENCE(ROWS(Sheet1!A1:Z)),0),
Sheet1!A1:A
)
)

How to use Countif function to count rows that has a specific value in either of column A or B in Google sheet?

I am trying to count the number of rows that has a specific value let say "John" in either Column C or Column D. If the two columns have both the same value, then only one of them is counted.
Here is the example sheet. column C & D has the data.
Column G contains the function countif
https://docs.google.com/spreadsheets/d/1i9I2bhtlHAMWqVqdE7hbkgLRLCCUrfnOMSQcf9Gj4_0/edit#gid=0
Try:
=ArrayFormula(COUNTIF(C5:D12,F5:F7)-COUNTIFS(C5:C12,F5:F7,D5:D12,F5:F7))

Google Sheets Combine a column with duplicates and update total sum in another colum

This might be something fairly simple but struggling to find a way to do it.
In Column B, I have a list of foods required.
In Column C, I have the amount needed.
In Column D, I have g (for grams) ml (for mills) etc.
I would like to combine the duplicates in Column B and update the totals from Column C, with the g or ml in Column D beside it.
The list I have has been created by using an array formula based on dropdowns in another sheet.
I have seen people using UNIQUE formula in 1 column (this works) and then a SUMIF formula in another column and then a JOIN formula in another... I tried this but the SUMIF is always returning 0.
Would someone please be able to advise on how I can do this?
TIA :D
It's hard to be sure exactly what you need without seeing the data. But based on my understanding of solely what you've posted, this QUERY formula should generate a condensed mini-report:
=QUERY({B2:D},"Select Col1, SUM(Col2), Col3 WHERE Col1 Is Not Null GROUP BY Col1, Col3 LABEL SUM(Col2) ''")
In plain English, this means "Arrange the data from the range B2:D in the same order as the raw data, but sum the second column's data according to matches in both the first and third columns. Only return results for the raw data where the first column is not blank. Replace the default 'sum' header on the second column with nothing; I don't need it."
This formula assumes that every ingredient will always be attached to the same measurement (e.g., 'salt' in Col B is always paired with 'mg' in Col D, etc.). If this is not the case, you will wind up with ingredients being listed as many times as there are different measures in Col D.

How do I add to my QUERY function to add an additional requirement?

=ARRAYFORMULA({
AVERAGE(QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(Data!D$2:E856<>"", "♠"&Data!D$2:E856&"♦"&Data!F$2:G856, )),,999^99)),,999^99), "♠")), "♦"),
"select Col2
where lower(Col1) contains '"&LOWER(A2)&"'
offset "&COUNTIF(Data!D$2:E856, A2)-6))})
I have the above formula that I am using. What this does is the following:
The last 6 times A2 shows up in either Column D or E, it accumulates the corresponding value in column F or G. Those 6 values are then turned into an average, as well.
I am trying to add one condition to this. I want it to only take those last 6 instances where column H and I are also something specific.
So when A2 shows up in column D, I only want to use the row if Column I is the value "X". When A2 shows up in column E, I only want to use the row if Column H is "X".
I am unable to get this implemented into my function myself and desperate for some help. One problem is that the "X" search is in reversed column order (ie. when A2 is in D, trying to search I...and when A2 is in E, trying to search G...which isn't the order those appear in the alphabet). Also just not sure where in formula this conditional even needs to go.
Data! just references the sheet I have my dataset dumped into, obviously.

Resources