Forum Discussion
Add Custom Column in GroupBy All Rows Column Based on Max Date
- 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:
- The grouped by All Rows table is always referenced by the _ char.
- I modified the default All Rows, which is normally just All Rows, each _, etc...
- I added two variables.
- varMaxDate finds the max date in the "current" table. So for Ford it found Feb 23, 2023.
- 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.
- Then Table.AddColumn add the "Max ID" field to the All Rows _ table.
- 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.
This worked out extremly well, do have any resources applicable to Nested Table Groups and Vairables? Anything would be helpful.
Thanks Again!
- edhans3 years agoCommunity Champion
I wrote a blog on this a few years ago that does a deeper dive into adding columns for a Group By operation here - Return Row Based on Max Value From One Column when Grouping — ehansalytics
As far as variables go, they are very simlar to DAX variables with few exceptions:- They need to go in the right place, which is usually after the each statement before you start with the main function.
- You start them with let and close them with in. DAX variables don't start with anything and are closed with RETURN.
- If you have 2 or more variables as I did, they all need to end with a comma except for the last one. DAX variables never end with a comma.
But just like DAX variables, they can build on any previous variables, as I did in this example, or do their own logic.