How to strip the time off a date column in Powerpivot 2013 - powerpivot

I'm attempting to strip the time from my date column in Powerpivot 2013. I tried this:
=DATE (YEAR([Completion Date]), MONTH([Completion Date]), DAY([Completion Date]))
However, this is not working. Any ideas?

Related

How to pull next 2 quarter data with a data model that doesn't have fiscal time database and company defined fiscal year

I would like to pull all orders from the current, next and next to next quarter of the time stamp. I am able to pull up current quarter data but I am not able to pull next and next+1 quarter data.
I am currently using MS SQL Server 2013.
The time stamp also appears in a weird format. for eg.20191031_FY20_Q1_Wk1 whereas [Order Close Fiscal Year Quarter Display Code] appears as 2020-Q1. So to pull current quarter data I have used below condition:
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code]
What I logically want to do is this:
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code] + 1
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code] + 2
Obviously, I came across data type conversion error. I even tried using Dateadd() function:
[Opportunity Close Fiscal Year Quarter Display Code] = DATEADD(quarter,1, cast(left(time_stamp,8) as date) )
but still I keep coming across same error.
The MOST IMPORTANT THING I would like to HIGHLIGHT is my org's Fiscal Year is not same as generic Fiscal Year. In my org, Fiscal Year begins from Oct and ends in Sep. So I am not sure how even DateAdd() function will help. I believe having a Fiscal Time table customized as per org's Fiscal Year could be of great help for me but my manager thinks the BI team won't entertain such a request.
Any help in building this query would be really great!!
You would seem to have time_stamp values whose format is not as you describe.
You should find the column values that don't convert to a date:
select time_stamp
from t
where try_cast(left(time_stamp, 8) as date) is null and time_stamp is not null
With this information, you can debug your code or the data.

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;

Create a sql query based on selected month from dropdown combobox in excel userform

I have a userform in excel for attendance reporting. On the form the user chooses the employee they want to review, and then they can check a box that says year to date, or select individual months from a combobox drop down. This is all to query our sql server that has the data on it. Year to date is easy, because I know how to use the variables for current date and first day of the year. What I'm having trouble with is how to convert the user's month selection into a usable query. For example: If the user selects January for the drop down, the query would be
select * from [dbo.mytablename]
where [agent name] = '<value from a textbox that I know how to pass in>'
and [cal date] > '1/1/2018' and [cal date] < '1/31/2018'
Right now my only idea is to have cells on a holding spreadsheet in the workbook that have the first and last day of each month and then use the cell values. But I'd like to avoid that if possible. If you have any ideas that would be great!
First of all
and [cal date] > '1/1/2018' and [cal date] < '1/31/2018'
will select all data from Jan 1 to Jan 30 and not include any data from Jan 31.
Keeping this in mind, the check should be
and [cal date] > '1/1/2018' and [cal date] < '2/1/2018'
So if you do get a month from drop down you can do something like below
select * from [dbo.mytablename] t
join
(values
('Jan', 1),('Feb',2),('Mar', 3),
('Apr', 4),('May',5),('Jun', 6),
('Jul', 7),('Aug',8),('Sep', 9),
('Oct', 10),('Nov',11),('Dec', 12)
)v(mon,num)
on v.mon='<month name received from a text box>'
t.[agent name] = '<value from a textbox that I know how to pass in>'
and t.[cal date] > cast(cast(v.num as varchar)+'/1/2018' as date)
and t.[cal date] < cast(cast((v.num+1)%12 as varchar)+'/1/'+ cast(2018+ (v.num/12) as varchar) as date)
In SQL Server there are numerous techniques for date calculation/converions. If 2012+ you can use EOMonth() or DateFromParts().
I should also add [cal date] < '1/31/2018' should be [cal date] <= '1/31/2018'
Example
Select StrCast1 = try_convert(date,concat('January',' 1, ',2018))
,StrCast2 = EOMonth(try_convert(date,concat('January',' 1, ',2018)))
,FromParts1 = DateFromParts(2018,1,1)
,FromParts2 = EOMonth(DateFromParts(2018,1,1))
Returns
StrCast1 StrCast2 FromParts1 FromParts2
2018-01-01 2018-01-31 2018-01-01 2018-01-31
Assuming your month is a numeric value between 1 and 12, you can use something like
Dim fromDate As Date, toDate As Date
fromDate = DateSerial(2018, month, 1)
toDate = DateSerial(2018, month + 1, 1)
Debug.Print fromDate, toDate
This will work even with December, DateSerial accepts a 13 as month.
Note that you have to ask for < first day of next month to catch data from the last day of the month.
Side note: I assume you use a ADODB.Command to query the database. I would strongly suggest that you use ADODB.parameter to pass the dates (and the agent name) to the database - you don't have to deal with formatting or converting the dates so that the database understand them correctly. See for example https://stackoverflow.com/posts/10353908/edit to get an idea how this works.
Update
I assume you fill your combo box with values from a range and the value is linked to a cell. Use for example the match function to get the month as number (I would never trust that the name of a month is translated correctly by Excel or the database, there are too many things like regional settings that could cause that it breaks).
Assuming:
Range with month names: A1..A12
Linked cell: B1
Put the formula =MATCH(B1,A1:A12) in cell B2. VoilĂ , you have the number of the month.

