DateDiff() function help - Dates in 2 different columns - datediff

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

Related

finding difference between 2 months in spark sql

im trying to use function months_between in spark sql to find difference between 2 months in two different dates however I don't want to consider number of days between the 2 months for example :
I have these 2 dates
28-1-2-21 and 4-4-2021 , I'm getting a difference =2.2 however I want value to be 3
another two dates :
7-1-2021 and 18-3-2021 , I'm getting difference = 2.36 , I want value to be 2
I was trying to use round function but it's not accurate since for some dates I need to round up a number and for other dates I need to round down the same number ,same as the example above
the function im using months_between((date1),(date2))
Looks like you want the number of months regardless of dates.
In that case, you can combine trunc and months_between.
trunc will truncate to the unit specified by the format, so using the unit=month, you will get the first day of the month.
months_between(trunc('date1', 'month'), trunc('date2', 'month'))

Power BI Week Visual Filtering

Power BI novice here. I have multiple reports which require date filtering by week. I can sometimes get the data to display with my Week column using dates from a column in the same table.
I thought building a Week column based on the date column would result in an easy to use visual. The week column is calculated by:
WeekYear = IF(
FORMAT(WEEKNUM(START.[Date],1)-1,"00"="00",
"Wk53-" & YEAR(START.[Date])-1,
"Wk" & FORMAT(WEEKNUM(START.[Date],1)-1,"00") & "-" & YEAR(START.[Date]))
This results in an x-axis displaying weeks in this format: Wk52-2019. If the underlying data of column STARTis in the proper datetime format, what could be the issue?
I noticed data on the visual which is not filtered for a date range display without issue. Trying to filter with DATESINPERIOD or other DAX date filters caused calculated measures to not display or break the model. I know a lot of references state having a separate calendar table is critical and I suppose I don't fully understand. Thanks in advance.
If you are trying to create the week in date format, then you can use the following calculation:
Week = Table[Start] - WEEKDAY(Table[Start],2)+1
This returns the Monday date of the week, if you want other days you can adjust the calculation accordingly.
If this is not what you are looking for, then you might have to clarify your requirements a bit more.

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

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.

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.

Tableau: How to calculate number of days

I have custom SQL query in tableau that displays the data in the following format:
Start Date | App
6/21/16 app1
6/22/16 app2
6/23/16 app3
In this case, the end date would be '6/23/16'. So, app1 has been "live" for 2 days, app2 for 1 and so on.
I am trying to find the the number of days an app has been live. I can try using the DateDiff function but I would need to hardcode the values in that case and I want it to be dynamic.
The challenge is to have a calculated field that would find the max date in the entire column and subtract it from the individual app's date. This would give me the 'number of live days' for an app.
I am new to tableau and do not know how to proceed. Any help is appreciated.
Here is one solution.
datediff('day', [Start Date], { fixed : max([Start Date]) } )
Note the expression in Curley braces. That is a level of Detail (LOD) calculation -- basically a separate subquery at a potential different level of detail. So you can compare values for each row with values computed based on the whole table.
Depending on how and where you want to use this calculation, you might want to alter that LOD calculation to be fixed for certain dimensions or include or exclude certain dimensions. The online help should explain.
Just use"DATEDIFF('day',[Order Date], [Ship Date])",
order date and ship date are example dimensions from superstore data.xlxs