Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Equivalent to a new column usign OVER functionality

Two columns : one is an ID column, with non-unique values.  The second column SIZES contains integers.  I want to create a third column with the maximum value of SIZES for each value of ID, without d...
  • v-kelly-msft's avatar
    v-kelly-msft
    6 years ago

    Hi Anonymous ,

     

    Using below M code:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlQ4tACOlHSUIEwDpVgdXHImeOTMwXJGeMzEKRcLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, NUMS = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"NUMS", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each let i = [ID]
    in
    Table.Max(Table.SelectRows(#"Changed Type",each [ID] = i),"NUMS")[NUMS])
    in
        #"Added Custom"

     

    And you will see:

     

    For the related .pbix file,pls click here.

     

    Best Regards,
    Kelly
    Did I answer your question? Mark my post as a solution!
  • Anonymous's avatar
    Anonymous
    6 years ago

    I've come up with a performant solution - the ones so far did not work well on 120k rows.  Basically, I created a (temporary) table of the grouped data, then merged this back into the main table.  Something like this:

    let

        Source = whatever

        Grouped = Table.Group(#"Source", col1, max(number))

        Merged = Table.Merge(#"Source", #"Grouped", using col1)

    Because this uses table processing rather than row processing, it runs pretty quickly.