Forum Discussion

cflynn_29's avatar
cflynn_29
Helper I
3 years ago
Solved

Add Custom Column in GroupBy All Rows Column Based on Max Date

I have a "Test" Column which has the previous Table[s] that has one through a Groupby All Rows fucntion. In each of these nest grouped tables there is an ID number that in each group changes p...
  • edhans's avatar
    3 years ago

    This logic will work for you.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXLLL0oBUkb6Rob6RgZGxkqxOtFKRigSRggJYxQJY4SECVDE3RcibI4QNkUSNkUImyEJm0GFYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Model = _t, Date = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
        #"Grouped Rows" = 
            Table.Group(
                #"Changed Type", 
                {"Model"}, 
                {
                    {
                        "All Rows", 
                        each 
                            let
                                varMaxDate = Table.Max(_,"Date")[Date],
                                varID = Table.SelectRows(_, each [Date] = varMaxDate)[ID]{0}
                            in
                        Table.AddColumn(_, "Max ID", each varID),
                        type table [ID=nullable text, Model=nullable text, Date=nullable date, Max ID = text]
                    }
                }
            )
    in
        #"Grouped Rows"

     

    My original data is:

    I want to return ID 3 for Ford and ID 5 for GM as those have the max dates on the Model field where I grouped by it.

    You can see it works for Ford, and does for GM as well.

     

     

    Here is what I did:

    1. The grouped by All Rows table is always referenced by the _ char.
    2. I modified the default All Rows, which is normally just All Rows, each _, etc...
    3. I added two variables.
      1. varMaxDate finds the max date in the "current" table. So for Ford it found Feb 23, 2023.
      2. varID filtered the "current" table for that date, and returned the first record of the ID field in that table, which for Ford was 3.
    4. Then Table.AddColumn add the "Max ID" field to the All Rows _ table.
    5. Note the type table step must include a type for the new column or it will not show up.

    One comment - I cannot guarantee performance on larger datasets. A few thousand rows might work well. Tens of thousands, hundreds of thousands, or millions many not, and in those cases DAX is really the way to go there. If you need this on tens of millions of rows, it makes an excellent use case for a Calculated Column. Power Query just isn't super performant with table scans. DAX was designed for it.

     

    How to use M code provided in a blank query:
    1) In Power Query, select New Source, then Blank Query
    2) On the Home ribbon, select "Advanced Editor" button
    3) Remove everything you see, then paste the M code I've given you in that box.
    4) Press Done
    5) See this article if you need help using this M code in your model.

    If you need more help, please post usable data. I cannot work with images for source data.

     

    How to get good help fast. Help us help you.

    How To Ask A Technical Question If you Really Want An Answer

    How to Get Your Question Answered Quickly - Give us a good and concise explanation
    How to provide sample data in the Power BI Forum - Provide data in a table format per the link, or share an Excel/CSV file via OneDrive, Dropbox, etc.. Provide expected output using a screenshot of Excel or other image. Do not provide a screenshot of the source data. I cannot paste an image into Power BI tables.