Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Average and sum while pivot column

Hello everyone,   I am trying to pivot a table with similar data as below to have the months in column.   Country Attribute Month Value Country 1 Amount 2020-01 100 Country 1 Qt...
  • mahoneypat's avatar
    5 years ago

    Here is one way to do it with Group By before the Pivot.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs4vzSspqlQwVNJRcswFcYAMIwMjA10DkJChgYFSrA6qssCSShQ1RqYYSoISS1JR1BjoYVGFxT4jIuwzxVRC2D4jTPuMsPjPCM0+EN/YDEMJin1GYPtAqmIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Country = _t, Attribute = _t, Month = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}, {"Attribute", type text}, {"Month", type date}, {"Value", type number}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Country", "Month", "Attribute"}, {{"ValueSum", each List.Sum([Value]), type nullable number}, {"ValueCount", each Table.RowCount(_), Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Value", each if [Attribute] = "Rate" then [ValueSum]/[ValueCount] else [ValueSum], type number),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"ValueSum", "ValueCount"}),
        #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "Value")
    in
        #"Pivoted Column"

    Regards,

    Pat

  • Jimmy801's avatar
    5 years ago

    Hello Anonymous 

     

    there is no way to do this with Pivot but with Table.Group. You can pack all your requirements in the functions of Table.Group. The trick is to filter the grouped table before summing or averaging. Here an example

    let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs4vzSspqlQwVNJRcswFcYAMIwMjA10DkJChgYFSrA6qssCSShQ1RqYYSoISS1JR1BjoYVGFxT4jIuwzxVRC2D4jTPuMsPjPCM0+EN/YDEMJin1GYPtAqmIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Country = _t, Attribute = _t, Month = _t, Value = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}, {"Attribute", type text}, {"Month", type text}, {"Value", type number}}, "en-US"),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Country", "Month"}, {{"Amount", each List.Sum(Table.SelectRows(_,each [Attribute]="Amount")[Value]), type number}, {"Qty", each List.Sum(Table.SelectRows(_,each [Attribute]="Qty")[Value]), type number}, {"Rate", each List.Average(Table.SelectRows(_,each [Attribute]="Rate")[Value]), type number}})
    in
        #"Grouped Rows"

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works. 

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy