How to substract a day to a date field in Datastudio - google-data-studio

I need to subtract a day from a date field in Datastudio. My date has the DDMMYYYY format.

It can now be achieved using the DATETIME_SUB function (which was introduced in the 17 Sep 2020 update to Dates and Times):
1) Date - 1
Where the field is represented by Date, the following Calculated Field does the trick (subtracting a Day):
DATETIME_SUB(Date, INTERVAL 1 DAY)
2) Type (Date - 1)
Date
Google Data Studio Report and a GIF to elaborate:

DATETIME_SUB can also become more dynamic adding another field or parameter as its second parameter. BUT you have to CAST it as an int64_expression. Example:
DATETIME_SUB(Date_Field, INTERVAL CAST(Numeric_Field AS INT64) DAY)

Related

How can I make last month name dynamic in title using Google Data Studio?

I would like to create a custom field in score card so the titles of the tables are dynamic.
I would like to make 3 of them:
Current month > February
-1 month > January
-2 month > December
Below example of dashboard:
https://datastudio.google.com/reporting/0a45e6ba-deba-41d1-b158-87b29c6990a2
I already have tried something like Todate((),-1,MMMM) but that did not work.
Below a image for extra information.
It can be achieved using Scorecards with the following Calculated Fields:
0) Scorecard Settings
The configuration that will be used for all three Scorecards:
Data Tab
Aggregation: MAX (either MAX or MIN would result in the same value, thus selecting either Aggregation would be fine, in this case)
Type: Date & Time > Month
Style Tab
Hide Metric Name: Select (☑)
1) Current Month
The DATETIME_TRUNC function is used to get the CURRENT_DATE at a more granular level, in this case, by MONTH:
DATETIME_TRUNC(CURRENT_DATE(), MONTH)
2) Last Month
This uses 1) Current Month as the base and wraps it with the DATETIME_SUB function, which subtracts 1 MONTH from the CURRENT_DATE MONTH value:
DATETIME_SUB(DATETIME_TRUNC(CURRENT_DATE(), MONTH), INTERVAL 1 MONTH)
3) -2 Months
Replaces 1 with 2 in the INTERVAL 2 MONTH section of the DATETIME_SUB function from 2) Last Month:
DATETIME_SUB(DATETIME_TRUNC(CURRENT_DATE(), MONTH), INTERVAL 2 MONTH)
Editable Google Data Studio Report (Embedded Google Sheets Data Source) and a GIF to elaborate:

DataStudio : YYYYMM giving the wrong month (MM) & year (YYYY)

YYYYMM, when converted into MM & YYYY, is refecting the previous month. For example - 202001(which is Jan 2020) is giving 12 & 2019 (that is, Dec 2019). What is going on ??!!
My data source is BigQuery where I have joined two google sheets on YYYYMM as the primary key - I created this by extracting year and month from dates of the two google sheet and concatenating them.
I would suggest using complete YYYYMMDD Dates in Google Data Studio, as I haven't faced any issues with inaccuracy, using the YYYYMMDD format.
One way it could be achieved is by adding a fixed Day component to the end of the YYYYMM field, such as the 15th; for example, where YYYYMM represents the current Date field:
TODATE(CONCAT(YYYYMM, "15"),"%Y%m%d","%Y%m%d")

last day of last month date expression in ssis

I have to create a derived column to upload a date in OLEDB destination because my source file doesn't contain this date. The date i want to get through derived column is last day of last month. Does anyone know how to get it?
Try the following expression, just substract the current day from the current date using DATEADD Function.
DATEADD("d", -DAY(GETDATE()), GETDATE())
If you want to remove time you have two choices:
convert to string
LEFT((DT_STR,50,1252)DATEADD("d", -DAY(GETDATE()),GETDATE()),10)
convert to string then to date (it will generate time 12:00 AM)
(DT_DATE)LEFT((DT_STR,50,1252)DATEADD("d", -DAY(GETDATE()),GETDATE()),10)
The simplest way if you don't need time is :
(DT_DBDATE)(DATEADD("d",-DAY(GETDATE()),GETDATE()))

Day and month swapping only for single digits in SSIS

I have written an SSIS integration which fetches an employee and his expiry date. All the data is flowing correctly; however when a single digit day and month is present in expiry date, the destination column swaps the month and day (when the date has double unit for months or days its fine).
Example:
07/08/2016 to 2016-07-08 WRONG
15/03/2016 to 2016-03-15 CORRECT
since your date are in dd/MM/yyyy and you are in trying to convert it into datetime,so problem occurs.
I think this more safe,
declare #jk varchar(20)='07/08/2016'
select right(#jk,4)+'-'+substring(#jk,charindex('/',#jk)+1,2)+'-'+substring(#jk,1,2)

Sql server and Excel float to date offset

I'm working with date and float conversion on sql server and excel.
This is my query:
select getdate(),convert(float, getdate())
I get:
2014-11-21 16:38:49.973 41962,6936339506
If I copy this float number to Excel and I change cell type as date I get this value:
19-nov-2014
What's that? Why there is an offest of two days?
SQL server simply calculates the conversion of a date time to a float as the number of days since midnight on 01-Jan-1900 (i.e. select convert(DATETIME, 0) gives 1900-01-01 00:00:00.000)
Excel calculates a similar number, but the zero date is "00/01/1900". This is probably related to the fact that excel uses one based indexing, rather than the more common zero based indexing. The second day of difference comes from a well known bug whereby excel considers 1900 to have been a leap year.
Takeaway message: if you assume that excel is always behind by two days you'll be ok, except for dates on or before the 28th of February 1900.

Resources