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:
Irobbo314 -
Code:
each if _{0}?[position]? = null
This is basically shorthand for "If Not Exists return null" you can read about it here: Referencing Individual Cell Values From Tables In Power Query
I only needed it on my second row reference, it's unneeded in the part quoted.

Due to that, your query will produce an error row if an entry only has 1 item, so if you change your SR line as such, it will "catch" that error:
Power Query:
 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"),
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Thank you all for you contributions. I apologise for the delay in response. I have been away for work and internet connectivity is a bit scarce.
 
Upvote 0
Please try


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Combine(List.RemoveNulls(Table.Group(Source, {"name"}, {{"T", each  if  [position]{0}= null and [position]{1}= 1 then Table.FirstN(_,4) else null}})[T]))
in
    Group
 
Last edited:
Upvote 0
Please try


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Combine(List.RemoveNulls(Table.Group(Source, {"name"}, {{"T", each  if  [position]{0}= null and [position]{1}= 1 then Table.FirstN(_,4) else null}})[T]))
in
    Group
FYI this solution fails if there is only one record for a name. Changing the if condition will fix it:
Power Query:
= Table.Combine(List.RemoveNulls(Table.Group(Source, {"name"}, {{"T", each  if  [position]{0}= null and [position]?{1}?= 1 then Table.FirstN(_,4) else null}})[T]))
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,936
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