Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Weight based on multiple GROUPBY conditions

Hi Everyone,   This is my first community post, I apolgise in advance if the structure of the question is unclear (I have read throught the guidelines of asking questions so will try to stick them ...
  • BA_Pete's avatar
    4 years ago

    Hi Anonymous ,

     

    Pste this into a new blank query using Advanced Editor:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI21Dc00jcyMDIEcsLCwoCkoZGpUqwOFumIiAggaY4ka6BvYImm2cAAuzREM0zaCd3syMhIIGmGQzYqKgpIGiHJIhsN0WuBQxahNxYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [fund = _t, quarter = _t, security = _t, dollar_position = _t]),
        chgTypes = Table.TransformColumnTypes(Source,{{"fund", type text}, {"quarter", type date}, {"security", type text}, {"dollar_position", Int64.Type}}),
        groupRows = Table.Group(chgTypes, {"fund", "quarter"}, {{"data", each _, type table [fund=nullable text, quarter=nullable date, security=nullable text, dollar_position=nullable number]}, {"dollar_posn_sum", each List.Sum([dollar_position]), type nullable number}}),
        expandData = Table.ExpandTableColumn(groupRows, "data", {"security", "dollar_position"}),
        addWeight = Table.AddColumn(expandData, "weight", each [dollar_position] / [dollar_posn_sum])
    in
        addWeight

     

     

    This gives me the following output:

     

    Pete