Sum products in certain conditions - google-sheets

I'm sorry to ask this, I have not any code skills and I've trying to figure that out for a few hours now.. I think an image will be better for you to understand what I want:
I want A2 to show the sum of the products in G:G that fit certain conditions (2020,jan,buy). I haved tried several formulas but I came up with this one as the closest, I think, but still won't work:
=arrayformula(SUMIFS(E:E=B1,F:F="jan",G:G="buy",H:H))
Can anyone explain me how to achieve that?
Thanks very much :)

Please use this formula in A2 it will work
=sumifs(G2:G100,D2:D100,2020,E2:E100,"jan",F2:F100,"buy")
So basically, sumifs formula is right one as you want to check for multiple conditions.
so this is how this formula work
=sumifs(sum_range,criteria_range1,criteria1,criteria_range2,criteria2,...)
In your case your same range is column 'G' so if you have a finite range like only 25 rows you have in your table then instead of G2:G100 you can use G2:G25 as G1 is containing label and make sure that all other ranges also similar to the range of column G. for example if you take range of G2:G100 means 99 rows then you should take E2:E100 or E3:E101(range of 99 rows, that rows must be 99 and series start and end number is as per your requirement, similar case for other columns in this formula)
you have to check 2020 in column D, so you criteria_range1 is of D column I took it D2:D100 and criteria 1 is 2020 as it's a number it doesn't need double quotes
criteria 2 is you need to check Jan in column E so criteria_range2 is column E I took it E2:E100 and criteria 2 is "jan" as it's not a number so I took it in double quotes.
criteria 3 is you need to check 'buy' in column F so Criteria_range3 is column F. I took it as F2:F100 and criteria 3 is "buy" again it's not a number so took it under double quotes.

Related

How do I filter a list with date and time values by only extracting specific times each day?

I have a list of days and times in column A. Column B is a dollar value for each time value.
I want to get a specific time for each day and list the price in the next column. I have this right now:
The formula for column J is this:
=filter(A11:B,(mod(A11:A,1)*24)>=4.5,(mod(A11:A,1)*24)<=4.6)
This formula filters 4:30AM and the price associated with it.
Formula for column L is this:
=filter(A11:B,(mod(A11:A,1)*24)>=9.5,(mod(A11:A,1)*24)<=9.6)
Which filters only 9:30AM for each day and the price that goes with it.
As you can see, the dates stop matching when column J is 8/12/2021 and column L is 8/13/2021. This is because column L didn't have 9:30AM for 8/12/2021 so it skipped to 8/13/2021.
How can I make so that one of the columns skip a date if the other column doesn't match that time? I don't know if I explained that well but in the example above, column J should skip 8/12/2021 because column L didn't have 8/12/2021.
Another way of dealing with this issue is perhaps using UNIQUE and FILTER formulas but I'm not sure how that could be done. Any help would be greatly appreciated!
Here's a link to the spreadsheet: https://docs.google.com/spreadsheets/d/1TvP0_UsYJbLb5bscx2e4nTEnSkP5roBZpP8aIGdl414/edit?usp=sharing
I'm not 100% sure, but if you need to filter values so the time element is 09:30 and 04:30, then hide values where a date only occurs once (ie. no 09:30 value or no 04:30 value), you could use:
=arrayformula(filter(
filter(A11:B,(round(mod(A11:A,1)*24,2)=9.5)+(round(mod(A11:A,1)*24,2)=4.5)),
countif(int(filter(A11:A,(round(mod(A11:A,1)*24,2)=9.5)+(round(mod(A11:A,1)*24,2)=4.5))),int(filter(A11:A,(round(mod(A11:A,1)*24,2)=9.5)+(round(mod(A11:A,1)*24,2)=4.5))))>1)
)
Adjust >1 at the end to whatever logic you need (=2, <>1 etc).
You can also add sort() around everything if you need to.
Explanation:
It turns out that mod(A11,1)*24 doesn't round particularly well, so I suggest round(mod(A11:A,1)*24,2).
Therefore to filter the date/times for 9.30am and 4.30am, you can apply:
round(mod(A11:A,1)*24,2)=9.5
and
round(mod(A11:A,1)*24,2)=4.5
like this:
=arrayformula({filter(A11:B,(round(mod(A11:A,1)*24,2)=9.5)+(round(mod(A11:A,1)*24,2)=4.5))})
filter is usually AND but to get OR condition it's filter(A:B,(A=1)+(A=2))
This gets the dates only:
=arrayformula(int(filter(A11:A,(round(mod(A11:A,1)*24,2)=9.5)+(round(mod(A11:A,1)*24,2)=4.5))))
Then this counts how many times a date appears:
=arrayformula(countif(int(filter(A11:A,(round(mod(A11:A,1)*24,2)=9.5)+(round(mod(A11:A,1)*24,2)=4.5))),int(filter(A11:A,(round(mod(A11:A,1)*24,2)=9.5)+(round(mod(A11:A,1)*24,2)=4.5)))))
Combining the elements gets the solution.
I'll add a tab to your sheet.
You could filter each list on finding a match for each date (integer part of datetime) in the other list:
In J:
=filter(A11:B,(mod(A11:A,1)*24)>=4.5,(mod(A11:A,1)*24)<=4.6,isnumber(match(int(A11:A),int(filter(A11:A,(mod(A11:A,1)*24)>=9.5,(mod(A11:A,1)*24)<=9.6)),0)))
In L:
=filter(A11:B,(mod(A11:A,1)*24)>=9.5,(mod(A11:A,1)*24)<=9.6,isnumber(match(int(A11:A),int(filter(A11:A,(mod(A11:A,1)*24)>=4.5,(mod(A11:A,1)*24)<=4.6)),0)))
Note
Plz see Aresvik's answer for a possibly better approach to rounding, but note that 4:30 (3/16) actually has an exact representation as a binary decimal so rounding errors aren't an issue here.

