+ Reply to Thread
Results 1 to 42 of 42

New to PQ - How do I get first occurrence of a record

  1. #1
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    New to PQ - How do I get first occurrence of a record

    Hi All,

    I am new to PQ and am stumbling through.

    Attached is a very simple sheet.

    What I would like to do is use PQ to

    a) sort by name in ascending order - easy enough
    b) then sort by date in descending order - easy enough
    c) return only the first occurrence ie the most recent. - not so easy :-(

    thanks for your help

    Jeff
    Attached Files Attached Files
    Last edited by JeffGrant; 07-23-2021 at 03:18 AM.

  2. #2
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: New to PQ - How do I get first occurrence of a record

    Please try

    Please Login or Register  to view this content.
    Need to add Table.Buffer before remove duplicates


    Formula , Just for fun

    =SORT(INDEX(SORT(Table1,2,-1),MATCH(UNIQUE(Table1[Name]),INDEX(SORT(Table1,2,-1),,1),),SEQUENCE(,COLUMNS(Table1))))
    Attached Files Attached Files

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    To get this?

    Excel 2016 (Windows) 32 bit
    A
    B
    1
    Name Latest Date
    2
    Anazone
    04/07/2021
    3
    Another Gem
    25/06/2021
    4
    Asghar
    20/06/2021
    5
    Buskers Rush
    02/07/2021
    6
    Captured In Time
    15/05/2021
    7
    Moonscape
    10/07/2021
    8
    My Little Miracle
    21/06/2021
    9
    Plucky Dan
    25/06/2021
    10
    Postman Jack
    16/02/2021
    11
    Real Salty
    19/02/2021
    12
    Shamus Oh
    14/01/2021
    13
    Urjuwaan
    02/07/2021
    14
    Watch Me Nae Nae
    25/06/2021
    15
    Yee Hing Princess
    28/05/2021
    Sheet: Table1

    As easy as:

    1. Sort on name.
    2. Group on name - aggregate column on the date column set to max.

    Or vice versa!

    M Code:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by AliGW; 07-23-2021 at 03:26 AM.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  4. #4
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Thanks Bo Ry & Ali for such a quick response...

    You folk are great

    Jeff.

    PS...
    I am starting to think that my days of using VBA are somewhat numbered :-0
    Last edited by JeffGrant; 07-23-2021 at 03:30 AM.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    Glad to help.

  6. #6
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Hi Ali,

    i just did a data sort on Name in the real data set, which is some 16,000 rows deep and it returned null in every field..any idea why?


    scratch that.... for some reason blank rows in the data
    Last edited by JeffGrant; 07-23-2021 at 03:39 AM.

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    A sort will sort data, not return a null string.

    Please can you explain in more detail what you did?

    Perhaps provide a small sample that shows the problem?

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    OK - you can filter out blank rows in the query.

  9. #9
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Hi Ali,

    When I do the Group By Name, then do Max on Date, why does it return only the two columns of Name and Date?

    How do I get it to return all of the other columns as well in the full data set?

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    You either include them in the groupings OR aggregate them. Hard to say without seeing a more realistic sample dataset.

  11. #11
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    I just attached a better data set.

    Horse Name is in Column O & Meeting Date is on Column AR

    Hope that helps
    Attached Files Attached Files

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    OK - but you haven't said which columns you want to keep.

  13. #13
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    all of them

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    OK - I don't think this is the most efficient way to do this!

    Tell us the purpose and we can suggest other approaches.

  15. #15
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    The sole purpose is to keep the most recent record based on the most recent date the form meeting date column. Col AR.

    Any other records for the same horse can be removed

    All columns are required, because the source data has already been filtered via another app to get to this point.
    Last edited by JeffGrant; 07-23-2021 at 05:19 AM.

  16. #16
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    Right. Let me have a think.

  17. #17
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    so I can learn something from you - why is this not efficient?

  18. #18
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: New to PQ - How do I get first occurrence of a record

    Same m-code

    Please Login or Register  to view this content.
    Same formula

    =SORT(INDEX(SORT(Table1,2,-1),MATCH(UNIQUE(Table1[Name]),INDEX(SORT(Table1,2,-1),,1),),SEQUENCE(,COLUMNS(Table1))))

    applies to Horse name and sort by start time
    Attached Files Attached Files
    Last edited by Bo_Ry; 07-23-2021 at 05:34 AM.

  19. #19
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    Look at the attached. If it's what you want, I'll explain all.
    Attached Files Attached Files

  20. #20
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    looking now :-)

  21. #21
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    Bo-Ry's might be better.

  22. #22
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Yep, that is bang on Ali..

  23. #23
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    Look at Bo-Ry's - which do you prefer?

    I'll explain mine if you want mine.

  24. #24
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Thanks Bo Ry, but i have absolutely no idea what this is doing mate. Sorry

  25. #25
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    I had a look at the spread sheet Bo Ry created. I am not sure what he is doing, but the data is not in the format I require. Yours is.

    Most recent record on one line -> one line is what gets pick up at the next stage

  26. #26
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    Does the ALTERNATIVE tab provide you with what you want? Please look and let me know.
    Attached Files Attached Files

  27. #27
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    I have just compared everything, I may need new glass because i cant the difference between Filtered Table and Alternative :-(

  28. #28
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    Right. So the ALTERNATIVE is what Bo-Ry was trying to show you. There are three steps after loading your source query into PQ:

    1. Sort descending on form meeting day.
    2. Right click at the top of the horse name and select Remove Duplicates.
    3. Sort alphabetically on horse name.

    That's it.
    Last edited by AliGW; 07-23-2021 at 06:18 AM.

  29. #29
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    ok thanks. I'll play with it now

  30. #30
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: New to PQ - How do I get first occurrence of a record

    a) sort by horse name in ascending order - easy enough
    b) then sort by start time in descending order - easy enough
    Please Login or Register  to view this content.
    c) return only the first occurrence of horse name ie the most recent
    Please Login or Register  to view this content.

    44389.53472 => 12-Jul-21 12:50

    but the data is not in the format I require
    really?
    Attached Images Attached Images

  31. #31
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Nope. I'm Sorry. I don't get it.

    The steps you described above do not give the results that in the Alternative table.

    Almost Always, the form meeting date is not a duplicate date.

  32. #32
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    Whom are you addressing now? Me or Bo_Ry?

  33. #33
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Sorry Bo Ry, I have very little experience with PQ. It is completely foreign to me

  34. #34
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    you mate, I didn't realise Bo Ry was still in the conversation until just now :-)

  35. #35
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    My instruction at step 2 was incorrect - sorry. You remove duplicates on the horse name column (I've updated my post).

  36. #36
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    GOT IT ! - YIPPEEEEEEEE

    i stupidly removed duplicates based up on meeting date...not horse name...
    the words - I am an idiot - come to mind

  37. #37
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Just read your post......worked it out at the same time..

    Both of your are worth way more money :-)

  38. #38
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    I spent hours working on this with VBA today....looks like i'll spend more time in PQ

    anyway, thanks again....

    the Olympics Opening Ceremony is about to start

  39. #39
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    More money than diddly squat is not much.

    But you can reward us if you wish: you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  40. #40
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Did that before...

    The forum wouldn't let me give you an "Add Reputation" said, I had to share it around a bit....

  41. #41
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,647

    Re: New to PQ - How do I get first occurrence of a record

    Give some to Bo_Ry.

  42. #42
    Forum Contributor
    Join Date
    11-22-2020
    Location
    Sydney Australia
    MS-Off Ver
    365
    Posts
    202

    Re: New to PQ - How do I get first occurrence of a record

    Already did :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copying A Record/Row from an ODBC-Downloaded Query When Record Meets Criteria
    By Saabra in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-07-2020, 02:55 PM
  2. Replies: 16
    Last Post: 08-19-2019, 08:00 AM
  3. Find the Occurrence and Result of each occurrence
    By suriya0702 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-21-2019, 01:43 PM
  4. Find each occurrence in a column and do for each occurrence
    By jfoerch in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-21-2014, 12:29 PM
  5. Replies: 1
    Last Post: 11-16-2012, 09:25 AM
  6. Command Buttons for Viewing Record, Next Record and Previous Record
    By david1987 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-23-2012, 06:30 AM
  7. Multiple occurrence => 1 occurrence
    By exhortae in forum Excel General
    Replies: 4
    Last Post: 09-08-2010, 05:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1