Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
AyubSherif
Frequent Visitor

Dynamic Max of each column Grouped by ID

I have a step where I am retreiving the max of each column grouped by ID (#"Grouped Rows"). I want to convert this step to a dynamic function (dynamic list of columns) that takes all the columns in the table instead of hard coded names. Here is a sample code:

 

let
table = Table.FromRecords({
[SiteID = 1, Address = "123 street", City = "C", State = null],
[SiteID = 2, Address = null, City = "C", State = "O"],
[SiteID = 1, Address = null, City = "C", State = "O"],
[SiteID = 2, Address = "234 street", City = null, State = "O"]
}),
#"Changed Type" = Table.TransformColumnTypes(table,{{"SiteID", type text}, {"Address", type text}, {"City", type text}, {"State", type text}}),
/*#"Grouped Rows" = Table.Group(#"Changed Type", {"SiteID"}, {{"Adress", each List.Max([Address]), type nullable text}, {"City", each List.Max([City]), type nullable text}, {"State", each List.Max([State]), type nullable text}}),*/
columnNames = List.RemoveMatchingItems(Record.FieldNames(#"Changed Type"{0}), {"SiteID"}),
groupedTable = Table.Group(#"Changed Type", "SiteID", List.Transform(columnNames, each {_, List.Max}))
in
groupedTable

 

 My code generates a table of error:

 

Expression.Error: We cannot apply operator < to types Record and Record.
Details:
Operator=<
Left=
SiteID=1
Address=123 street
City=C
State=
Right=
SiteID=1
Address=123 street
City=C
State=

 

 The final objective of what I am trying to remove duplicate SiteIDs and fill all the columns with none null fields if found.

To go from this:

SiteIDAddressCityState
1123 streetCnull
2nullCO
1nullCO
2234 streetnullO

 To this:

SiteIDAdressCityState
1123 streetCO
2234 streetCO
1 ACCEPTED SOLUTION
jgordon11
Resolver II
Resolver II

let
    tbl = Table.FromRecords({
        [SiteID = 1, Address = "123 street", City = "C", State = null],
        [SiteID = 2, Address = null, City = "C", State = "O"],
        [SiteID = 1, Address = null, City = "C", State = "O"],
        [SiteID = 2, Address = "234 street", City = null, State = "O"]
        }),
    tcn = List.Skip(Table.ColumnNames(tbl)),
    tbl1 = Table.Group(tbl, {"SiteID"}, {{"All", each Table.SelectColumns(_, tcn)}}),
    tbl2 = Table.TransformColumns(tbl1, {{"All", (t)=> 
        let
            lst = Table.ToColumns(t),
            lst1 = List.Transform(lst, each List.Accumulate(_, "", (s,c)=> 
                    if c <> null then 
                        let ct = Text.From(c) 
                        in (if Text.Length(ct) > Text.Length(s) then ct else s)
                    else s))
        in Table.FromRows({lst1}, tcn)
    }}),
    Result = Table.ExpandTableColumn(tbl2, "All", tcn)
in
    Result

View solution in original post

2 REPLIES 2
AyubSherif
Frequent Visitor

Thank You ❤️❤️❤️❤️

jgordon11
Resolver II
Resolver II

let
    tbl = Table.FromRecords({
        [SiteID = 1, Address = "123 street", City = "C", State = null],
        [SiteID = 2, Address = null, City = "C", State = "O"],
        [SiteID = 1, Address = null, City = "C", State = "O"],
        [SiteID = 2, Address = "234 street", City = null, State = "O"]
        }),
    tcn = List.Skip(Table.ColumnNames(tbl)),
    tbl1 = Table.Group(tbl, {"SiteID"}, {{"All", each Table.SelectColumns(_, tcn)}}),
    tbl2 = Table.TransformColumns(tbl1, {{"All", (t)=> 
        let
            lst = Table.ToColumns(t),
            lst1 = List.Transform(lst, each List.Accumulate(_, "", (s,c)=> 
                    if c <> null then 
                        let ct = Text.From(c) 
                        in (if Text.Length(ct) > Text.Length(s) then ct else s)
                    else s))
        in Table.FromRows({lst1}, tcn)
    }}),
    Result = Table.ExpandTableColumn(tbl2, "All", tcn)
in
    Result

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.