RSA Archer calculated Date - archer

I'm new to Archer.
I'm trying to create a calculated date field based on 2 other fields.
Field A (Date Field)
Field B (Value List): Annually (365 days);Semi (182 Days); Monthly (30 Days)
Field C (Date Field)
What I want to see is, if Field A has value 6/12/2017 and Field B has value annually (365 days), Field C should show me the value 6/12/2018
Thank you

It's very easy :)
Please write the below calculation in FIELD C: (use DATEADD inbuilt Function)
IF([FIELD B] =VALUEOF(FIELD B], "Annually"), DATEADD(DAY, 365,[FIELD A]),
IF([FIELD B] =VALUEOF(FIELD B], "Semi"), DATEADD(DAY, 182,[FIELD A]),
DATEADD(DAY, 30,[FIELD A])))
Hope this will help!!

Another approach is to capture the Day Month and Year in off layout fields. Example shown below. This may need some tweaking of how to combine it back into a Calculated Date Field. Benefit of this approach is it handles leap years and and we aren't concerned about how many days are in each month. Simply adding so many days to the date will result in so many unwanted results and to handle that will result in some real complex solution.
note: formatting of calculation is taken from RSA's documentation on debugging calculations.
Example
Helper Day
DAY([Field A])
Helper Month
IF(
[Field B] = VALUEOF(FIELD B], "Semi")
, MONTH([Field A])+6
, IF(
[Field B] = VALUEOF(FIELD B], "Monthly")
, MONTH([Field A])+1
, MONTH([Field A])
)
)
Helper Year
IF(
[Field B] = VALUEOF(FIELD B], "Annually")
, YEAR([Field A])+1
, IF(
[Helper Month] > 12
, YEAR([Field A] + ([Helper Month] - 12))
, YEAR([Field A])
)
)
Field C
IF(
[Helper Month] > 12
, DATEFORMAT(CONCATENATE([Helper Day],"/",[Helper Month] -12,"/",[Helper Year]))
, DATEFORMAT(CONCATENATE([Helper Day],"/",[Helper Month] -12,"/",[Helper Year]))
)

Related

Two columns with dates (one any day of week, another one WEEK ENDING DATE (SATURDAY) BASED ON 1st column)

