Forum Discussion
Relationship or Group by?
- 6 years ago
See if this works for you. Here is the M code, which you can paste into a blank query using the Advanced Editor. All of that binary gibberish on the first row is the table data you provided. You can click on the gear icon in the query view to see the table. And apologies if I completely butchered the titles. The weren't aligned in the data you provided and I don't speak French. I barely speak English well. 😉
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZNNbtswEIWvQnhNA/yns3QSoEBht4ZTdBNkwUgDizVFuvpx4Z6oRo+hi3WopN1lUWYhYASOPr7Re/P4uOCMGy0WdMHxqYLreyBaMny5BxIc2cHgB1geXAe5yUjJtMVqHU5Nin2i5D61PvrquHii/4tbrThTmXabxqpxXU3J1nWXHgpYszR+g9XGxbq7LNchvBBRm4NQgrQrrrjC6gGOJ6Bk706Nm37/ZQku+CvrQ5fGExAp3mIJYbSx+eo1qvsJ3+BHf/SUPDQQni8FQGsMY1nbuushujEMlHwO/uyhK5GnrTU38+k4gBsp+epjBXEoYHHNlTVqdqJNQ4M/rr103rUlumTWZV9cPcDZR0o+uXr6dYASZUwZpSVW2xSgx+yioXG6FrHylFbPrAjDgFNup2tItS+iKcmkysp2rv8+uoArRcnG5SbvSoDcWCOyCbvUdekMVdV43Na7MF1bQGsLmEoboTNy7wYIASf+CC7GfyjJ7SuKCb1UnC23LZDNJcJb7hrNb1j+4BbzEXFZ75qQpmsxUAspuZ4P+2cPES3ep+hC/S6iymu7CxjmWCcM4Nz+rplFdvoLJpqSdYvb//QH", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Comm = _t, Ecole = _t, NomCommunaute = _t, Moyenne = _t, Address = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Comm", Int64.Type}, {"Ecole", type text}, {"NomCommunaute", type text}, {"Moyenne", Int64.Type}, {"Address", type text}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"All Rows", each _, type table [ID=number, Comm=number, Ecole=text, NomCommunaute=text, Moyenne=number, Address=text]}}), #"Added Min Row Table" = Table.AddColumn(#"Grouped Rows", "Min Row Table", each Table.Min([All Rows], "Moyenne")), #"Expanded Min Row Table" = Table.ExpandRecordColumn(#"Added Min Row Table", "Min Row Table", {"Comm", "Ecole", "NomCommunaute", "Moyenne", "Address"}, {"Comm", "Ecole", "NomCommunaute", "Moyenne", "Address"}), #"Removed Other Columns" = Table.SelectColumns(#"Expanded Min Row Table",{"ID", "Comm", "Ecole", "NomCommunaute", "Moyenne", "Address"}), #"Changed Type1" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Comm", Int64.Type}, {"Moyenne", Int64.Type}, {"Ecole", type text}, {"NomCommunaute", type text}, {"Address", type text}}) in #"Changed Type1"The resultant table looks like this:
What I did was:
- Grouped all of the data by the ID column, but in the New Column, I used the All Rows operation, so it created a table per ID.
- I then created a custom column with the following formula: Table.Min([All Rows], "Moyenne") - This will extract a single record based on the minimum value of the Moyenne field.
- I expanded all fields in that record except the ID field, which already exists in the parent table.
- I then cleaned it up by removing the columns no longer needed and re-type'd the data. When you use nested tables, the data type gets lost and usually everything comes back out as type ABC123.
- That then gets loaded to the DAX data model to do whatever reporting you need.
- Grouped all of the data by the ID column, but in the New Column, I used the All Rows operation, so it created a table per ID.
See if this works for you. Here is the M code, which you can paste into a blank query using the Advanced Editor. All of that binary gibberish on the first row is the table data you provided. You can click on the gear icon in the query view to see the table. And apologies if I completely butchered the titles. The weren't aligned in the data you provided and I don't speak French. I barely speak English well. 😉
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZNNbtswEIWvQnhNA/yns3QSoEBht4ZTdBNkwUgDizVFuvpx4Z6oRo+hi3WopN1lUWYhYASOPr7Re/P4uOCMGy0WdMHxqYLreyBaMny5BxIc2cHgB1geXAe5yUjJtMVqHU5Nin2i5D61PvrquHii/4tbrThTmXabxqpxXU3J1nWXHgpYszR+g9XGxbq7LNchvBBRm4NQgrQrrrjC6gGOJ6Bk706Nm37/ZQku+CvrQ5fGExAp3mIJYbSx+eo1qvsJ3+BHf/SUPDQQni8FQGsMY1nbuushujEMlHwO/uyhK5GnrTU38+k4gBsp+epjBXEoYHHNlTVqdqJNQ4M/rr103rUlumTWZV9cPcDZR0o+uXr6dYASZUwZpSVW2xSgx+yioXG6FrHylFbPrAjDgFNup2tItS+iKcmkysp2rv8+uoArRcnG5SbvSoDcWCOyCbvUdekMVdV43Na7MF1bQGsLmEoboTNy7wYIASf+CC7GfyjJ7SuKCb1UnC23LZDNJcJb7hrNb1j+4BbzEXFZ75qQpmsxUAspuZ4P+2cPES3ep+hC/S6iymu7CxjmWCcM4Nz+rplFdvoLJpqSdYvb//QH", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Comm = _t, Ecole = _t, NomCommunaute = _t, Moyenne = _t, Address = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Comm", Int64.Type}, {"Ecole", type text}, {"NomCommunaute", type text}, {"Moyenne", Int64.Type}, {"Address", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"All Rows", each _, type table [ID=number, Comm=number, Ecole=text, NomCommunaute=text, Moyenne=number, Address=text]}}),
#"Added Min Row Table" = Table.AddColumn(#"Grouped Rows", "Min Row Table", each Table.Min([All Rows], "Moyenne")),
#"Expanded Min Row Table" = Table.ExpandRecordColumn(#"Added Min Row Table", "Min Row Table", {"Comm", "Ecole", "NomCommunaute", "Moyenne", "Address"}, {"Comm", "Ecole", "NomCommunaute", "Moyenne", "Address"}),
#"Removed Other Columns" = Table.SelectColumns(#"Expanded Min Row Table",{"ID", "Comm", "Ecole", "NomCommunaute", "Moyenne", "Address"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Comm", Int64.Type}, {"Moyenne", Int64.Type}, {"Ecole", type text}, {"NomCommunaute", type text}, {"Address", type text}})
in
#"Changed Type1"
The resultant table looks like this:
What I did was:
- Grouped all of the data by the ID column, but in the New Column, I used the All Rows operation, so it created a table per ID.
- I then created a custom column with the following formula: Table.Min([All Rows], "Moyenne") - This will extract a single record based on the minimum value of the Moyenne field.
- I expanded all fields in that record except the ID field, which already exists in the parent table.
- I then cleaned it up by removing the columns no longer needed and re-type'd the data. When you use nested tables, the data type gets lost and usually everything comes back out as type ABC123.
- That then gets loaded to the DAX data model to do whatever reporting you need.
It worked!!!! 😀😁
I pasted your M code but only show me the 3 rows I asked you. But my real table was longuer than that, so I reproduced the 5 steps.
I have learned about Table.Min.
Thank you so much edhans
- edhans6 years ago
Community Champion
Great! I use Table.Min and Table.Max all of the time for this purpose.