Formula to calculate historical data based off a specific condition? - google-sheets

I am trying to observe historical trends on customer acquisitions (new and returning) and am looking to use a formula to automate it for me.
Essentially, I am looking to determine the average amount of new customers we acquire on a specific day, specific week, and specific month. For example: what are the average customers we have acquired every Monday for the past 6 months, or what is the average number of customers we acquire the first week of every month?

Solution:
You can use the date operators in your QUERY statement to filter by month, week, or even day of week.
Examples:
every Monday for past 6 months
=query(A1:B, "select avg(B) where datediff(todate(now()),todate(A)) < 180 and dayofweek(A) = 2", 1)
first week of every month
=query(A1:B, "select month(A),avg(B) where day(A) <= 7 group by month(A) offset 1", 1)
You would need to tweak the sample queries to cover your data range and which columns do you need to average and compare.
References:
QUERY()
Query Language Reference | Scalar Functions

Related

Data Validation + VLOOKUP for Month in Sheets

So I'm currently setting up a sheet that records weekly numbers. I'd love to have something that summarizes the totals (money in, loss, etc) by month. So if the week = 4/4/0 this is April (4) and is calculated just by a drop-down of months. Is such a thing possible in Sheets? I don't need the formula just the clue, really.
Update:
I found something similar:
=SUMIFS($N$7:$N$11,$M$7:$M$11,">="&S8,$M$7:$M$11,"<="&EOMONTH(S8,0))
N7:N11 is amount
M7:M11 is date
S8 is the date I'm querying for
The issue I have with that (^^) formula is that it wants me to present the date as D-MONTH-YYYY
but I'd love the date to just be the MONTH
You can set the column to show just the month by setting its format:
and creating a "Custom date & time"
Alternatively, you can use text to represent the months and convert them to number representation using MONTH(S8&1)
Then, you can do some funky way of converting the "month" to "date" using DATE(YEAR, MONTH(S8&1), 1), where "year" is the year you're querying for.
To sum it up, the formula from your question might look something like this:
=SUMIFS($N$7:$N$11, $M$7:$M$11, ">="&DATE(2022, MONTH(S8&1), 1), $M$7:$M$11, "<="&EOMONTH(2022, MONTH(S8&1), 1),0))

How to get the average of the last cells in a column based on the last month?

I have a sheet that contains an amount in column D and the week number(A), month number (B), and year(C). How do I get the average of the last month's amount (D)? So in the example below, I am looking for the average of D251 to D254 based on last month (Column B) 7. Then next month, with it's September, The same cell would average all the "Amounts" in column D for the month of last month, Aug 8th. I hope that all makes sense.
How do I get the average of the last month's amount (D)?
You can use AverageIF, or possibly AverageIFs (plural) to account for prior year. I updated my example. You also might consider a different structure/column for time that combines Year_Month. See example in sheet.
Formula used:
=IFERROR(AVERAGEIFS(D:D,B:B,if(B2=1,12,B2-1),C:C,if(B2=1,C2-1,C2)),"First Period")
See this example.
Please try the following
=AVERAGE(INDEX(FILTER(A2:D,B2:B=MAX(B2:B)),,4))

How to get last closing price of a stock in google finance (In a non-trading day)

