How can i remove a row if the dates are already included in dates on another row

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
Ok so i have a problem, not got a clue how i can fix this so please help if you can
I download dates from a csv file,
Each Line has an ID, start date, and end date,
Now the problem I have is our systems are very old and quite a lot we get mutlpile lines for the same dates,
So for example I might get one which is for ID "7", 23 December 2019 to 3 January 2020
and another line for ID "7", 25 December2019 to 26 December 2019 but if I record the first dates then i get a doubling up of the second dates. so I need to remove the second dates.
So what i need a very clever macro that when run looks at the Info and does this
Column B is ID
Column D start date
Column E end date
for every row, if the ID is the same as this row then if the dates are with the dates of that row delete this row? (or something like this)
P.s there is space in L & M for any help rows that might be needed.
IDStart DateEnd Datewhat to do
71 jan 202010 jan 2020Keep this row!
67 jan 20209 jan 2020Keep this row!
74 jan 20207 jan 2020
delete this row!
because this row has dates that are already withing dates in another row I dont need it!
71 feb 20205 feb 2020Keep this row!
53 march 20205 march 2020Keep this row!
71 march 20205 march 2020Keep this row!
69 Jan 20209 jan 2020
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
In Power Query:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Start Date", type date}, {"End Date", type date}}),
    AddIndex = Table.AddIndexColumn(ChangeType, "Index", 0, 1, Int64.Type),
    Group = Table.Group(AddIndex, {"ID"}, {{"All", each _, type table [ID=nullable number, Start Date=nullable date, End Date=nullable date]}}),
    AddKeep = Table.TransformColumns(Group, {{"All", (x)=> Table.AddColumn(x, "Keep", each List.Accumulate(List.Zip({x[Start Date],x[End Date]}),0,(s,c)=> 
        if [Start Date]>= c{0} and [End Date]<= c{1} then s+1 else s))}}),
    tcn = List.Skip(Table.ColumnNames(AddKeep[All]{0}),1),
    Expand = Table.ExpandTableColumn(AddKeep, "All", tcn, tcn),
    Filter = Table.SelectRows(Expand, each ([Keep] = 1)),
    Sort = Table.Sort(Filter,{{"Index", Order.Ascending}}),
    Result = Table.RemoveColumns(Sort,{"Index", "Keep"})
in
    Result

Book3
ABCDEFGH
1IDStart DateEnd DateIDStart DateEnd Date
271/1/20201/10/202071/1/20201/10/2020
361/7/20201/9/202061/7/20201/9/2020
471/4/20201/7/202072/1/20202/5/2020
572/1/20202/5/202053/3/20203/5/2020
653/3/20203/5/202073/1/20203/5/2020
773/1/20203/5/2020
861/9/20201/9/2020
9
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top