Maximum Dates of each product in a list with multiple entries

Lucas Salomons 26 Reputation points
2021-06-11T13:47:17.43+00:00

Hi,

I´m trying to get the maximum date of each product in a list. An example of the data with the correct result in the third row is as follows:

104796-image.png

Any ideas? I´m familiar with excel, but still new to power query...

Thanks in advance!

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
38,701 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lz._ 9,006 Reputation points
    2021-06-15T13:09:50.813+00:00

    Hi @LucasSalomons-0933

    No worries at all. As you seem to be new on the forum: when a proposal solves your problem, it's a good habit to accept it as answer in order to help those who search before posting

    Below is another option that is easier to understand for someone who begins with PQ where, inside Table.Group I decomposed the steps, hopefully with names that talk:

    let  
        // Table for demo.  
        Source = Table.FromColumns(  
            {  
                List.Dates(#date(2020,7,2), 15, #duration(2,0,0,0)),  
                List.Repeat({"A"}, 4) & List.Repeat({"B"}, 6)  
                    & List.Repeat({"C"}, 5)  
            },  
            type table [Date = date, Product = text]  
        ),  
        GroupedProduct = Table.Group(Source, {"Product"},  
            {"TBL", each  
                let  
                    DatesAsList = Table.Column(_, "Date"),  
                    MaxInList = List.Max(DatesAsList),  
                    AddedColumn = Table.AddColumn(_, "Date Max", each  
                        MaxInList, type date  
                    )  
                in  
                    AddedColumn,  
                type table  
            }  
        ),  
        CombinedNestedTables = Table.Combine(GroupedProduct[TBL])  
    in  
        CombinedNestedTables  
    

    Any question let me know - not to explain every steps though :) as you're right a bit of home work is required

    Just in case, PQ documentation is available here
    Cheers

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Lz._ 9,006 Reputation points
    2021-06-11T16:10:06.037+00:00

    Hi @LucasSalomons-0933

    One way:

    let
        // Table for demo.
        Source = Table.FromColumns(
            {
                List.Dates(#date(2020,7,2), 15, #duration(2,0,0,0)),
                List.Repeat({"A"}, 4) & List.Repeat({"B"}, 6)
                    & List.Repeat({"C"}, 5)
            },
            type table [Date = date, Product = text]
        ),
        GroupedProduct = Table.Group(Source, {"Product"},
            {"TBL", (state)=>
                Table.AddColumn(state, "Date Max", each
                    Table.Max(state, {"Date"})[Date], type date
                ),
                type table
            }
        ),
        CombinedNestedTables = Table.Combine(GroupedProduct[TBL])
    in
        CombinedNestedTables
    

    If you're new to PQ, search the former forum (and this one), it's a huge source of information & solved cases
    Also, if you need some "learning" pointers let me know


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.