Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Sum data to new rows in same table

Hi all,   Would appreciate some guidance on an challenge I have. Basically I'm working with some sales data that shows global countries, but for the US shows each State split out with no Total US r...
  • dax's avatar
    6 years ago

    Hi lavelle72, 

    You could refer to my sample for details(create a table for reference).

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8ixJzKlU0lGKAGJDAyBhCcQaxppKsTrRSr6ZyRmZ6Yl5QKFIIDYFYnOQtCFE2i+1XMErtag4FWRAFMgAEyABwhqmEBX+GZn5SJrNQFJmEKnggsTMPJg+U5jtGuYQ2ZDUisRiqKwFVLeGCVAuFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Market = _t, Product = _t, #"Sales Value" = _t, #"Sales Volume" = _t, COGS = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Market", type text}, {"Product", type text}, {"Sales Value", Int64.Type}, {"Sales Volume", Int64.Type}, {"COGS", Int64.Type}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Market"}, #"Table (2)", {"Market"}, "Table (2)", JoinKind.LeftOuter),
        #"Expanded Table (2)" = Table.ExpandTableColumn(#"Merged Queries", "Table (2)", {"country"}, {"Table (2).country"}),
        #"Grouped Rows" = Table.Group(#"Expanded Table (2)", {"Table (2).country", "Product"}, {{"Sales Value", each List.Sum([Sales Value]), type number}, {"Sales Volume", each List.Sum([Sales Volume]), type number}, {"COGS", each List.Sum([COGS]), type number}, {"Market", each List.Min([#"Table (2).country"]), type text}}),
        #"Filtered Rows1" = Table.SelectRows(#"Grouped Rows", each ([#"Table (2).country"] = "US")),
        #"Appended Query" = Table.Combine({#"Filtered Rows1", #"Expanded Table (2)"}),
        #"Removed Columns" = Table.RemoveColumns(#"Appended Query",{"Table (2).country"})
    in
        #"Removed Columns"

    Best Regards,
    Zoe Zhi

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.