how can i make google sheet itrate over columns - google-sheets

I have a sheet like this how can make a cell in front of x under the sum column get the sum of the x count column and y get the sum y count column
of course, I use sum function on both but the issue I face is how to make the z ,x1,y1,z1 the same I try to fill it down but as you see in the picture it is wrong
how can I do it for 100 row ?

This formula seems to give the result you want:
={"Header","Sum";
ARRAY_CONSTRAIN(TRANSPOSE(D1:1),COUNTA(D1:1),1),
ARRAYFORMULA(MMULT(TRANSPOSE(N(D2:AJ10)),{SEQUENCE(ROWS(D2:AJ10),1,1,0)}))}
It places the two column labels in the first row, then transposes all of the header values into a vertical column in A2:A, but prevents any blank rows by using ARRAY_CONSTRAIN, and a check for the number of header values to transpose.
The main result is the Sums, calculated using MMULT. You need to enter the range of the cells you are going to sum over - I've used D2:AJ10, entered twice in the formula. MMULT can slow down performance the more cells it has to review, but this seemed fine for 33 columns by 9 rows. Test it out in your actual sheet, and report back if any issues.
REFERENCES:
ARRAY_CONSTRAIN To limit size of an array result, by # rows and # columns
MMULT The matrix product of two matrices. Can be used for summing, if one matrix is one dimensional (eg. a row or a column) with values of just 1.

Related

Count number of times values increase in range - Google Sheets

how are you? I'm just not sure what to do here, but I surely can use your help!
I have a series of values in a row range, such as in the following:
1000 2000 1500 2100 3200
I need to figure out a google sheets formula to put in the 6th cell of this row that counts the number of times the value of any cell is greater than the one to the left of it. In this example, my result should be 3.
Is there any way that this can be done? In Excel or Google Sheets, either is great to learn.
Thank you
You can also try the following formula
=COUNTIF(QUERY({{"";TRANSPOSE(B2:F2)},{TRANSPOSE(B2:F2);""}},
"select Col1-Col2 offset 1 label Col1-Col2 ''",0), "<0")
Assuming you have data in A2:E, place the following in F2:
=ArrayFormula(IF(A2:A="",,MMULT(IF(B2:E>A2:D,1,0),SEQUENCE(4,1,1,0))))
Since there are only four columns that might be greater than others (B:E), you can structure it this way. MMULT is hard to explain, but it essentially multiplies everything in each row of one matrix (range) by everything in another range and then adds them. SEQUENCE just forms a column of four 1's by which to multiply then add together everything in each row of the first range. Since anything times 1 is the original number, this just serves to to row-by-row addition.
This formula will process as many rows as have data in A2:E.
try in 7th column on row 2:
=ARRAYFORMULA(MMULT(IF(A2:E4<B2:F4, 1, 0), {1;1;1;1;1}^0))

Google Sheets - Multiply field by field three to the left

My title might not be very specific, so I'm going to try to explain a little better.
The sheet is divided into a name(Column A), containing a certain number of values(Column B), that get added together to a total in Column C. Furthermore, Column D, E and F contains the values I want the Total in Column C multiplied by. These first columns A to F I just fill in manually, but I would like a function to calculate the Columns I've called x, y and z total (G, H and I).
I see a pattern in this, I just can't figure out the syntax to get Sheets to see it aswell.
The pattern I'm invisioning is for each row, I want column G, H and I to take the value 3 fields to their left, and multiply it by Column C, at their row number.
Is this somehow achievable? I tried finding a solution online but I guess I don't know how to word myself.
Here's a picture to maybe make everything a little clearer
This would save me alot of time, given that I have over a hundred different rows this calculation needs to be performed on...
If something is not clear, please feel free to write a comment. I'll be following this thread quite liberally.
Thanks in advance!
You can have this formula on the first xTotal:
cell G2: =ARRAYFORMULA(if(len(C2:C),C2:C*D2:D,))
cell G2: =ARRAYFORMULA(if(len(C2:C),C2:C*E2:E,))
cell G2: =ARRAYFORMULA(if(len(C2:C),C2:C*F2:F,))
I created a sheet with the same results you had before, but this time you don't need vertical columns, just say in the # of Values column how many numbers you should have below. You just need to input the values with the grey columns.
Note: This is assuming you will always have growing vertical numbers like 1,2,3,4,5. In the new sheet you just need to set 5 in the column and it will calculate the result.
Please make a copy of this sheet and edit as you wish.
Sheet
You can use a single, simpler formula for this in cell G2
=ARRAYFORMULA(if(C2:C="",,C2:C*{D2:D,E2:E,F2:F}))

Working with hierarchical data in Google Sheets

