Forum Discussion
smpa01
3 years agoCommunity Champion
Incorporating Group by Max in Table.SelectRows
I have a dataset and I want Table.SelectRows to return the rows that has max fileIndex group by file Name.
I can currently do this
let
Source = Table.FromRows(Json.Document(Binary.Decomp...
- 3 years ago
Group and filter:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSjNU0lEC4WinzLzEospYpVgdqKgRVlFjDFEjrCYYYTXBCKcJJiiisQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fileName = _t, fileIndex = _t, Content = _t]), ct = Table.TransformColumnTypes(Source,{{"fileName", type text}, {"fileIndex", Int64.Type}, {"Content", type text}}), #"Max fileIndex per filName" = Table.Group(ct,{"fileName"},{ {"filtered", each Table.FromRecords({Table.Max(_,"fileIndex" )}), type table[fileName=text, fileIndex=Int64.Type, Content=text]} }), #"Expanded filtered" = Table.ExpandTableColumn(#"Max fileIndex per filName", "filtered", {"fileIndex", "Content"}) in #"Expanded filtered" - 3 years ago
This might help. Check out my Approach #4 in particular:
Here's a version with Table.Join instead of Table.NestedJoin:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSjNU0lEC4WinzLzEospYpVgdqKgRVlFjDFEjrCYYYTXBCKcJJiiisQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fileName = _t, fileIndex = _t, Content = _t]), ct = Table.TransformColumnTypes(Source,{{"fileName", type text}, {"fileIndex", Int64.Type}, {"Content", type text}}), #"Grouped Rows" = Table.Group(ct, {"fileName"}, {{"fileIndex", each List.Max([fileIndex]), type nullable number}}), #"Merged Queries" = Table.Join(ct, {"fileName", "fileIndex"}, #"Grouped Rows", {"fileName", "fileIndex"}, JoinKind.Inner) in #"Merged Queries" - 3 years ago
In terms of performance, Table.Group() is better. All in all, try to use database to finish this task.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSjNU0lEC4WinzLzEospYpVgdqKgRVlFjDFEjrCYYYTXBCKcJJiiisQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fileName = _t, fileIndex = _t, Content = _t]), ct = Table.TransformColumnTypes(Source,{{"fileName", type text}, {"fileIndex", Int64.Type}, {"Content", type text}}), Grouped = Table.Group(ct, "fileName", {"grp", each Table.Max(_, "fileIndex")}), #"Expanded grp" = Table.ExpandRecordColumn(Grouped, "grp", {"fileIndex", "Content"}, {"fileIndex", "Content"}) in #"Expanded grp"-- for SQL Server WITH cte_rn AS (SELECT * , Row_number () OVER ( partition BY filename ORDER BY fileindex DESC) AS RN FROM yearly_sales) SELECT * FROM cte_rn WHERE rn = 1
AlexisOlson
3 years agoSuper User
This might help. Check out my Approach #4 in particular:
Here's a version with Table.Join instead of Table.NestedJoin:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSjNU0lEC4WinzLzEospYpVgdqKgRVlFjDFEjrCYYYTXBCKcJJiiisQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fileName = _t, fileIndex = _t, Content = _t]),
ct = Table.TransformColumnTypes(Source,{{"fileName", type text}, {"fileIndex", Int64.Type}, {"Content", type text}}),
#"Grouped Rows" = Table.Group(ct, {"fileName"}, {{"fileIndex", each List.Max([fileIndex]), type nullable number}}),
#"Merged Queries" = Table.Join(ct, {"fileName", "fileIndex"}, #"Grouped Rows", {"fileName", "fileIndex"}, JoinKind.Inner)
in
#"Merged Queries"