SSAS DAX time intelligence previous year and calculation gives wrong value

We want to show current periods sales versus previous period sales.
To show the previous period we make use of a date dimenion table and the following calculation:
CALCULATE(SalesValueGross; DATEADD(Date[Date]; -1; YEAR))
Unfortunately somehow it shows minor (decimal) differences when comparing years.
The difference get's bigger when we slice to months.
Another issue we have is that this calculation does not seem to work when comparing (for example) week 1 - 2015 with week 1 - 2014.
Any help is greatly appreciated!
When I want to get prior calendar year sales, I use the a formula such as the following:
Cal Prior Yr Sales:=if(HASONEVALUE('Sale Date'[Calendar Year]),
Calculate([Total Sales],
PREVIOUSYEAR('Sale Date'[Date])),BLANK())
The HASONEVALUE just ensures that there is only one year selected so it will know the correct previous year to retrieve.
You can make a series of calculations that will let you use one calc that determines what level of the date hierarchy you are in (assuming you have the fields available in your date table). Here is something I've used in the past, with a fiscal calendar that was different from the normal calendar.
First, the base calculations:
Sales Same Week Prior Year:=
CALCULATE([Total Sales],Filter(All('Sale Date'),
'Sale Date'[Week Key] = max('Sale Date'[Same Week Last Year])))
Sales Same Month Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Month Seq] = max('Sale Date'[Month Seq])-12))
Sales Same Quarter Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Quarter Seq] = max('Sale Date'[Quarter Seq])-4))
Sales Prior Year:=CALCULATE([Total Sales], Filter(All('Sale Date'),
'Sale Date'[Fiscal Year] = max('Sale Date'[Fiscal Year])-1))
You can hide all of those calculations and then create one last calculation and leave it visible:
Sales Same Period Last Year:=
if(HASONEVALUE('Sale Date'[Week Key]), [Sales Same Week Prior Year],
if(HASONEVALUE('Sale Date'[Month Key]),[Sales Same Month Prior Year],
if(HASONEVALUE('Sale Date'[Quarter Key]),[Sales Same Quarter Prior Year],
if(HASONEVALUE('Sale Date'[Fiscal Year]), [Sales Prior Year], BLANK()))))
You may need to add a couple of calculated fields to your date table to make it work. I have fields for: [Same Week Last Year], [Month Seq], [Quarter Seq]. Same week last year is an integer field that is yyyyww. Month Seq and Quarter Seq are just autoincrementing integers in chronological order that do not repeat.
My formula for same week last year is
=if('Sale Date'[Week Nbr] = 53, (('Sale Date'[Fiscal Year]-1) * 100) + ([Week Nbr]-1),
(('Sale Date'[Fiscal Year]-1) * 100) + ([Week Nbr]))
I did the sequence numbers in my SQL view, which is the source for the date date. As an example, if my date table starts at 1/1/2010, the month seq for Jan 2010 is 1 and the month seq for Jan 2011 is 13. The quarter seq for Q1 2010 is 1 and the quarter seq for Q1 2012 is 9.
http://www.daxpatterns.com/time-patterns/ is a good read for this topic.

Selecting records where 'ActualLiveDate' is between today and X days in to the future with SQL Server

I'm trying to get the syntax to show a list of records where the Actual Live Date is equal to or greater than today by 7 days and ideally show how many per day (thursday = 7 records).
i was thinking something along the lines of:
SELECT [PW Number]
,[status]
,[install Date]
,[ICL Client Code]
,[Actual Live Date]
FROM
[QuoteBase].[dbo].[Circuits]
WHERE
[Actual Live Date] BETWEEN today and 7 days time (this is where I am a little stuck as im fairly new)
If you need "today" to start at 00:01 this morning, then you need to remove the time portion from e.g. GETDATE(). I'm also using explicit functions to show that I'm adding days:
SELECT [PW Number]
,[status]
,[install Date]
,[ICL Client Code]
,[Actual Live Date]
FROM
[QuoteBase].[dbo].[Circuits]
WHERE
[Actual Live Date] BETWEEN
DATEADD(day,DATEDIFF(day,0,GETDATE(),0) AND
DATEADD(day,DATEDIFF(day,0,GETDATE()+7,0)
If Actual Live Date contains a time component, you may want to adjust the +7 to +8, depending on exactly which rows should be included in the result or not.
you can use the BETWEEN and GETDATE() functions for this, the GETDATE() + 7 will add 7 days onto todays date
SELECT [PW Number]
,[status]
,[install Date]
,[ICL Client Code]
,[Actual Live Date]
FROM
[QuoteBase].[dbo].[Circuits]
WHERE
[Actual Live Date] BETWEEN GETDATE() AND GETDATE() + 7
If the column is actually a date, you need to be a bit careful:
[Actual Live Date] BETWEEN cast(GETDATE() as date) and cast(GETDATE() + 6 as date)
This would include today. If today is a Monday, it would include seven days up to next Sunday. If you would want next Monday too, then the -6 would be -7.
When the column is a date, The comparison [Actual Live Date] >= getdate() will always be false for dates today because of the time component.
Between works as expected for datetime values but not for date values.