I have such a situation.
I need to have 2 columns 1) Is just pull data from a table (just as it is) r.[RCLDTE] (Day of week)
and 2 column) I need to basically look at the first column and make it Saturday of that week.
SELECT r.[RCLDTE] AS 'Day of Week'
,r.[RCLDTE] AS 'Week Ending Day (Saturday)'
Before what I was doing at similar projects I just used this code and added to WHERE statement.
WHERE CONVERT(DATE, CONVERT(CHAR(8), r.[RCLDTE] )) = cast(DATEADD(dd, DATEPART(DW,GETDATE())*-1, GETDATE()) as date)
This code was changing the dates column to Saturday.
However, here I have a different situation. I need 2 columns 1) as it is and 2) where dates will be Saturdays of the week from r.[RCLDTE] column , as a result from the way how I understand I cannot use WHERE statement because it will affect both columns.
Does someone know how I can leave 1st column as it is and 2nd a column of Saturday.
Please let me know.
Thanks.
To avoid issues when someone changes either DATEFIRST or LANGUAGE settings, you can use this. Also, given that you are storing dates in a numeric column for some reason (you really should provide feedback to whoever owns the system so they can fix it), we have to first try to convert those values to a date (they may not all be valid, which is one of the problems with using the wrong data type):
;WITH t AS
(
SELECT *, ProperDate = CASE WHEN ISDATE(CONVERT(char(8), RCLDTE)) = 1
THEN CONVERT(date, CONVERT(char(8), RCLDTE)) END
FROM dbo.tablename
)
SELECT [Language] = ##language, [Datefirst] = ##datefirst,
RCLDTE = CASE WHEN ProperDate IS NULL THEN RCLDTE END,
[Day of Week] = ProperDate,
[Saturday] = DATEADD
(
DAY,
6 - ((DATEPART(WEEKDAY, ProperDate) + ##DATEFIRST - 1) % 7),
ProperDate
)
FROM t;
Updated db<>fiddle that also demonstrates the handling of garbage data and a version of SQL Server so old that TRY_CONVERT() didn't exist yet (at least 12 years ago).
here is one way :
select
r.RCLDTE AS 'Day of Week'
, dateadd(day, 7 - datepart(weekday, r.RCLDTE) , r.RCLDTE)
from tablename r
db<>fiddle here

Making new column based on date with variable year

As topic is saying, it all should apply to dates with variable year.
I would like to make a new column (called which_part for example) saying if the date (in this case date is in "sil.sales_invoice_date") is between 1st of january to 30th of june - which should say H1 (half 1) and else is H2 (between 1st of july to end of the year).
I've tried doing something like that with convert
SELECT
sil.sales_invoice_date AS "Distributor Invoice Date",
CONVERT(sil.sales_invoice_date, #year + '-01-01') AS [start year],
CONVERT(sil.sales_invoice_date, #year + '-06-30') AS [end year],
It doesn't work at all and I can't write [half year] in there.
I've also tried to do it by using
case sil.sales_invoice_date between
But I don't really know how to format it to work with variable year.
if you have date in format dd.mm.yyyy
Try this
Case when substr(sil.sales_invoice_date,1,5) between '01.01' and '30.06' then 'H1'
else 'H2'
end as which_part;

Select Date range between two months

My table has one [date] column and would like to filter dates between March to auguest for any year.
Here is my functions, but none of them work and returns all data.
iif(CDate([Date]) between DateAdd ("m",3, CDate([Date])) And DateAdd ("m",7,CDate([Date])),"1","0")
iif([Date of Activity] between (DatePart("m", [Date of Activity]) = 4) And (DatePart("m", [Date of Activity]) = 8),"1","0")
Date is a reserved word and should not be used for a field name. That said, the following should work if your field is a date/time field and not text. Month() Between 3 and 8

Is it possible to create a dynamically calculated field?

How can I create a dynamically calculated field to determine the age in days based on a date/time stamp?
For example say I have a field that has a date time stamp. What I want to be able to see when browsing the cube is a column showing the number of days since the time stamp. So if the date time stamp is '8/21/13 12:00PM' and I browse the cube on '8/22/13 12:00PM', the calculated column "Number of Days Since" would show 1.
Is this even possible with SSAS?
Yes, you can:
with member Measures.[date string] as
Left(Right([Date].[Date].CurrentMember.UniqueName, 9), 8)
member Measures.[date formatted] as
Left(Measures.[date string], 4) + "-" +
Mid(Measures.[date string], 5, 2) + "-" +
Right(Measures.[date string], 2)
member Measures.[date date] as
CDate( Measures.[date formatted])
member Measures.[to today] as
now() - Measures.[date date]
, format_string = 'yy" years, "m" months, "d" days"'
select {
Measures.[date string],
Measures.[date formatted],
Measures.[date date],
Measures.[to today]
}
on columns,
[Date].[Date].[Date].Members
on rows
from [Adventure Works]
gives what you want. You do of course not need all the intermediate measures, they just show step by step what is calculated.
One remark, however: I formatted the to today measure with a date format using yy. A four digit year would show as "1912 years" etc, as SSAS is internally using the date coding like Excel of number of days since Jan, 1, 1900. I mis-used the date formatting here to format a duration, which is a number and not a real date. This formatting approach is only giving correct results for positive durations less than 100 years. But this is only a formatting issue and the main duration calculation is correct nevertheless. It just shows the duration in days, and the time within the day as fractions.

How to change only the year of a date datatype

I have a list of birthdays and the year is the only part that is incorrect. I have a list of ID #s for these individuals. Is there a way to change only the year for all of these people? I was thinking something like making a table of the query results and then using an UPDATE SET query, but I don't know how to only change the year.
Sample included in edit. Every year needs to be decreased by 2.
If all rows need to be decreased by two years, then:
UPDATE dbo.TableToUpdate
SET [Date Column] = DATEADD(YEAR, -2, [Date Column]);
If all rows need to be set to a specific year (say, 2019), and the column is date:
UPDATE dbo.TableToUpdate
SET [Date Column] = DATEFROMPARTS(2019, MONTH([Date Column]), DAY([Date Column]);
If all rows need to be set to a specific year (say, 2019) and the column is not date, you could use DATETIMEFROMPARTS or SMALLDATETIMEFROMPARTS, but at that point the following becomes shorter:
UPDATE dbo.TableToUpdate
SET [Date Column] = DATEADD
(
YEAR,
-DATEDIFF(YEAR, '20190101', [Date Column]),
[Date Column]
);
Here's the shortest I can think of, change the year to 2050:
select
dateadd(year, (2050 - year(d)), d)
from x
Sample data:
create table x(d date);
insert into x values
('2001-11-19'),
('2020-07-05'),
('2012-05-01');
Output:
COLUMN_0
2050-11-19
2050-07-05
2050-05-01
Live test: http://www.sqlfiddle.com/#!3/9a8b4/2
In case anyone else needs it for MySQL, here's the syntax:
UPDATE dbo.TableToUpdate
SET [Date Column] = DATE_ADD([Date Column], INTERVAL -2 year);