create date range from date column

Hi Knimers,

I would like to create a date range based on a single date column.

Input Data:

id | attribute | date
01 | att1 | 01-01-2020
01 | att1 | 02-01-2020
01 | att1 | 03-01-2020
01 | att2 | 04-01-2020
01 | att2 | 05-01-2020
01 | att2 | 06-01-2020
01 | att1 | 07-01-2020
01 | att1 | 08-01-2020
01 | att1 | 09-01-2020
01 | att1 | 10-01-2020
01 | att3 | 11-01-2020
01 | att3 | 12-01-2020
01 | att3 | 13-01-2020

Desired Output:
id | attribute | StartDate | EndDate
01 | att1 | 01-01-2020 | 03-01-2020
01 | att2 | 04-01-2020 | 06-01-2020
01 | att1 | 07-01-2020 | 10-01-2020
01 | att3 | 11-01-2020 | 13-01-2020

Thanks in advance

Try Group by node with grouping on id, attribute and min and max function on the date.

2 Likes

Hi @andrew_crockett,

Using the GroupBy node alone won’t result in your desired output since grouping on “id” and “attribute” will aggregate all values of the same group so there would be no such a row like the third row in your desired output example.

To overcome this issue, you have to create a grouping column based on the chain of attributes. So you can use the Lag Column node on “attribute” and filter rows where the value changes and assign a group ID (e.g. by using the Counter Generation node), the by using the Cell Replacer node, you can add the values to the main table and fill the other rows using the Missing Value node (in this case, using the previous value to fill the missing rows). Also you have to convert the date column from String to Date in the standard format, otherwise the min and max will not work properly.
Now the table is ready to be passed to the GroupBy node, grouping on the new group ID column (and if you like to keep id and attribute, add them as well) and using the min and max on the date column as @izaychik63 has suggested.

Here is an example workflow to check how this works:
date_range.knwf (42.5 KB)

:blush:

6 Likes

:+1: :knime: :star: :star: :star:

Hi there,

following Lag Column node from @armingrudd example you can use Rule Engine node to get group id using following syntax:
NOT $attribute$ = $attribute(-1)$ => $$ROWINDEX$$

Then immediately Missing Value can be applied as is.

Br,
Ivan

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.