How to calculate age in Google Data Studio? - google-analytics

I'm trying to create a calculated field for the age of an asset (say, an article or youtube video) in Data Studio. So basically it's the current date less the creation date, hopefully leaving me with the number of days since the item has been created.
Google's sample for this is:
DATE_DIFF(TODATE(end_time, 'NANOS', '%Y-%m-%d'), TODATE(start_time, 'MICROS', '%Y%m%d'))
Which doesn't work despite any tweaks I make to it. Any ideas?

One way that it can be achieved is by using the New Date Time functions released in the 17 Sep 2020 Update.
The Calculated Field below uses the DATETIME_DIFF function to get the difference between the CURRENT_DATE and the Date field (titled Creation Date in this Report), and can display the result as required, based on the third input of the DATETIME_DIFF function, such as DAY or YEAR:
1) DAY
DATETIME_DIFF(CURRENT_DATE(), Creation Date, DAY)
2) YEAR
DATETIME_DIFF(CURRENT_DATE(), Creation Date, YEAR)
Google Data Studio Report and a GIF to elaborate:

TODAY() worked for me.
Here is an example for calculating date of birth:
FLOOR(DATE_DIFF(TODAY(), TODATE(date_of_birth, '%Y-%m-%d', '%Y-%m-%d')) / 365)
https://issuetracker.google.com/issues/78200216

Related

Spotfire change datetime column to monday of weekday

im using spotfire software and i have a datetime column like:
DateTime
21/07/2022 12:11:01
21/07/2022 14:32:01
04/12/2022 10:22:01
30/06/2022 16:22:01
how can i created a new calculated column where it instead changes the date to the monday of the week for all values?
many thanks
i can do this in power bi by dropping the time and using the function
Start of Week Monday = 'Data'[DateTime ]+1-WEEKDAY('Data'[DateTime ]-1)
this results in all the values changing to the monday of its given week/month/year
how can i do this in spotfire?
Assuming you have the following functions available to you: DateAdd, DayOfWeek (*)
You could try:
DateAdd("dd",1 - (If(DayOfWeek([original_Date])=0,7,DayOfWeek([original_Date]))),[original_Date])
This resets the date (including the time portion) to the previous Monday.
Initially I had tried the simpler formula:
DateAdd("dd",1 - DayOfWeek([original_Date]),[original_Date])
but Sunday is mapped to a zero, so whenever the original day was a Sunday, it was pushed to the following Monday.
It does depend on your original settings so you may have to play around with the numbers a bit.
(*) depending on what the source of your data is, equivalent functions may be available.

Is it possible to print the official calendar week range and not just the available dates in my dataset?

I have a data frame which sums values over a weekly basis.
I am able to calculate the official calendar week that the summed values fall into in each case, but I want to know if I can add the calendar week range also for reference as some values only contain a couple of days' worth of dates for a particular week.
I currently use paste(min(created), max(created), sep = ' - ') but this only gives me the range of values with created and not the official calendar week range and can sometimes be misleading due to an incomplete weeks' worth of values being present in the dataset, as mentioned above.
Can an official calendar week range be achieved?

Quantmod R , how to select cells with Sys.date?

I'm making some calculations based on quantmod for some stocks.
Below I prepared a very simple example to reflect what I'd like to do which is at this time select some cells based on date, for example yesterday.
library(quantmod)
getSymbols("BAC", from="2018-06-18", src="yahoo")
As a result I get the following:
Now I'd like to make some calculations with the volume of yesterday so I wonder if something like this could work:
# I would like to multiply yesterday's volume for 1.05.
Vol_k <- (BAC$BAC.Volume Sys.Date()-1) * 1.05
How do I use sys.date here to indicate today -1 and select the volume cell of yesterday date?
Thank you very much for any comment.
V.
When I pulled this data, I don't get anything for yesterday being the 4th July (timezone I assume, or maybe due to public holiday in US?) so I did it for 2 days ago.
BAC[Sys.Date() - 2, "BAC.Volume"]
should give you the desired result of the volume. Did a bit of research (https://s3.amazonaws.com/assets.datacamp.com/blog_assets/xts_Cheat_Sheet_R.pdf)
last(BAC, '1 day')$BAC.Volume
should give you the last day, regardless of weekends/ holidays
You can always get the last value by accessing the index.i.e.
xts.object[max(index(xts.object),column]
In your case:
BAC[max(index(BAC)),"BAC.Volume"]

VB or macro to exclude period of times from time duration calculation in Excel

I have an Excel table which contains thousands of incident tickets. Each tickets typically carried over few hours or few days, and I usually calculate the total duration by substracting opening date and time from closing date and time.
However I would like to take into account and not count the out of office hours (night time), week-ends and holidays.
I have therefore created two additional reference tables, one which contains the non-working hours (eg everyday after 7pm until 7am in the morning, saturday and sunday all day, and list of public holidays).
Now I need to find some sort of VB macro that would automatically calculate each ticket "real duration" by removing from the total ticket time any time that would fall under that list.
I had a look around this website and other forums, however I could not find what I am looking for. If someone can help me achieve this, I would be extremely grateful.
Best regards,
Alex
You can use the NETWORKDAYS function to calculate the number of working days in the interval. Actually you seem to be perfectly set up for it: it takes start date, end date and a pointer to a range of holidays. By default it counts all days non-weekend.
For calculating the intraday time, you will need some additional magic. assuming that tickets are only opened and closed in bussines hours, it would look like this:
first_day_hrs := dayend - ticketstart
last_day_hrs := ticketend - daystart
inbeetween_hrs := (NETWORKDAYS(ticketstart, ticketend, rng_holidays) - 2) * (dayend - daystart)
total_hrs := first_day_hrs + inbetween_hrs + last_day_hrs
Of course the names should in reality refer to Excel cells. I recommend using lists and/or names.

Interval of one month back not working on the 31st?

Essentially, I have a query that is responsible for fetching all records (with specific filters) within the last month. I'm using Oracle's interval keyword and all was working great until today (December 31st, 2009). The code I'm using is
select (sysdate - interval '1' month) from dual
and the error I get it
ORA-01839: date not valid for month specified
How can I use the interval keyword to be compatible with any date? Or if anyone has a better way of approaching the issue, I'm all ears.
Thank you.
try
select add_months(sysdate,-1) from dual
Being pedantic...
The requirements are not quite specified perfectly unambiguously. What does the business mean by "within the last month"? Most people would take that to mean "within the current calendar month" in which case I'd use:
TRUNC(SYSDATE,'MM')
Otherwise, perhaps they want an arbitrary period of 1 month prior to the current date - but then how do you define that? As you've found, INTERVAL '1' MONTH simply subtracts one from the month portion of the date - e.g. 15-JAN-2009 - INTERVAL '1' MONTH returns 15-DEC-1999. For some dates, this results in an invalid date because not all months have the same number of days.
ADD_MONTHS resolves this by returning the last day in the month, e.g. ADD_MONTHS(31-DEC-2009,-1) returns 30-NOV-2009.
Another possibility is that the business actually wants to use an average month period - e.g. 365/12 which is approximately 30.4. They might want you to use SYSDATE-30, although of course twelve iterations of this will only cover 360 days of the year.

Resources