How to extract the first four occurances

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
558
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a data set of some 45,000 records that looks like this.

Book2
ABCD
1nameclassdateposition
2A Big ChanceClass 104/08/2021
3A Big ChanceBM6828/07/20218
4A Big ChanceClass 121/07/20213
5A Big ChanceClass 107/07/202110
6A Big ChanceClass 202/06/202112
7A Big ChanceClass 126/05/20219
8A Fighting FuryBM5819/07/2021
9A Fighting FuryBM5819/07/20217
10A Fighting FuryBM5805/07/20213
11A Fighting FuryBM5822/06/20214
12A Fighting FuryBM5807/06/20216
13A Fortunate LassClass 331/07/2021
14A Fortunate LassClass 319/06/20214
15A Fortunate LassClass 329/05/20214
16A Fortunate LassClass 216/05/20211
17A Good ChanceBM5531/07/2021
18A Good ChanceClass B08/05/20211
19A Good ChanceMdn25/04/20211
20A Good ChanceMdn13/03/20217
21A Knight In ParisBM5404/08/2021
22A Knight In ParisBM5414/07/20219
23A Knight In ParisBM5416/06/202110
24A Knight In ParisBM6202/06/20213
25A Knight In ParisBM5416/05/20216
26A Krupt PickBM5816/07/2021
27A Krupt PickBM5806/07/20217
28A Krupt PickClass 320/06/202111
29A Krupt PickClass 305/03/20218
30A Land OfMdn25/07/2021
31A Land OfMdn01/07/20214
32A Land OfMdn22/06/20212
33A Land OfMdn22/05/20217
34A Little LuceMdn07/08/2021
35A Little LuceMdn23/01/20215
36A Little LuceMdn05/01/20215
37A Little LuceMdn10/07/20206
38A Little VagueBM6516/07/2021
39A Little VagueBM6502/07/20217
40A Little VagueBM6524/06/202112
41A Little VagueBM6528/05/20215
42A Little WittyOpen02/08/2021
43A Little WittyMdn27/07/20218
44A Lone HeroBM5803/08/2021
45A Lone HeroRST5803/07/20211
46A Lone HeroRST5813/06/20213
47A Lone HeroClass 113/05/20216
48A Magic ZarizBM6822/07/2021
49A Magic ZarizBM6803/07/20212
50A Magic ZarizOpen20/06/20212
51A Magic ZarizBM7226/05/20217
52A Midnight ShadowMdn04/08/2021
53A Midnight ShadowMdn29/07/20213
54A Midnight ShadowClass 114/07/202116
55A Midnight ShadowMdn23/06/20218
56A Pinch Of LuckBM6817/07/2021
57A Pinch Of LuckOpen03/07/20219
58A Pinch Of LuckBM6402/06/202112
59A Pinch Of LuckBM6414/05/20214
60A Real WagMdn16/07/2021
61A Real WagMdn01/03/20216
62A Real WagMdn05/02/20216
63A Real WagMdn22/01/20219
64A Shin RookOpen10/07/2021
65A Shin RookGroup 328/11/202016
66A Shin RookListed04/07/20208
67A Shin RookOpen06/06/20207
68A Tender LadyMdn16/07/2021
69A Tender LadyMdn18/06/20213
70A Tender LadyMdn31/05/20215
71A Tender LadyMdn07/05/20213
72A Thousand DegreesBM5418/07/2021
73A Thousand DegreesBM5418/07/20212
Pattern__2



How can I use PQ to extract only the four most recent occurrences on the field name?

The next dilemma is to only keep the four most recent occurrences, where there is a 1, in the position column below the blank cell.

In other words, the sequence I am looking to extract from column D is:

Blank or null
1
any other number
any other number

if the first four occurrences of the name do not follow this sequence, they can filtered out.


Any and all guidance is graciously received.

Thanks
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Using your example, please mock up manually what your expected results should look like. I don't quite understand what you are looking to do.
 
Upvote 0
Hi Alan, hope you are well.

From the above 73 rows, the only results that meet the criteria would look like this:

nameclassdateposition
A Good ChanceBM55
31/07/2021​
A Good ChanceClass B
08/05/2021​
1​
A Good ChanceMdn
25/04/2021​
1​
A Good ChanceMdn
13/03/2021​
7​
A Lone HeroBM58
03/08/2021​
A Lone HeroRST58
03/07/2021​
1​
A Lone HeroRST58
13/06/2021​
3​
A Lone HeroClass 1
13/05/2021​
6​


Cheers
 
Upvote 0
Hi Alan,

just thought I would let you know that I managed to achieve the result with two passes of some VBA code. Thanks anyway.
 
Upvote 0
Good Idea Alan.
1. Sort the data by Name.
2. On the first pass of the code, using the code below, I counted out the number of occurrences
VBA Code:
Sub AddCount()
count = 1
For rw = 3 To LastRow
    If Cells(rw, 1) = Cells(rw - 1, 1) Then
        Cells(rw - 1, 5) = count
        count = count + 1
        Else
        Cells(rw - 1, 5) = count
        count = 1
    End If
