Managing dates in SPSS - Time difference in months - date

Im a novice SPSS user and are working on a data set with two columns, customer ID and order date. I want to create a third variable with a month integer of number of inactive months since the observed customer ID:s last order date. This is how the data looks like:

This will create some sample data to demonstrate on:
data list list/ID (f3) OrderDate (adate10).
begin data
1 09/18/2016
1 03/02/2017
1 05/12/2017
2 06/06/2016
2 09/09/2017
end data.
Now you can run the following syntax to create a variable that contains the number of complete months between the date in the present row and the date in the previous row:
sort cases by ID OrderDate.
if ID=lag(ID) MonthSince=DATEDIF(OrderDate, lag(OrderDate), "months").

Related

sql (or postgres) how to set the date as single year and month?

I am compiling data of multiple years but I only need time not date and year. How can I set the year and date data into a single year and a single date so that I can use the time information as accumulative data?
2020-01-01 + time value.
Thanks!!!!
The information you provided is a little light. Still with certain assumptions:
create table ts_sum (ts_fld timestamptz);
insert into ts_sum values ('2020-04-14 08:15:32'), ('2021-09-27 18:45:01'), ('2022-01-09 20:21:05');
select sum(to_char(ts_fld, 'HH24:MI:SS')::interval) from ts_sum;
sum
----------
47:21:38
Needs to be tested with your data. The procedure is extract the time portion out of the timestamp using to_char then cast that to an interval and then sum the time intervals.

How to merge two data streams in Alteryx

Alteryx
Table 1 is a google sheet file. It has x fields with primary key.
Every day to that table is added the weekday with the x data
For example:
Monday
Tuesday (is added on Tuesday) and so on.
My problem is that my workflow has a formula that does calculations with all the Weekdays.
Example:
Balance = All_Income - Monday - Tuesday - Wednesday - Thursday - Friday - Saturday - Sunday
But today for example, in the google sheet data I don't have the other weekdays except Monday and Tuesday, so I get the error "Unknown Variable" for Thursday.
I've inserted a Text Input and added all the weekdays.
I want to (Append maybe) these two data streams together so that I have all the weekdays there.
So if I run the calculations I have all the weekdays there.
Right now that formula works only on Sunday, when all weekdays are inserted as columns.
Any idea how to achieve this?
(p.s Creating the weekdays as columns in the google sheet with empty rows is not an option).
I managed to do it by creating a Text Input with the same column names (headers) as the other Data Source and performing a union.
Apparently I needed to perform a IF statement to check all Weekdays if they exist and replace the null values with.
If anyone encounters the same error, feel free to contact for help :)
Use the Transpose tool to verticalise the days of the week. Then Summarize using the primary key and sum the [value] field. That will give you the balance regardless of which days of the week are present in your worksheet. This technique applies to any problem in which one needs to aggregate multiple fields which may or may not be present or known.
Here is the simplest path to victory:
1. Input worksheet.
2. Connect Transpose tool.
3. In Transpose Key Columns, select only primary key.
4. In Transpose Data Columns, deselect all fields except for days of week and Dynamic or Unknown Columns. This will still work even if the worksheet doesn't have all the days of the week because as they come in, the Dynamic or Unknown Columns option will select them as Data columns.
5. In Transpose Missing Columns, select Ignore.
6. Connect Summarize tool.
7. In Summarize, group by primary key and sum on [Value] field.
From here, you can rename the sum_value field to Balance or something else friendly. You can also use a Join tool, joining on primary keys, to the original worksheet to get back to where you started with the new aggregated value.

Calculated Field to Count While Between Dates

I am creating a Tableau visualization for floor stock in our plant. We have a column for incoming date, quantity, and outgoing date. I am trying to create a visualization that sums the quantity but only while between the 2 columns.
So for example, if we have 9 parts in stock that arrived on 9/1 and is scheduled to ship out on 9/14, I would like this visualization to include these 9 parts in the sum only while it is in our stock between those 2 dates. Here is an example of some of the data I am working with.
4/20/2018 006 5/30/2018
4/20/2018 017 5/30/2018
4/20/2018 008 5/30/2018
6/29/2018 161 9/7/2018
Create a new calculation:
if [ArrivalDate]>="2018-09-01" and [ArrivalDate]<"2018-09-15"
and [Shipdate]<'2018-09-15"
then [MEASUREofStock] else 0 end
Here is a solution using UNIONs written before Tableau added support for Unions (so it required custom SQL)
Volume of an Incident Queue at a Point in Time
For several years now, Tableau has supported Union directly, so now it is possible to get the same effect without writing custom SQL, but the concept is the same.
The main thing to understand is that you need a data row per event (per arrival or per departure) and a single date column, not two. That will let you calculate the net change in quantity per day, and you can then use a running total if you want to see the absolute quantity at the close of each day
There is no simple way to display the total quantity between the two dates without changing the input table structure. If you want to show all dates and the "eligible" quantity in each day, you should
Create a calendar table that has all dates start from 1990-01-01 to 2029-12-31. (You can limit the dates to be displayed in dashboard later by applying date filter, but here you want to be safe and include all dates that may exist in your stock table) Here is how to create the date table quickly.
Left join the date table to stock table and calculate the eligible quantity in each day.
SELECT
a.date,
SUM(CASE WHEN b.quantity IS NULL THEN 0 ELSE b.quantity END) AS quantity
FROM date a
LEFT JOIN
stock b on a.date BETWEEN b.Incoming_Date AND b.Outgoing_Date
GROUP BY a.date
Import the output table to Tableau, and simply add dates and quantity to the chart.

Spotfire Text to Integer for Dates

I am attempting to load time series data from an excel spreadsheet into spotfire. In my spreadsheet there is a separate column for year (spotfire sees it as an integer) and month (spotfire sees it as text) since it is in the three letter abbreviation format ie January is JAN. I am trying to avoid changing the data in excel and would like to do all of my work in spotfire as this will be updated periodically. How do I link these columns in spotfire so that I can plot a variable over a time frame?
Click Insert > Insert Calculated Column... Make sure you have the right data table selected. In the Expression field type:
Date([year],
case when [month]="JAN" then 1
when [month]="FEB" then 2
when [month]="MAR" then 3
when [month]="APR" then 4
when [month]="MAY" then 5
when [month]="JUN" then 6
when [month]="JUL" then 7
when [month]="AUG" then 8
when [month]="SEP" then 9
when [month]="OCT" then 10
when [month]="NOV" then 11
when [month]="DEC" then 12 end,
1)
I would name it something like "monthdate". Note that each date will have the day equal to 1. If you also have the day in your data, just put that column in the formula above instead of the last 1.

Grouping by date difference/range

How would i write a statement that would make specific group by's looking at the monthly date range/difference. Example:
org_group | date | second_group_by
A 30.10.2013 1
A 29.11.2013 1
A 31.12.2013 1
A 30.01.2015 2
A 27.02.2015 2
A 31.03.2015 2
A 30.04.2015 2
as long es there isnt a monthly date_diff > 1 it should be in the same second_group_by. I hope its clear enough for you to understand, the column second_group_by should be generated by the user...it doesnt exists in the table.
date diff between which rows though?
If you just want to separate years (or months or weeks) use
GROUP BY DATEPART(....)
That's Sybase or SQL Server but other SQLs will have equivalent.
If you have specific data ranges, get them into a table with start and end date-time and a monotonically increasing integer, join to that with a BETWEEN and GROUP BY the integer.