Google Sheets: Is there a way to make an Array for a sequence that grows to a repeat given number of times

I have managed to make this formula:
=IF(COUNTIF($A$1:A1,A1)=$B$1,A1+1,A1)
Is making an incremental list in the same column, starting with 01, and on cell B1 I am instructing how many times this number should repeat (up to 5 times per number). So, if I say repeat 2 times it looks like this:
Column A
1
1
2
2
How can I make it an Array Formula? I'm barely starting on Arrays so I'm just trying to learn as I go.
you can try to solve this with the following formula =ArrayFormula(flatten(if(SEQUENCE(1,C1),SEQUENCE(C2)))) but you should specify the value limit in cell C2 to which the numbers must increase, otherwise the formula will loop
Try this
=ARRAY_CONSTRAIN(arrayformula(query(flatten(split(rept("|"&A2:A,B2:B),"|")),"select * where Col1 is not null")),SUM(B2:B),1)
https://docs.google.com/spreadsheets/d/1qXy-hzWUnsUmAa-8aV5IHOyZABGD_h8ajICLwdyNge8/edit?usp=sharing
Try Sequence() with ROUNDUP() function.
=ArrayFormula(ROUNDUP(SEQUENCE(B1*B1)/B1,0))
When enter 3:

Automatically Number/ Count All Columns or Rows in Google Sheets

I just wanted a simple way to number columns or rows in a Google Sheet, and most answers I've found offer many options that are far more complicated than I needed them to be.
Example: I want to number every column in the active sheet, starting with 1 for Column A and counting up by 1, regardless of the content of any other cells on the sheet and if I add columns to the sheet later, I want them to automatically update with the correct column numbers.
Another way is to use SEQUENCE.
So putting =SEQUENCE(99) in A1 would number the first 99 rows, from 1 to 99.
To number columns, just rotate that array, with TRANSPOSE.
So if A1 held =TRANSPOSE(SEQUENCE(26))
that would number columns A to Z with the numbers 1 to 26.
If you want to number both columns and rows,try:
in A1: =SEQUENCE(999), and
in B1: =TRANSPOSE(SEQUENCE(25,1,2))
I realise that this is numbering a specific number of rows, or columns, but I often find that very useful. You could modify this to number all columns or rows by adding some count to determine the total number of rows or columns, and using that in place of the first parameter for the SEQUENCE function.
The simplest way I've found to do this is by putting either of the following formulas in A1:
For numbering rows: =ArrayFormula(ROW(A:A))
And for columns: =ArrayFormula(COLUMN(1:1))
After putting the formula in A1, I'll usually hide the column or row the formula is in so I don't accidentally change or delete it.
If I want the counting to start at 1 on the 2nd, 3rd, or 4th row or column, then adding a -1,-2, or -3 respectively to the end of the formula gets that done.
For example: To number columns starting with 1 in Column C, the formula I put in A1 is =ArrayFormula(COLUMN(1:1)-2).
This may be way more basic than most people on this site are generally looking for, but for some reason it took me an unexpectedly long time to find it/ figure it out, so I thought maybe someone else would find it useful in the future.

