How monthly moving average in google sheets? - google-finance

I am trying to make a system that update moving average from google sheets.
Such as 3 months moving average. Average of 2022-03-13, 2022-02-13, 2022-01-13.
The problems are below:
GoogleFinance function does not provide "monthly" interval.
It excludes market-closed day of course when I try to get "daily" information.
So my idea is to use "weekly" and extract only 12 weeks such as when I try to get 3 months moving average. I took 1 month as 4-weeks simply.
=average(query(sort(GoogleFinance("QQQ","price", TODAY()-320, TODAY(),"weekly"),1,0),"select Col2 limit 12"))
I think this way is incorrect at some points. What's the better way to get it?

The exact value should be 364,64 with the selection of date :
=average(query(GoogleFinance("QQQ","price", TODAY()-93, TODAY(),"daily"),"select Col2 where Col1>DATE'"&TEXT(date(year(today()),month(today())-3,day(today())),"yyyy-MM-dd")&"' "))
359,34 with (12 weekly values) :
=average(query(sort(GoogleFinance("QQQ","price", TODAY()-93, TODAY(),"weekly"),1,0),"select Col2 limit 12"))
and 363,79 with (60 daily values) :
=average(query(sort(GoogleFinance("QQQ","price", TODAY()-93, TODAY(),"daily"),1,0),"select Col2 limit 60"))

Related

Year over year running month total percentage change in power bi

I've been struggling with this for a while. I need to calculate running month total percentage change year over year. I got it working but its not getting the running total for the month to do the calculation. By the way this is on the graph. Here are my calculations below:
how graph looks
ImprovementIncidentPercentage = 1 - DIVIDE([CurrYearIncidents],[LastYearIncidents],0)
CurrYearIncidents = CALCULATE(count(VW_RI_INCIDENTONLY_PBI[Incident]),
Filter(VW_RI_INCIDENTONLY_PBI,VW_RI_INCIDENTONLY_PBI[EVENTDATE].[Year] =('newsecondyear'[SecondYearValue])))
LastYearIncidents = CALCULATE(count(VW_RI_INCIDENTONLY_PBI[Incident]),
Filter(VW_RI_INCIDENTONLY_PBI,VW_RI_INCIDENTONLY_PBI[EVENTDATE].[Year] = 'newfirstyear'[FirstYearValue]))
The second and first year value is used as slicers for them to be able to pick two years they want to see the percentage improvement on:
slicers to pick what two years to do percent improvement on
I know something has to change CurrYearIncidents and LastYearIncidents so it does the calculation for running total for the month not the monthly total. I am just not sure how to make it work. Here is the close look at the issue.
show as table graph of error
For January the calculation is correct 1-(10/14) = 28.6%
For February it should be 1-(30/29) but instead it does monthly total for february and doesn't add up january so it does 1-(20/15)
To get accumulation values month over month I use this measure:
Cumulative events = TOTALYTD ( count (VW_RI_INCIDENTONLY_PBI[INCIDENT]), VW_RI_INCIDENTONLY_PBI[EVENTDATE].[Date])
Please help!

Date filtering of DAX measure in pivot chart

I have created 3 DAX measures in Power Pivot to calculate backlog value and I have an issue with filtering in pivot chart. When I filter out a specific time period, the backlog calculates only based on filtered dates. Instead of filtering, I would like to only "zoom in" to some specific time-range.
Measures
Opened:=CALCULATE(COUNTA([CreatedOn]))
Closed:=CALCULATE(COUNTA(Ticket[ClosedOn]),USERELATIONSHIP(Ticket[ClosedOn],'Calendar'[Date]))
Backlog:=CALCULATE([Opened]-[Closed],FILTER(ALLSELECTED('Calendar'),'Calendar'[Date]<=MAX('Calendar'[Date])))
Based on example data below, without filtering any dates the backlog value is following:
Date;Backlog
1/1/2021;1
2/1/2021;3
3/1/2021;1
4/1/2021;3
When I filter dates excluding 1st of January, I get following values:
Date;Backlog
2/1/2021;2
3/1/2021;1
4/1/2021;3
I want to get same values as without filtering the date. I just want to to "zoom in" on some specific date range. In my case i have 1 year of input data and want to see the backlog evolution of last 8 weeks. So when I filter last 8 weeks, my first week shows only value calculated for that week, but instead it should take into account all the values from previous periods + the result of first week
Example data
TicketNumber;Type;CreatedOn;ClosedOn;Status
ticket1;Service Request;1/1/2021;1/3/2021;Closed
ticket2;Incident Record;1/2/2021;1/3/2021;Closed
ticket3;Incident Record;1/2/2021;1/5/2021;Closed
ticket4;Service Request;1/4/2021;;Open
ticket5;Service Request;1/4/2021;;Waiting for
Fixed thanks to my colleague! :)
I had to change ALLSELECTED to ALL
Backlog:=CALCULATE([Opened]-[Closed],FILTER(ALL('Calendar'),'Calendar'[Date]<=MAX('Calendar'[Date])))

