Difference between two dates returns date instead of number [DAX] - powerpivot

In PowerPivot DAX formulas, I need the number of days between two dates.
My formula is
ProjectDays:=[ProjectFinish]-[ProjectStart]
But instead of a number, it returns a date.
Any ideas?

ProjectDays:= VALUE( [ProjectFinish] - [ProjectStart] )
VALUE() is the poorly named 'convert to numeric type' function.

Related

Differentiate Numbers and Date in MSExcel

I am processing a large set of data. The data contain both numbers, String and Date. I want to differentiate between Date, String and Numbers. I am facing problem in differentiating between Numbers and Date.
I am currently using the below formula.
=(IF(TYPE(A1)=2,A1,TEXT(A1,"mmmm d, yyyy"))&"& "&IF(TYPE(B1)=2,B1,TEXT(B1,"mmmm d, yyyy"))&"& "&IF(TYPE(C1)=2,C1,TEXT(C1,"mmmm d, yyyy")))
If I give 1, It is formatting it as Date January 01, 1900
Even I tried ISNUMBER function, It's returning true for a date as well, e.g: ISNUMBER(10/01/1900).
ISNUMBER doesn't work because dates are numbers in actual sense. It's just the formatting that makes them appears as dates. For example 00/01/1900 is 0 formatted as a date. The same applies for TYPE function as it will return a 1 for a number (whether the number is formatted as a number or date)
Use this:
=LEFT(CELL("format",A1),1)
It returns a d if A1 is formatted as date.

Comparing a date and getting result of records having further dates

I am using sql for database connectivity. I am filtering records in database on date basis using sql query which consists of function convert(varchar(100),column name,106). I am comparing it with date having dd/MMM/yyyy format. As a result I get records of next all the dates.
Example: If I try to filter the table by 01/June/2017 then inspite of having records of that date, I get records of next all the dates. I am not able to get records of 01/June/2017.
I have used greater than or equal to expression.
It is better to convert it to this yyyymmdd number format and do the comparison as below:
select convert(int,convert(varchar(10),[your column],112))
20170529
Don't convert your dates into any format (=strings) when doing comparison. Always use the date (or datetime etc) format. That way you will be comparing the actual values, not string or numeric comparison. Here the problem is that format 106 is dd mon yyyy so it will never match your date because it's a string dd/MMM/yyyy but you will get any dates starting with 02 anyhow, for example 02/Jan/1990, because in alphabetical order that will come after your given date.

How to use datediff equivalent in Oracle with YYYYMMDD formatted number?

I have Oracle database columns with the number format YYYYMMDD. I have not been successful in using this format with datediff to get the difference between two dates. The documentation I've read online uses a different format:
DATEDIFF(day,'2008-06-05','2008-08-05')
What's the best way for me to get number of days between two dates given the format available to me in Oracle? Answers not involving datediff are acceptable as long as it gets the number of days between two dates with the format YYYYMMDD.
Simple subtraction in Oracle:
SELECT TO_DATE('20080805','YYYYMMDD') - TO_DATE('20080605','YYYYMMDD')
FROM DUAL;
Oracle doesn't have a DATEDIFF() function. Instead, you can use simple arithmetic with Oracle dates, where subtracting one date from another gives the number of days, and where you can add an subtract days from a given date. (You can also subtract fractions of days, but that might be outside the scope of this answer.)
To convert your NUMBER dates of the format YYYYMMDD to actual dates, just use the TO_DATE() function (I am pretty sure that Oracle will implicitly convert the NUMBER value to a VARCHAR2 before converting to a date; if not, use TO_CHAR() to do that explicitly).
TO_DATE(20150301, 'YYYYMMDD')
To get the difference between two dates, you can do the following:
SELECT TO_DATE(my_number_date1, 'YYYYMMDD') - TO_DATE(my_number_date2, 'YYYYMMDD')
FROM my_table;
Incidentally, if you want to get intervals instead of days, convert to timestamp (using TO_TIMESTAMP()) instead of converting to date.

SQL - Subtract Date with Integer

I have a document that has "Delivery Date" and "Days to Deliver" fields
I would like to calculate a "Dispatch Date" = "Delivery Date" subtract "Days to Deliver"
Currently i'm trying:
dbo.t0.docduedate-dbo.crd1.daystodeliver
Am getting a datetime format error for above. Where am I getting this wrong?
Assuming that you are using SQL server, you could use SQL function DATEADD().
First parameter is d which stands for day.
Second parameter is how many days you want to add and putting minus (-) in front instead of adding subtracts the value.
Last parameter is initial date itself that you want to be manipulated.
You might need to cast input values to appropriate types - second parameter has to be integer.
SELECT DATEADD(d,-[DaysToDeliver], [DeliveryDate]) AS 'Dispatch Date' FROM [TABLE]
Try this
Select Dispatchdate = Dateadd(dd,-daystodeliver,Deliverydate)
from table

Date range comparison using varchar columns in Teradata

I've been tasked to take a calendar date range value from a form front-end and use it to, among other things, feed a query in a Teradata table that does not have a datetime column. Instead the date is aggregated from two varchar columns: one for year (CY = current year, LY = last year, LY-1, etc), and one for the date with format MonDD (like Jan13, Dec08, etc).
I'm using Coldfusion for the form and result page, so I have the ability to dynamically create the query, but I can't think of a good way to do it for all possible cases. Any ideas? Even year differences aside, I can't think of anything outside of a direct comparison on each day in the range with a potential ton of separate OR statements in the query. I'm light on SQL knowledge - maybe there's a better way to script it in the SQL itself using some sort of conversion on the two varchar columns to form an actual date range where date comparisons could then be made?
Here is some SQL that will take the VARCHAR date value and perform some basic manipulations on it to get you started:
SELECT CAST(CAST('Jan18'||TRIM(EXTRACT(YEAR FROM CURRENT_DATE)) AS CHAR(9)) AS DATE FORMAT 'MMMDDYYYY') AS BaseDate_
, CASE WHEN Col1 = 'CY'
THEN BaseDate_
WHEN Col1 = 'LY'
THEN ADD_MONTHS(BaseDate_, -12)
WHEN Col1 = 'LY-1'
THEN ADD_MONTHS(BaseDate_, -24)
ELSE BaseDate_
END AS DateModified_
FROM {MyDB}.{MyTable};
The EXTRACT() function allows you to take apart a DATE, TIME, or TIMESTAMP value.
You have you use TRIM() around the EXTRACT to get rid of the whitespace that is added converting the DATEPART to a CHAR data type. Teradata is funny with dates and often requires a double CAST() to get things sorted out.
The CASE statement simply takes the encoded values you suggested will be used and uses the ADD_MONTHS() function to manipulate the date. Dates are INTEGER in Teradata so you can also add INTEGER values to them to move the date by a whole day. Unlike Oracle, you can't add fractional values to manipulate the TIME portion of a TIMESTAMP. DATE != TIMESTAMP in Teradata.
Rob gave you an sql approach. Alternatively you can use ColdFusion to generate values for the columns you have. Something like this might work.
sampleDate = CreateDate(2010,4,12); // this simulates user input
if (year(sampleDate) is year(now())
col1Value = 'CY';
else if (year(now()) - year(sampleDate) is 1)
col1Value = 'LY'
else
col1Value = 'LY-' & DateDiff("yyyy", sampleDate, now());
col2Value = DateFormat(sampleDate, 'mmmdd');
Then you send col1Value and col2Value to your query as parameters.