Next rw
MsgBox "Done!"
End Sub

3. Then off to PQ and filtered the results to return counts of 1, 2, 3 and 4.
4. Then on second pass, using this code.....

VBA Code:
Sub Find1stPlace()
count = 1
For rw = 2 To lastRow

    If Cells(rw, 4) = "" And Cells(rw + 1, 4) = 1 Then
        Cells(rw, 5) = 1
        Cells(rw + 1, 5) = 2
        Cells(rw + 2, 5) = 3
        Cells(rw + 3, 5) = 4

    End If
Next rw
MsgBox "Done!"
End Sub

Looked for the criteria of blank and 1, and reassigned the count or index for lack of a better word.

5. Back in PQ to do a transpose as described in an earlier post

And ended up with the results I required. Didn't take long at all.

Cheers
 
Upvote 0
a PQ solution

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    nmbrRows = Table.RowCount(Source),
    ChangeType = Table.TransformColumnTypes(Source,{{"name", type text}, {"class", type text}, {"date", type date}, {"position", Int64.Type}}),
    
    lstL1 = List.Generate(()=>0, each _ < nmbrRows -3, each _ +1, 
       each ChangeType[position]{_} = null and 
            ChangeType[position]{_+1} = 1 and 
            ChangeType[position]{_+2} <> null and 
            ChangeType[position]{_+3} <> null),

    AddIndex = Table.AddIndexColumn(ChangeType, "Index", 0, 1, Int64.Type),
    AddCustom = Table.AddColumn(AddIndex, "Custom", each lstL1{[Index]}),
    ReplaceErrors = Table.ReplaceErrorValues(AddCustom, {{"Custom", false}}),
    
    AddConditionalColumn = Table.AddColumn(ReplaceErrors, "TF", each if 
            ReplaceErrors[Custom]{[Index]} = true or 
            ReplaceErrors[Custom]{[Index]-1} = true or 
            ReplaceErrors[Custom]{[Index]-2} = true or 
            ReplaceErrors[Custom]{[Index]-3} = true 
    then true else false),
    
    RemoveColumns = Table.RemoveColumns(AddConditionalColumn,{"Index", "Custom"}),
    ReplaceErrors1 = Table.ReplaceErrorValues(RemoveColumns, {{"TF", false}}),
    FilterRows = Table.SelectRows(ReplaceErrors1, each ([TF] = true)),
    RemoveColumns1 = Table.RemoveColumns(FilterRows,{"TF"})
in
    RemoveColumns1
 
Upvote 0
Here's another PowerQuery Solution
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"name"}, {{"AllData", each _, type table [name=text, class=text, date=text, position=nullable number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Position Condition", each if [AllData]{0}[position] = null and [AllData]{1}[position]= 1 then true else false),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Position Condition] = true)),
    #"Added Custom1" = Table.AddColumn(#"Filtered Rows", "FirstFour", each Table.FirstN([AllData],4)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"FirstFour"}),
    #"Expanded FirstFour" = Table.ExpandTableColumn(#"Removed Other Columns", "FirstFour", {"name", "class", "date", "position"}, {"name", "class", "date", "position"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded FirstFour",{{"name", type text}, {"class", type text}, {"date", type date}, {"position", Int64.Type}})
in
    #"Changed Type"
 
Upvote 0
With a little more advance PQ, you can combine the logic and First 4 rows into the Grouping, then it's just a matter of filter, remove unneeded columns and expand ;)
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"name"}, {{"AllData", each Table.FirstN(_,4), type table},{"Condition" ,each if _{0}?[position]? = null and _{1}?[position]? = 1 then true else false, type logical}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([Condition] = true)),
    #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"AllData"}),
    #"Expanded AllData" = Table.ExpandTableColumn(#"Removed Other Columns", "AllData", {"name", "class", "date", "position"}, {"name", "class", "date", "position"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded AllData",{{"name", type text}, {"class", type text}, {"date", type date}, {"position", Int64.Type}})
in
    #"Changed Type"
this also catches errors if there's only 1 row in the grouping
 
Last edited:
Upvote 0
Interesting. Never seen this kind of syntax before.
Code:
each if _{0}?[position]? = null

Here was my take on it. Just a bit different.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"name"}, {{"AllData", each _, type table [name=text, class=text, date=date, position=nullable number]}}),
    SR = Table.SelectRows(Table.TransformColumns(Group,{{"AllData", each if _[position]{0}=null and _[position]{1}=1 then Table.FirstN(_,4) else "x"}}), each _[AllData]<>"x"),
    Expand = Table.ExpandTableColumn(SR, "AllData", {"class", "date", "position"}, {"class", "date", "position"}),
    Types = Table.TransformColumnTypes(Expand,{{"name", type text}, {"class", type text}, {"date", type date}, {"position", Int64.Type}})
in
    Types
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,941
Members
452,539
Latest member
delvey

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