Rolling Average - Calendar Table and slicers

I'm having an issue that i can't seem to solve on my own:
I have a model that has two tables.
Fruit_data_Table
Date
Fruit_id
Fruit_category
Fruit_scheduled_picking_time
Fruit_real_picking_time
Fruit_picked_within_1min = 1 or 0
Fruit_picked_within_5min = 1 or 0
etc etc
On every given day i have 700 different fruit entries on my fruit_data_table.
I created a Calendar_Table using a classic :
Calendar_Table= CALENDAR(MIN(Fruit_data_Table[Date]);MAX(Fruit_data_Table[Date]))
And then i needed combined averaged values for any given day, including rolling averages, so inside my Calendar_Table i added some calculated columns for every dates such as :
Fruit_on_time_1_min_pick=
VAR this_date = Calendar_Table[Date]
RETURN CALCULATE(
SUM(Fruit_data_Table[Fruit_picked_within_1min ])/COUNT(Fruit_data_Table[Fruit_picked_within_1min ]);Fruit_data_Table[Date] = this_date)
Or a rolling average :
Fruit_on_time_1_min_pick_7day_average=
VAR this_date = Calendar_Table[Date]
RETURN CALCULATE(
SUM(Fruit_data_Table[Fruit_on_time_1_min_pick])/COUNT(Fruit_data_Table[Fruit_on_time_1_min_pick]);
DATESBETWEEN(Calendar_Table[Date];DATEADD(LASTDATE(Calendar_Table[Date]);-6;DAY);LASTDATE(Calendar_Table[Date])))
Which mean i ahve something like
Calendar_Table
Date
Fruit_on_time_1_min_pick (%)
Fruit_on_time_1_min_pick_7day_average (%)
etc etc
This all seems to work pretty well and i have my daily rate of fruit pciked on time and rolling averages.
The problem is that then i can't use my slicer to sort by fruit_category, or by time_of_day for example... any idea how i can get back to those slicers ?
(The PowerBi rolling average function don't seem to work at all for me, even though my Calendar_Table is a PowerBi generated Date Table...)
Thank you very much !
PS: real code is about train numbers is a major french train station... not really about fruits :)

How to find calculated population in dax formula

I have a table with country and population for 2017, and I have another table with country and population growth rate%. And I have one table with years like (2018 to 2028). I am trying to find calculated population for 10 years on the basis of these data as we are calculating compound interest.
Because you are working with growth rates, it is very unlikely that you will want to do this calculation as a measure. Rates don't aggregate well.
So, the first thing you're going to want to do is get your data into one table. I would do this in query editor.
You'll need a Cartesian join between your list of countries and a list of years. The PowerBI method for this is a little non-intuitive. You add a custom column, and in the formula you just type in the name of the table.
The result is that every single row in the countries table will be matched with every single row from the years table. If you have 5 rows in one and 10 rows in the other, the resulting table is 50 rows.
Then Merge in your table with the growth rates. Now you have a table that has the name of the country, the 2017 starting population, the growth rate. This set of rows will be repeated for every year from 2018 - 2028.
There is a specific formula for cumulative (compounded) growth.
Pricipal * ( 1 + RatePerPeriod / NumberOfCompoundsPerPeriod) ^ (NumberOfPeriods * NumberOfCompoundsPerPeriod)
You're doing this annually, so it simplifies a bit
Pricipal * ( 1 + Rate) ^ (NumberOfYears)
And the M will look like this:
[2017 Population] * Number.Power((1 + [Growth]),([Year] - 2016))
Good Luck! Hope it helps.

Calculate Number Months between 2 dates DAX

I am doing a rolling 12 month avarage over a specifik sum that are a calculated column if that matter? and my count over 12 month returns 355 day?
My sum column is [Volume]
I have a Calendar Table that are set up accordingly to a hierarchy as picture show below and more,
my code for the rolling avarage is,
12M tonnage Avarage =
CALCULATE(
SUM(STOWAGEACTUAL[VolyMton]);
DATESINPERIOD('Calendar'[DATE]; LASTDATE('Calendar'[DATE]);12;MONTH)
)
/
CALCULATE(
DISTINCTCOUNT('Calendar'[DATE]);
DATESINPERIOD('Calendar'[DATE];LASTDATE('Calendar'[DATE]);12;MONTH
))
All my values that i have in my SUM column looks like this,
And my Rolling AVG abowe calculates to this,
After some error searching i find that this piece of code,
DISTINCTCOUNT('Calendar'[DATE]);
DATESINPERIOD('Calendar'[DATE];LASTDATE('Calendar'[DATE]);12;MONTH
)
Gives me a Distinct Count value of 356. And when build my report to look on Days. My Rolling Average does compute correctly.
BUT, this is not what i want since i want this DAX to return 12 and i cant figure out why is doesnt?