I tried using
=INDEX(GOOGLEFINANCE("NASDAQ:MSFT","price",TODAY(), 2),2,2)
And saw that on some days (e.g. 12/6/2021, a Saturday) the function failed to return value. I assume that it is because there was no trading on that day.
Is there a way for me to pick the last value of a stock prior to that day? (e.g. If I calculate on a Saturday or on a Sunday with an American stock I would get Friday's value, Thursday for an Israeli stock etc.)
I am not aware of a way for GOOGLEFINANCE to automatically adjust for no trading days.
One way to do it is to get the "price" data for the last 7 days (to be safe) and then query that data to get the "price" value next to the max date.
This formula works for me:
=INDEX(QUERY(GOOGLEFINANCE("NASDAQ:MSFT","price", TODAY()-7, TODAY()),"select Col1, Col2 order by Col2 desc",1), 2, 2)
You can use the formula below, in the cell that you want the price to be.
=INDEX(GOOGLEFINANCE(A5;"price";$B$2);2;2)
Where A5 contains the stock symbol, like CMI, JNJ, NEE or whatever.
And the B2 contains the following formula:
=if(weekday(B1)=2;B1-3;if(weekday(B1)=1;B1-2;B1-1))
Finally, B1 is just =today().
This will adjust the day for weekdays only. So if it is Saturday, Sunday or Monday, it will give you the price of the stock on Friday.
Basically it will give the last closing price on business days.
Best regards.

Commission calculation based on sliding month (googlesheets)

I have to pay a commission to Agents (affiliates) based on the following conditions:
the commission starts on a monthly basis following a USER (linked to the Agent) first deposit/purchase on a website
agents have a decreasing commission, ex: 1 month following first deposit of their USER = 30% of sales, 2d month period following 1st deposit of USER: 25% of sales, etc
Commission are paid on a month basis calculation (ex: from 01/07/2020 till 31/07/2020)
If a USER makes a first purchase on June 22d and if sales commission for 1st period is 30%, then agent is eligible to a 30% commission on sales from june 22d till July 22d, then 25% for sales from 23rd july till 23rd august, etc
I have designed a googlesheets (see below) that serves the purpose (using 12 columns to get the correct commission% for a specific user on a specific day!), but I am trying to find a more straight forward formula to get the applicable com. % based on the commission sliding table and the first deposit date of a specific user.
Can anyone help?
The google sheets showing my calc is here:
https://docs.google.com/spreadsheets/d/1I1gzZ670hJH8HwCGizzbvlQkg0dgAvgOSQTfOUL0VgU/edit?usp=sharing
This might help you. (Updated to correct the row number of where the formula should go.)
If you insert a new column in your sheet, to the right of Column W, the Current Commision Month, and paste the following formula in the cell where the Current Commision Month header text should appear (Row 9 in your sample) of that column, it replicates the results in your Current Commision Month.
But it does not require columns I through V. You can test that by deleting columns I through V - you can use Undo and Redo to go back and forth, if necessary. Depending on how you use your "End" column - the logic wasn't clear to me - the info for that can also be gained in this one column.
={"Current
Commision
Month";"";ArrayFormula(
if(
($H11:H<>"") * ($A11:A>=date(year(H11:H),month(H11:H),day(H11:H))),
ifs( $A11:A< date(year(H11:H),month(H11:H)+1,day(H11:H)),1,
$A11:A< date(year(H11:H),month(H11:H)+2,day(H11:H)),2,
$A11:A< date(year(H11:H),month(H11:H)+3,day(H11:H)),3,
$A11:A< date(year(H11:H),month(H11:H)+4,day(H11:H)),4,
$A11:A< date(year(H11:H),month(H11:H)+5,day(H11:H)),5,
$A11:A< date(year(H11:H),month(H11:H)+6,day(H11:H)),6,
$A11:A>=date(year(H11:H),month(H11:H)+7,day(H11:H)),9999),
""))}
The first IF test is to check that the FirstDeposit date is not blank, and that the sale date is greater than or equal to the FirstDeposit date.
The IFS tests go through and check whether the sale date is less than one of the months, and stops at the first value (commission month) that is greater than the sale date. If never, it places a vlaue of 9999.
Note that the "9999" values are just to indicate the sale date is greater than the "End" date, and can be changed to blanks or whatever you want.
[![enter image description here][1]][1]
I've added a sample tab with the final result. Let me know if this helps. There may be several other enhancements possible for your sheet, in particular the use of ARRAYFORMULAS to fill values down many of your columns.
I haven't spent time on the actual commision calculations, in the final columns, but if you feel that still needs improvements, I can try to simplify there as well.
[1]: https://i.stack.imgur.com/TfFZ5.png

'weeknum' function

Here is what I'm trying to do:
I have three Spreadsheets.
(1) Days (holds increase in user-nr per day)
(2) Weeks (is supposed to sum up user increases so they are shown each week)
(3) Months (is supposed to sum up user increases so they are shown each month)
To give an example: if we have 10 users on Monday, 20 more on Tuesday, 15 more on Wednesday (that's when the next calendar week starts), then I want in the sheet "weeks" to see e.g. 45 users in calendar week 27 or so.
So what I try is this: =SUMIF(WEEKNUM(Days!A2:A977); A2; Days!B2:B977)
A holds the date of the day
B holds the number of users.
What happens is it does not sum up the number of the users shown in B, but only gives the number in the first cell of the weeknumber shown in A2.
What is my mistake?
The formula seems to be correct, but two things are needed.
You should embrace it in ArrayFormula()
You should use one more weeknum()
=ArrayFormula(sumif(weeknum(Days!A2:A977);weeknum(Days!A2);Days!B2:B977))

Resources