Create a comparison with more than one month in datastudio - case

Good morning to all,
I would need your help of the most professional!
In my datastudio I have a column MONTH and I would like to create an automatism, where I can take the column MONTH -1 and based on the previous month, and once done this take into account the column RATING and return the value.
I used this formula but it doesn’t work:
WHEN Month < DATETIME_TRUNC(TODAY(), MONTH) AND Month >= DATETIME_TRUNC(DATETIME_SUB(TODAY(), INTERVAL 1 MONTH), MONTH) THEN 1 "
can you help me?
I would like to take the case Month and then take into account the previous Month and on this basis calculate the rating

Related

How to calculate the difference in a column based on first and last date by account in R

I am trying to calculate the difference in revenue for each unique account id based on the first and last transaction date. In the case of which there is only one transaction, I want the value to remain the same revenue value instead of having an NA. I have the code below:
df %>%
group_by(AccountID) %>%
summarize(revenue_diff = REVENUE[which.max(MONTH_END_DATE)]-REVENUE[which.min(MONTH_END_DATE)])
The output I am looking for is the following:
AccountID
revenue_diff
123456
-5500
987654
0
334567
23900

How to subtract a number of weeks from a yearweek/weeknumber in R?

I have a couples of weeknumbers of interest. Lets take '202124' (this week) as an example. How can I subtract x weeks from this week number?
Lets say I want to know the week number of 2 weeks prior, ideally I would like to do 202124 - 2 which would give me 202122. This is fine for most of the year however 202101 - 2 will give 202099 which is obviously not a valid week number. This would happen on a large scale so a more elegant solution is required. How could I go about this?
convert the year week values to dates subtract in days and format the output.
x <- c('202124', '202101')
format(as.Date(paste0(x, 1), '%Y%W%u') - 14, '%Y%V')
#[1] "202122" "202052"
To convert year week value to date we also need day of the week, I have used it as 1st day of the week.

Average temperature by day of year with SQLite DDB

I'm looking for a good solution to show the average temperature day by day of the year from a SQLite Database.
In my database, to be sample, I have a date column and a temp column, for each day since 5 years like that.
example
I would like to get, for each day of the year, the average temperature from my database.
I found the request to calculate the average for one day, but I don't how can I do like that for each day
SELECT avg(min) FROM historique WHERE strftime('%m-%d', date )= "04-01";
Could you help me please ?
You must use use GROUP BY:
SELECT strftime('%m-%d', date) day, avg(min)
FROM historique
GROUP BY day

Calculate mean of one column for 14 rows before certain row, as identified by date for each group (year)

I would like to calculate mean of Mean.Temp.c. before certain date, such as 1963-03-23 as showed in date2 column in this example. This is time when peak snowmelt runoff occurred in 1963 in my area. I want to know 10 day’s mean temperature before this date (ie., 1963-03-23). How to do it? I have 50 years data, and each year peak snowmelt date is different.
example data
You can try:
library(dplyr)
df %>%
mutate(date2 = as.Date(as.character(date2)),
ten_day_mean = mean(Mean.Temp.c[between(date2, "1963-03-14", "1963-03-23")]))
In this case the desired mean would populate the whole column.
Or with data.table:
library(data.table)
setDT(df)[between(as.Date(as.character(date2)), "1963-03-14", "1963-03-23"), ten_day_mean := mean(Mean.Temp.c)]
In the latter case you'd get NA for those days that are not relevant for your date range.
Supposing date2 is a Date field and your data.frame is called x:
start_date <- as.Date("1963-03-23")-10
end_date <- as.Date("1963-03-23")
mean(x$Mean.Temp.c.[x$date2 >= start_date & x$date2 <= end_date])
Now, if you have multiple years of interest, you could wrap this code within a for loop (or [s|l]apply) taking elements from a vector of dates.

Apache Drill: Group by week

I tried to group my daily data by week (given a reference date) to generate a smaller panel data set.
I used postgres before and there it was quite easy:
CREATE TABLE videos_weekly AS SELECT channel_id,
CEIL(DATE_PART('day', observation_date - '2016-02-10')/7) AS week
FROM videos GROUP BY channel_id, week;
But it seems like it is not possible to subtract a timestamp with a date string in Drill. I found the AGE function, which returns an interval between two dates, but how to convert this into an integer (number of days or weeks)?
DATE_SUB may help you here. Following is an example:
SELECT extract(day from date_sub('2016-11-13', cast('2015-01-01' as timestamp)))/7 FROM (VALUES(1));
This will return number of weeks between 2015-01-01 and 2016-11-13.
Click here for documentation

Resources