Conditional mean calculation in excel - vba

I have a dataset organized as following :
The column A is the name
The column B is the date
The column C is the value registered for that person in that day
How can i calculate for the whole dataset a mean of the value of that person in the 30 past days without manually ordering for name and making the mean checking the date?

Try the AVERAGEIFS function with the EDATE function giving you a one month window.
=AVERAGEIFS(C:C, A:A, "Jack", B:B, ">"&EDATE(TODAY(), -1), B:B, "<="&TODAY())
    

You can use a nested array formula (Ctrl+Shift+Enter instead of Enter):
=AVERAGE(IF($A$2:$A$15=A2,IF($B$2:$B$15>=TODAY()-30,$C$2:$C$15,""),""))

Add columns for these two fomulas:
=COUNTIF(A1:A4,"Jack")
=SUMIF(A1:A4,"Jack",C10:C13)
That will give you the count, and it will give you the sum. With those two you can calculate the mean.
That's the basic idea, anyway.
Of course, you can add another count for the date ranges. It's all the same sort of thing.

Related

Apple Numbers sum if is date is in Month

In Apple numbers I tried to sum cells if the date is in a specific month.
So I have a column A with a date and column B has a price.
Date
Price
29-07-2021
12,50
16-06-2021
15,00
I use the function sum.if and the function month that returns the number of the month.
My formule look like this
som.if(month(Date));7;Price);
But this generates an error numbers can only check one date and not for each row for counting.
Is there anybody who can help me with this?
Thanks a lot!
Use SUMPRODUCT() instead. I am using US-EN version of excel. So, decimal separator and formula definition may differ.
=SUMPRODUCT((B2:B3)*(MONTH(A2:A3)=7))
If you want to use SUNIFS() then can try-
=SUMIFS(B2:B3,A2:A3,">="&DATE(2021,7,1),A2:A3,"<="&DATE(2021,7,31))
If you have Excel-365 then can use FILTER() function like-
=SUM(FILTER(B2:B3,MONTH(A2:A3)=7))

Include missing dates as "0" in SELECT ... A,count(A) group BY (Google Sheets, or SQL)

I am creating a histogram of dates using SQL and I want to have "0" values for the missing dates. The specific SQL system I'm using is Google Sheets. Here is my example:
What I really want is for it to look like this:
Is there any way to do this?
The query that I'm using is SELECT a,COUNT(a) GROUP BY a ORDER BY a.
Is there a way to do what I want?
Create a column of the dates you want. For instance, you can put in the first date, the right below it have a formula that adds one day, and copy the formula down.
Then, use COUNTIF or a similar function to get the counts you want, e.g.:
=COUNTIF($A$1:$A:$5,"=" & C2)

How to select variable (with a year in the name) for calculation based on value of a date field using SAS SQL

With SAS SQL (or just SAS) I need to use a variable for a calculation based on the year portion of a different date field. The variable's name contains the year that I'd need to match from the year portion of the other date variable. How can I select the right variable to use for my calculation?
For example, I need to select which one of these to use:
GRADE_2013
GRADE_2014
GRADE_2015
by looking at a date field of the format 15JAN2014 - so from that year of 2014 I want to grab the value from GRADE_2014 to use in another calculation.
You have a few options, one is an array with a year index and another is the VVALUEX function that looks up the value of a variable.
Data One;
set Have;
array grades(2013:2015) grade_2013-grade_2015;
*Array method;
variable_want1 = grades(year(date_field));
*VValueX method;
variable_want2 = vvalues('grades_'||put(year(date_field), 4.));
run;
Generally, this sort of problem becomes much easier if you can transpose your data into a more normalized format.
So instead of having three grade_YYYY variables with year suffixes on each, transpose each record into three records, with variables YEAR and GRADE.
Thanks so much for the great answers...I'm a novice so I'll have to go the non-array approach at least to start.

Excel, automatically updating variable in IF statement based on reference cell?

I have a sheet that needs to be updated monthly with a formula that needs to change with the month.
This is the formula: =IF($S3=AI$1,[#July],0)
The check is to make sure my values go into the correct category. After the category is determined correct, I need to take the month's values by referencing the month column in my table.
Question: Is there any way to make it so when I change the month somewhere, I can make the formula essentially move over a column to take the new month's values?
Note: I also have a similar case where instead of taking the month values verbatim, I'm summing the year's values til said month.
So I assume you have a table with column name January to December.
You can do this in at least two ways (I assume your month number is in cell A1)
Explicit: (in case these columns are not in order or adjacent to another)
=CHOOSE(A1,[#January],[#February], ... ,[#December])
Implicit: (if the months are next to each other:
=INDEX(TableName[#[January]:[December]],A1)
Obviously, I'd recommend the second option, if applicable.
If you want to sum from januar to the current month, you can use this little know trick/syntax:
=SUM([#January]:INDEX(TableName[#[January]:[December]],A1))

Find a previous date on a list closest to a specified date

I would like to have VBA code or formula that will take a variable date value and find it on a list range of dates. If it can't find the date I want it to find the closest previous date value. VLOOKUP works great for finding an exact match but I am struggling with the code to find the closest previous date.
My only thought is to create a loop where if it doesn't match it continues to subtract a day from the variable date until it can locate a match on the list. That does not seem like the best or most efficient method and I hope someone can recommend a simpler solution.
Example:
Variable Date = 01/26/2014
Date List
02/04/2014
01/27/2014
01/24/2014
01/13/2014
12/29/2013
Desired Result = 01/24/2014
Any help would be appreciated.
Suppose your list of dates was in cells A1:A5 and your check date was in cell C1, you could enter this function ENTERED AS AN ARRAY FORMULA:
=MAX(($A$1:$A$5<=C1)*A1:A5)
Rememeber, to enter it as an array formula, hit Ctrl + Shift + Enter when entering the formula.
Hope this does the trick!!
I went about this a little differently, no arrays needed
Find how many numbers are bigger then the one you are looking for with CountIf()
Then I used =Large this will find the nth dates in a list we have the nth we are looking for in the countIF()
=LARGE(A:A,COUNTIF(A:A,">="&TODAY()))
Vlookup can do this actually, if you set the final argument to true, which looks for an approximate match. You need your dates sorted from Oldest to newest and it will return the first date that is not after your search term.