In Google Sheets, how to check if Cell A (Date) is within the Date range of Cell B and C

I have a sheet with a timeline that shows a month per row in column A and an amount in USD next to that month in column B.
I want to be able to specify amounts in column G with a start and end date for that amount in columns E and F.
What I am trying to achieve is that the values in column B are automatically calculated by looking at the start and end dates specified in columns E and F and then taking the corresponding value from column G if the date in column A falls in between the date range specified in E and F.
I have found many suggestions for similar problems online but wasn't able to get any of them to work for my specific case. Any help is very welcome
You could do it as an array formula like this:
=ArrayFormula(mmult((text(indirect("A2:A"&count(A2:A)+1),"YYMM")>=text(TRANSPOSE(indirect("`E3:E"&count(E3:E)+2)),"YYMM"))*(text(indirect("A2:A"&count(A2:A)+1),"YYMM")<=text(transpose(indirect("F3:F"&count(F3:F)+2)),"YYMM"))*transpose(indirect("G3:G"&count(G3:G)+2)),(INDIRECT("G3:G"&count(G3:G)+2)+2)^0))
The idea is to develop a 2D array where the rows are the months and the columns are the amounts for matching time periods. Then use the standard Mmult method to get the row totals of the array.
Using indirect for the ranges makes the formula longer but using full-column references would be slow as it would result in a nearly 1000 X 1000 array for a default-sized sheet.
EDIT 1
Or shorter
=ArrayFormula(mmult((text(indirect("A2:A"&count(A2:A)+1),"YYMM")>=text(TRANSPOSE(indirect("E3:E"&count(E3:E)+2)),"YYMM"))
*(text(indirect("A2:A"&count(A2:A)+1),"YYMM")<=text(transpose(indirect("F3:F"&count(F3:F)+2)),"YYMM"))
,INDIRECT("G3:G"&count(G3:G)+2)))
because you can combine the row totals step with multiplication by column G.
EDIT 2
Alternatively you could just employ a much simpler pull-down formula using SUMIFS:
=ArrayFormula(sumifs(G$3:G,eomonth(E$3:E,-1)+1,"<="&A2,F$3:F,">="&A2))
This uses Eomonth to change all the start dates to the first of the month so they can be compared to the dates in column A correctly. The formula still has to be entered as an array formula because of the Eomonth calculation.
Note
The equivalent pull-down formula to the original array formulas above would be
=ArrayFormula(sumifs(G$3:G,text(E$3:E,"YYMM"),"<="&text(A2,"YYMM"),text(F$3:F,"YYMM"),">="&text(A2,"YYMM")))
but this gives zero for all rows - the reason for this is not obvious to me at time of writing.

Formula to Test for Blanks in two Columns Against a Third Column

So I am using a conditional formatting custom formula to highlight a cell if the column it is summing from another spreadsheet has blanks, but I don't know how to do it with changing ranges. Basically what I want to do is use a third column, say column A, to determine the length of the range (of rows) I want to scan with CountBlank, and if it picks up a blank in there to return a "True".
So basically:
Column A Column B Column C Column D
Person 1 5:30AM 3:00PM 9.5
Person 2 5:00AM 8
Person 3 4:30AM 4:00PM 10.5
So ideally, the cell sums the fourth column with a different function (already have that), and it conditionally formats itself if a blank is picked up in Columns B or C, going all the way down to the last row of column A that has a value. Any help here would be appreciated, thank you.
If I understand you correctly, the custom formula for conditional formatting should be:
=or(countifs(A2:A,"<>",B2:B,""),countifs(A2:A,"<>",C2:C,""))
Suppose you had a list of people's names starting in F2 and their total hours starting in G2. Then you could alter the formula to:
=or(countifs(A$2:A,F2,B$2:B,""),countifs(A$2:A,F2,C$2:C,""))

Resources