Keep 1st instance

junkforhr

Board Regular
Joined
Dec 16, 2009
Messages
115
Office Version
  1. 365
Platform
  1. Windows
I have a dataset which will have columns of Item and Status ( there are more columns but these are the two I need to use for my result)

Basically create a filter or use VBA to create a new dataset where I find duplicates of the combination of Item and Status, but keep the 1st instance of that combination but also keep every other row of data.

In the example below Item "5" and Status of "Ready" is in the data twice and also Item "7" and Status of "Assign" is there twice. I want to keep the 1st instance of both Item 5 and 7 and also Item 6.

The actual data set is 500K rows, so would like a filter so I can create a pivot table and use that to create the new dataset or VBA.


ItemStatus
5​
Ready
5​
Ready
6​
ABC
7​
Assign
7​
Assign

Desired Outcome

ItemStatus
5​
Ready
6​
ABC
7​
Assign
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I don't know what you mean by "keeping every other row of data". But, this gets you the desired results you list:

Mr Excel Questions 72.xlsm
ABCDEF
1
2ItemStatusItemStatus
35Ready5Ready
45Ready6ABC
56ABC7Assign
67Assign
77Assign
8
junkforhr
Cell Formulas
RangeFormula
D2:E5D2=UNIQUE(A2:B7,FALSE)
Dynamic array formulas.
 
Upvote 0
Apologies I should have have mentioned that I want to keep the 1sy instance of the combination of Item and where Status equals "Ready" or "Assign", so 5Ready where only have 1 row and and 7Assign would have 1 row, but for any other combination I would need duplicates. So if there were 2 rows of 6ABC, then that would have 2 rows.
 
Upvote 0
Maybe this then?

Mr Excel Questions 72.xlsm
ABCDE
1
2ItemStatus
35ReadyItemStatus
45Ready5Ready
56ABC7Assign
67Assign6ABC
79ABC9ABC
87Assign
junkforhr
Cell Formulas
RangeFormula
D3:E7D3=VSTACK($A$2:$B$2,UNIQUE(FILTER($A$3:$B$8,(B3:B8="Ready")+(B3:B8="Assign"))),FILTER($A$3:$B$8,((B3:B8<>"Ready")+(B3:B8<>"Assign"))=2))
Dynamic array formulas.
 
Upvote 0
Another option which keeps the relative order of the original data like the sample in post 1

23 11 08.xlsm
ABCDE
1
2ItemStatusItemStatus
35Ready5Ready
45Ready6ABC
56ABC7Assign
67Assign9ABC
79ABC6Ready
87Assign6ABC
96Ready
107Assign
116ABC
126Ready
junkforhr
Cell Formulas
RangeFormula
D2:E8D2=LET(ab,A2:A12&"|"&B2:B12,FILTER(A2:B12,(B2:B12<>"Ready")*(B2:B12<>"Assign")+(MATCH(ab,ab,0)=SEQUENCE(ROWS(ab)))))
Dynamic array formulas.
 
Upvote 0
Thank you for providing these answers. Both work, so I updated as requested by my manager and now they've thrown ID into the mix, so the same logic but with ID included to create the unique values.

My apologies but I was not aware of this added requirement when I posted my original question.



ItemStatusID
123​
ReadyABC
123​
ReadyABC
123​
ReadyABC
123​
UnkABC
543​
ReadyBAC
543​
ReadyBAC
543​
UNKBAC
543​
UNKBAC
1​
Status1ABC
2​
AssignHJL
2​
AssignHJL
4​
Status4HJL
4​
Status5HJL
1​
Status4LPI
2​
Status1LPI
3​
Status2LPI
4​
Status3LPI
5​
Status4LPI


Desired Result

123​
ReadyABC
123​
UnkABC
543​
ReadyBAC
543​
UNKBAC
543​
UNKBAC
1​
Status1ABC
2​
AssignHJL
4​
Status4HJL
4​
Status5HJL
1​
Status4LPI
2​
Status1LPI
3​
Status2LPI
4​
Status3LPI
5​
Status4LPI
 
Upvote 0
A VBA solution. see file attached.


VBA Code:
Option Explicit
Sub combination()
Dim i&, j&, k&, rng, res(1 To 10000, 1 To 3), id As String
Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")
rng = Range("A2").CurrentRegion.Value ' A2: cell within 1st range
For i = 2 To UBound(rng)
    If i = UBound(rng) Then
        k = k + 1: res(k, 1) = rng(i, 1): res(k, 2) = rng(i, 2): res(k, 3) = rng(i, 3)
        Exit For
    End If
    id = rng(i, 1) & "|" & rng(i, 2) & "|" & rng(i, 3)
    If Not dic.exists(rng(i, 1)) Then
        dic.Add rng(i, 1), ""
        For j = i + 1 To UBound(rng)
            If rng(j, 1) & "|" & rng(j, 2) & "|" & rng(j, 3) <> id Then
                k = k + 1: res(k, 1) = rng(i, 1): res(k, 2) = rng(i, 2): res(k, 3) = rng(i, 3)
                i = j - 1
                Exit For
            End If
        Next
    Else
        k = k + 1: res(k, 1) = rng(i, 1): res(k, 2) = rng(i, 2): res(k, 3) = rng(i, 3)
    End If
Next
'PASTE RESULTS INTO CELL E2. ADJUST TO ANY OTHER CELL
Range("E2:G10000").ClearContents
Range("E2").Resize(k, 3).Value = res
End Sub
 
Upvote 1
now they've thrown ID into the mix, so the same logic but with ID included to create the unique values.
Like this?

23 11 08.xlsm
ABCDEFG
1ItemStatusIDItemStatusID
2123ReadyABC123ReadyABC
3123ReadyABC123UnkABC
4123ReadyABC543ReadyBAC
5123UnkABC543UNKBAC
6543ReadyBAC543UNKBAC
7543ReadyBAC1Status1ABC
8543UNKBAC2AssignHJL
9543UNKBAC4Status4HJL
101Status1ABC4Status5HJL
112AssignHJL1Status4LPI
122AssignHJL2Status1LPI
134Status4HJL3Status2LPI
144Status5HJL4Status3LPI
151Status4LPI5Status4LPI
162Status1LPI
173Status2LPI
184Status3LPI
195Status4LPI
junkforhr (2)
Cell Formulas
RangeFormula
E1:G15E1=LET(ab,A1:A19&"|"&B1:B19&"|"&C1:C19,FILTER(A1:C19,(B1:B19<>"Ready")*(B1:B19<>"Assign")+(MATCH(ab,ab,0)=SEQUENCE(ROWS(ab)))))
Dynamic array formulas.
 
Upvote 1

Forum statistics

Threads
1,224,813
Messages
6,181,115
Members
453,021
Latest member
Justyna P

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