Tableau: I need to get the difference in days between two dates. Datediff is giving incorrect result, why? - tableau-api

I am using DATEDIFF as
DATEDIFF('day', #2016-12-24#, #2016-12-22#)
I am getting the result as -1468. If I interchange the dates, I am getting 1468
While the difference is just 2 days.
If I use DATEDIFF('day', #2016-12-24#, TODAY()) I am getting 8808. why?
Any help is greatly appreciated in getting the dates difference properly.

Weird problem with Tableau. Here is the solution. https://community.tableau.com/thread/122324
Tableau automatically uses SUM() when we drag this field into rows/columns.
We need to use non-additive aggregation like MIN() MAX() AVG() or MEDIAN().
Thanks Shawn, for your answer in the Tableau Community.

Format for DATEDIFF Function: DATEDIFF(date_part, [Start Time], [End Time]. It is showing -1468 because you're using end time first and start time later. Make sure your start and end times are in string format.

Related

MDX number of days between shell date dimension and regular date dimension

I have a shell date dimension, and a sale date dimension. Having trouble setting up a calculated measure with the difference in days between the 2 dates.
I have tried a number of things, and the calculation always seems to return an error.
mdx example is:
WITH
MEMBER [Measures].[TimeDate] AS [Date].[Day].currentmember
MEMBER [Measures].[DSODate] AS [DSO Date].[Day].currentmember
MEMBER [Measures].[DaysSinceSale] AS
DateDiff(
"d"
, [Measures].DSODate.MemberValue
, [Measures].TimeDate.MemberValue
)
Select
{[Measures].[DaysSinceSale]} ON COLUMNS,
{[Date].[Day].members} ON ROWS
from [Receivables];
I have tried using DateDiff, and tried just subtracting the 2 dates.
Assuming it may have something to do with the 2 date dimensions being of different hierarchies, but i am not really sure how to handle that.
MDX Results
Date conversions can be tricky in mdx so maybe initially try the following simple approach:
WITH
MEMBER [Measures].[TimeDate] AS [Date].[Day].currentmember
MEMBER [Measures].[DSODate] AS [DSO Date].[Day].currentmember
MEMBER [Measures].[DaysSinceSale] AS
DateDiff(
"d"
, VBA!CDate([Measures].DSODate.MemberValue)
, VBA!CDate([Measures].TimeDate.MemberValue)
)
Select
{[Measures].[DaysSinceSale]} ON COLUMNS,
{[Date].[Day].members} ON ROWS
from [Receivables];
Otherwise you might need to use the key and an approach similar to this:
MDX - Converting Member Value to CDate
I found a way to get this to work ...
Main issue was that i didn't have a crossjoin, like whytheq mentioned.
I also didn't need the custom Measures declared at the top.
The other adjustment i made was to utilize the DateKey in the date calculation. That seemed to work in all my tests, and improved performance quite a bit.
WITH
MEMBER [Measures].[DaysSinceSale] AS
[Date].[DateKey].CurrentMember.MemberValue - [DSO Date].[DateKey].CurrentMember.MemberValue
Select
{[Measures].[DaysSinceSale]} ON COLUMNS,
{[Date].[DateKey].Members * [DSO Date].[DateKey].members} ON ROWS
from [Receivables];
If you see any issues that may arise with using DateKey let me know. For what i am doing that seemed to pull back the correct date value, and allowed me to find the difference between dates without using a datediff function.

DateDiff() function help - Dates in 2 different columns

I am trying to write a function to enable Tableau to calculate the difference between 2 dates, however they are in 2 different columns and I am having a bit of trouble.
Example:
Column 1
First Opened Date - 10/01/2014
Column 2
Reviewed Date - 15/01/2014
Obviously from this example there is 5 days between the two different columns.
These columns are aligned in rows due to a unique ID.
Any help would be much appreciated!
Thanks
Ellie
I am not sure exactly what your data looks like, but you could calculate the difference in days between two dates by using the datediff function.
I am using this calculation: DATEDIFF('day',[Order Date],[Ship Date])
You can easily recreate this with the sample data set (superstore) that ships with tableau.
Please use attr(DATEDIFF('day',[Order Date],[Ship Date]))
you will get the correct answer

Difference in Days in DB2

I have to calculate the difference in days between two dates and I figured out that
there is no such thing as a DATEDIFF() function in DB2.
I tried doing it like that:
(DAYOFYEAR(date1)-DAYOFYEAR(date2)+(YEAR(date1)-YEAR(date2))*365)
This is obviously not working for leap years, but I do not have to deal with that.
I know that date1 is always later than date2.
Do I have any flaws in my logic? It is not working (it's an exercise and I have a function to test my results). Is there an easier way to do that?
Thank you.
It would possibly help if we knew what version of DB2 you were using and what platform it was running on. But it seems likely that you can do something like this:
select
days(my1stdate) - days(my2nddate) as myDuration
from mySchema.myTable
The DAYS() function converts a DATE value into the number of days between Jan 1, 0001, and the supplied DATE value. Once both DATEs are converted, the subtraction (difference) is straightforward.

Number of days between past date and current date in Google spreadsheet

I want to calculate the number of days passed between past date and a current date. My past date is in the format dd/mm/yyyy format. I have used below mentioned formulas but giving the proper output.
=DAYS360(A2,TODAY())
=MINUS(D2,TODAY())
In the above formula A2 = 4/12/2012 (dd/mm/yyyy) and I am not sure whether TODAY returns in dd/mm/yyyy format or not. I have tried using 123 button on the tool bar, but no luck.
The following seemed to work well for me:
=DATEDIF(B2, Today(), "D")
DAYS360 does not calculate what you want, i.e. the number of days passed between the two dates – see the end of this post for details.
MINUS() should work fine, just not how you tried but the other way round:
=MINUS(TODAY(),D2)
You may also use simple subtraction (-):
=TODAY()-D2
I made an updated copy of #DrCord’s sample spreadsheet to illustrate this.
Are you SURE you want DAYS360? That is a specialized function used in the
financial sector to simplify calculations for bonds. It assumes a 360 day
year, with 12 months of 30 days each. If you really want actual days, you'll
lose 6 days each year.
[source]
Since this is the top Google answer for this, and it was way easier than I expected, here is the simple answer. Just subtract date1 from date2.
If this is your spreadsheet dates
A B
1 10/11/2017 12/1/2017
=(B1)-(A1)
results in 51, which is the number of days between a past date and a current date in Google spreadsheet
As long as it is a date format Google Sheets recognizes, you can directly subtract them and it will be correct.
To do it for a current date, just use the =TODAY() function.
=TODAY()-A1
While today works great, you can't use a date directly in the formula, you should referencing a cell that contains a date.
=(12/1/2017)-(10/1/2017) results in 0.0009915716411, not 61.
I used your idea, and found the difference and then just divided by 365 days. Worked a treat.
=MINUS(F2,TODAY())/365
Then I shifted my cell properties to not display decimals.
If you are using the two formulas at the same time, it will not work...
Here is a simple spreadsheet with it working:
https://docs.google.com/spreadsheet/ccc?key=0AiOy0YDBXjt4dDJSQWg1Qlp6TEw5SzNqZENGOWgwbGc
If you are still getting problems I would need to know what type of erroneous result you are getting.
Today() returns a numeric integer value: Returns the current computer system date. The value is updated when your document recalculates. TODAY is a function without arguments.
The following worked for me. Kindly note that TODAY() must NOT be the first argument in the function otherwise it will not work.
=DATEDIF( W2, TODAY(), "d")
Today() does return value in DATE format.
Select your "Days left field" and paste this formula in the field
=DAYS360(today(),C2)
Go to Format > Number > More formats >Custom number format and select the number with no decimal numbers.
I tested, it works, at least in new version of Sheets, March 2015.

Set one date from another date

I am using Objective-C within X-code.
I am iterating through a dictionary which contains a date value as one of it's keys. All I want to do is get an array of all the distinct dates so I can use them in a table, as headers. I just plan on
iterating the dictionary and adding dates to a mutable array each time I encounter a new date.
I must set previous date to new date for comparisons to work and I am having a very difficult time figuring out how to set one date equal to another date.
This seems like it should be such a simple thing to do and I am trying to avoid converting the dates to strings first - but if that's what I have to do, then so be it.
Any help would be appreciated.
Thanks,
Gerry O.
If you know the time offset is same as GMT, you could do it by dividing the date's timestamp by 86400 seconds (or three more 0s in milliseconds) and comparing those. If a time offset is there, add or subtract by 3600 seconds per hour before you divide them. But then again, leap years and seconds would break that...
Most languages have libraries support extracting the year, date, month, etc. They take everything into account, usually.
In Objc, you can get a NSDateComponents from NSCalendar's components:fromDate: method. After this, you can call the components to see exactly what each component (I suggest year, month, day) is.
I think you want code to compare dates and You need two loops nested.... where in outer loops iterates with conditional inner loop... In inner loop you just check that previously you had the same date or not...
please go through the below post on same site...
How to compare two dates in Objective-C
hope you will get solution... else clarify your question....