If I have hierarchical data in a Google Sheet as in columns A and B in the example below, how can I write a formula that will fill the corresponding cells in column C with the product of the "parent" value in A1, and the "child" values in column B. That is, The formula in C8 for example, will search upward in column A until it finds the value 5 in A6, then multiplies it by 8, the value in B8.
Obviously I'm trying to avoid having to put the "parent" value in every row in column A.
After a fiddling around and jogging my memory on ArrayFormulas I figured out how to get the result I wanted. Here's the formula I'm using:
=ArrayFormula(index($B$2:$B2,MAX(IF(ISNUMBER($B$2:$B2),ROW($B$2:$B2)))-1,1))
(Note: Row 1 contains headings)
Edit: Explanation of how I arrived at this solution:
Ordinarily, IF(ISNUMBER(cell)), and ROW(cell) would return TRUE/FALSE, or a row number respectively. When used in an array formula and a range of cells as input instead, what you get is a list of TRUE/FALSE values, and row numbers evaluated for each cell in the input range. Wrap MAX around that and it will return the position in those two lists where IF(ISNUMBER()) is TRUE and the row number returned by ROW() is the highest, effectively searching from the bottom of the range.
The absolute reference for the first element in the range to be searched (in this case $B$2) keeps the range anchored to the same starting location when dragging the formula into other cells, while the lower bound of the range (in this case $B2) to be searched grows vertically.
Finally, the INDEX takes the input range, row number returned by the above (-1 row because of the header) and column position (1, since there is only one column) in order to return the desired value.
One way you could do this would be to associate each value in column B with a category or key that can be used to lookup the value for A in a separate table. This abstracts it somewhat, so you can change the values for the A column without having to have them in every row, but there's no empty cells.
i.e.
and lookup table:
In column C
= VLOOKUP($A1, <range for category lookup table>, 2, 0) * $B1
(And then this formula can be filled down)
More on VLOOKUP in Google Sheets here
Alternatively I suppose you could use a formula to find the last non-empty row in column A, or something along those lines, but this is more of a hack than a proper way to structure your data. Tables aren't really designed to be used in a hierarchical fashion like what you've shown. But they can easily represent hierarchical data using techniques like what I've suggested.

Is there a way to have formula for first row automatically populate the cells below?

I use this formula to print a yes in a cell if there are numbers >0 in the range $T4:$AE4 OR in the range $AG4:$AR4.
=IF(OR(COUNTIF($T4:$AE4,">0"),COUNTIF($AG4:$AR4,">0")),"yes","")
Then I drag the formula down (as I need this for a 1000 rows).
Is there a way to just have the formula for the first row and automatically populate the cells below if the rows below match the criteria?
I tried with an ARRAYFORMULA but it's not working..
=IF(OR(ARRAYFORMULA(COUNTIF($T4:$AE,">0")),ARRAYFORMULA(COUNTIF($AG4:$AR,">0"))),"yes","")
Try this formula:
=ArrayFormula(if(TRANSPOSE(MMULT(COLUMN(T4:AE)^0,TRANSPOSE(if(ISNUMBER(T4:AE),T4:AE,0))))+TRANSPOSE(MMULT(COLUMN(AG4:AR)^0,TRANSPOSE(if(ISNUMBER(AG4:AR),AG4:AR,0))))>0,"yes",""))
Explanation
mmult is used to make sums of $T4:$AE and $AG4:$AR
ISNUMBER is to convert all not numeric values into numbers in order to use only numbers in mmult function.
COLUMN(AG4:AR)^0 will give a column of ones (1,1,1,1,1) -- just what we need for mmult function.
Transpose is used because in [mmult] function:
the number of columns for matrix1 must equal the number of rows for
matrix2
so we need to transpose part of formula.
By the way, here's this formula but transformed to count matches of the word "word":
=ArrayFormula(if(TRANSPOSE(MMULT(COLUMN(T4:AE)^0,TRANSPOSE(--(T4:AE="word"))))+TRANSPOSE(MMULT(COLUMN(AG4:AR)^0,TRANSPOSE(--(T4:AE="word"))))>0,"yes",""))
As you can see, the principles of the formula can be used to match count logic.

Use text in a cell as plus and minus

I have a sheet with multiple rows. There is a cell on each row that says "In" and "Out" in a dropdown. Basically this is plus and minus. Next to that I have a cell that that holds an amount.
So it looks like this:
Item 1, In, 10
Item 2, Out, 5
This totals a profit of 5.
How can I with this setup calculate the total profit/loss for all the cells in the sheet depending on multiple in/out rows?
An if formula will calculate the rows and a sum formula will calculate the column.
First make the numbers positive and negative values.
=IF(B2="In",C2,C2*-1)
This formula in 'D2' checks if the value of 'B2' is "In".
If yes then just adds the value of 'C2'.
If no then it multiplies the value of 'C2' by negative one.
Second step is to add up the numbers.
=SUM(D2:D3)
Cell 'D4' sums the column.

Resources