Advance your Data & AI career with 50 days of live learning, dataviz contests, hands-on challenges, study groups & certifications and more!
Get registeredGet Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more
Person | Date | Department A Total | Department B Total |
John Smith | January | 2 | 1 |
John Smith | February| 1 | 0 |
John Smith | February| 1 | 2 |
I want to create a "New Table" from an existing query where it returns all columns except distinct for values if Person and Date, and in case of duplicate, chose the row which has higher value of "Department B Total"
So I want to return following
Person | Date | Department A Total | Department B Total |
John Smith | January | 2 | 1 |
John Smith | February| 1 | 2 |
Solved! Go to Solution.
In Power Query:
PBIX file:
Hi,
This M code works
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyAvOzSzJUNJR8krMK00sqgSyjIDYUClWB1XeLTWpCKrAEIgNCCkwUoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Person = _t, Date = _t, DepartmentATotal = _t, DepartmentBTotal = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"Date", type text}, {"DepartmentATotal", Int64.Type}, {"DepartmentBTotal", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Person", "Date"}, {{"B Total", each List.Max([DepartmentBTotal]), type number}, {"All rows", each _, type table [Person=text, Date=text, DepartmentATotal=number, DepartmentBTotal=number]}}),
#"Expanded All rows" = Table.ExpandTableColumn(#"Grouped Rows", "All rows", {"DepartmentATotal", "DepartmentBTotal"}, {"DepartmentATotal", "DepartmentBTotal"}),
#"Added Custom" = Table.AddColumn(#"Expanded All rows", "Custom", each [B Total]=[DepartmentBTotal]),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"DepartmentBTotal", "Custom"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Person", "Date", "DepartmentATotal", "B Total"})
in
#"Reordered Columns"
Hi,
This M code works
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyAvOzSzJUNJR8krMK00sqgSyjIDYUClWB1XeLTWpCKrAEIgNCCkwUoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Person = _t, Date = _t, DepartmentATotal = _t, DepartmentBTotal = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Person", type text}, {"Date", type text}, {"DepartmentATotal", Int64.Type}, {"DepartmentBTotal", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Person", "Date"}, {{"B Total", each List.Max([DepartmentBTotal]), type number}, {"All rows", each _, type table [Person=text, Date=text, DepartmentATotal=number, DepartmentBTotal=number]}}),
#"Expanded All rows" = Table.ExpandTableColumn(#"Grouped Rows", "All rows", {"DepartmentATotal", "DepartmentBTotal"}, {"DepartmentATotal", "DepartmentBTotal"}),
#"Added Custom" = Table.AddColumn(#"Expanded All rows", "Custom", each [B Total]=[DepartmentBTotal]),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"DepartmentBTotal", "Custom"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Person", "Date", "DepartmentATotal", "B Total"})
in
#"Reordered Columns"
In Power Query:
PBIX file:
Thanks, but the link is not working!
It says
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
Check out the October 2025 Power BI update to learn about new features.