finding difference between 2 months in spark sql - pyspark

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'))

Related

Azure Data Factory - calculate the number of days between two dates

I have to calculate the number of days between two dates and I search and I don't find any similar function available in ADF.
what I've noticed so far is that if I want to get the number of days between 2 columns, it means that the columns must be date columns, but I have timestamp columns (date + time)
how can I transform these columns into Date columns? or do you have other idea?
Using the fact that 86,400 is the number of seconds in a day
Now, using the function
ticks,
it returns the ticks property value for a specified timestamp. A tick
is a 100-nanosecond interval.
#string(div(sub(ticks(last_date),ticks(first_date)),864000000000))
Can re-format any type timestamp using function formatDateTime()
#formatDateTime(your_time_stamp,'yyyy-MM-dd HH:mm:ss')
Example:
#string(div(sub(ticks('2022-02-23 15:58:16'),ticks('2022-01-31 15:58:16')),864000000000))
This is the expression that I used for Data Flow.
toDate(toString({max_po create date},'yyyy-MM-dd')) - toDate(toString(max_datetimetoday,'yyyy-MM-dd'))
max_po, create date and max_datetimetoday are TimeStamp(date + time) columns.
The result is in days.

mongodb Get the difference of days from dates having different years

I am trying get the difference of days between two dates having different years. Like difference of days between 2015-12-26 and 2016-05-16.
In SQL I would get this below code.
DECLARE #s DATE ='2015-12-26',#t DATE ='2016-05-16'
SELECT DATEDIFF(N,#s,#d)
Can someone help me to convert this SQL Code in mongodb?
Since mongodb gives the difference in milliseconds, I have calculated the milliseconds for each day and divided with difference of milliseconds between two dates. See below example for reference:
>var s =ISODate("1996-12-31"), t=ISODate(), diff = Math.round((t-s)/(1000 * 3600 * 24))
>print(diff)
7140

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

SAS MDX check if time member belongs to 3 last periods

In MDX I need to define a measure that is calculated for all months except last N months.
For measure that becomes NULL in last 2 months I did this:
DEFINE Member '[Cube].[Measures].[my measure]' AS
'iif([DateDimension].[DateHierarchy].CURRENTMEMBER is
[DateDimension].[DateHierarchy].[All Months from hierarchy].LastChild.LastChild.Lag(1)
OR [DateDimension].[DateHierarchy].CURRENTMEMBER is
[DateDimension].[DateHierarchy].[All Months from hierarchy].LastChild.LastChild,
NULL,[Measures].[Measure XXX])';
And this works fine, but now I need to create lots of measures that should be NULLed in last 2, 4, 6 and 12 months. Above solution would work but would be very messy, so my question is:
Is there an MDX function / operator that allows to do somenting like this:
[DateDimension].[DateHierarchy].CURRENTMEMBER between STH
OR
[DateDimension].[DateHierarchy].CURRENTMEMBER >=
[DateDimension].[DateHierarchy].lastChild.lastChild.lag(N)
?
I checked the GT (greater or equal ) operator but this works only for comparing measures

Filter data by different time intervals

I need to filter my query with different time intervals like that:
...
where
date >= '2011-07-01' and date <='2011-09-30'
and date >='2012-07-01' and date >='2012-09-30'
I suppose such code is not good, because these dates conflicts with each other. But how to filter only these two intervals, skipping everything else? Is it even possible? Because if I query like this, I don't get any results.I tried to use BETWEEN, but it does same thing.
I bypassed this by extracting quarters from years and calculating only third quarter. But then other quarters sum is showed as zero and I can't ignore these rows that have sum column with zero value. I tried to filter where price > 0 (column where sum goes), but it says that column do not exist. So I put it whole FROM under '('')' brackets to make it calculate sum before where clause, but it still does give me error that such column do not exist.
Also if you need to see query I have now, I can post it, just tell me if it is needed.
I want to do this, because I need to compare third quarter of two different years (maybe I should use another approach).
You're not going to get any results because you can't have a date that's both within 7/1/2011 through 9/30/11 and after 7/1/2012 and after 9/30/12.
You can have a date that is either between 7/1/20122 and 9/30/2011 or between 7/1/2012 and 9/30/2012.
SELECT col1 FROM table1
WHERE date BETWEEN '7/1/2011' AND '9/30/2011' OR date BETWEEN '7/1/2012' AND '9/30/2012';