Forum Discussion

KDS's avatar
KDS
Helper I
4 years ago
Solved

Revising code for subtotals

Hello all -    I found some code for creating subtotals in my data which works nicely except for how it's grouping information.  I want the subtotals to be on each change in Teams, not in each chan...
  • PhilipTreacy's avatar
    4 years ago

    Hi KDS 

    Please provide sample data/file when asking questions so we don't have to recreate everything by hand.  Without data it takes longer for you to get a reply.

     

    Download sample PBIX with solutions.

     

    You can do this a number of ways, in the attached file are 2 approaches, one more dynamic than the other.

     

    Firstly, you don't need the Totals rows so delete them.  Totals can be calculatd later based on the values in the other columns.

    Next I'd also advise that you use a Date column that contains a date for each record, rather than having diferent columns for each month.

     

    That said, given the data you've screenshotted, you can select the Team, Group and Desc columns then Group By and sum the Month columns.  This isn't dynamic in that if you add more month columns it won't adapt.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("tZMxD4MgEIX/SsPscJygdO7g2KFuxoGhaUgMGLX/vyKYWtOCpu0CyfHx3oODqiInkhCkADDOZSd135pukIMy2i64OpA6iZAMNqO2SLldnNBDaQbZBLV9jMUmi3OccaWVvnnlIOAKGFYRmchzFpOiKWecHWOYADd8k3qs+HF1YW8OiMgoppOnw4uY6QcAF+0sgpaw0spcty73tm3UtX81eyqFsHPwUTLYSsJOEucH9iPZf7hT4T6D2Bh1d9L4h6S+BXzKUD8A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Team = _t, Group = _t, Description = _t, Oct = _t, Nov = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Team", type text}, {"Group", Int64.Type}, {"Description", type text}, {"Oct", Int64.Type}, {"Nov", Int64.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each not Text.Contains([Team], "Total")),
        #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Team", "Group", "Description"}, {{"Oct", each List.Sum([Oct]), type nullable number}, {"Nov", each List.Sum([Nov]), type nullable number}})
    in
        #"Grouped Rows"

     

     

    A dynamic approach is to select the Team, Group and Desc columns then Unpivot Other Columns.  Now you can Group By the Team, Group, Desc and Attribute (month) columns, summing the Values.

    Finally Pivot the Attribute column to restore individual month columns

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("tZMxD4MgEIX/SsPscJygdO7g2KFuxoGhaUgMGLX/vyKYWtOCpu0CyfHx3oODqiInkhCkADDOZSd135pukIMy2i64OpA6iZAMNqO2SLldnNBDaQbZBLV9jMUmi3OccaWVvnnlIOAKGFYRmchzFpOiKWecHWOYADd8k3qs+HF1YW8OiMgoppOnw4uY6QcAF+0sgpaw0spcty73tm3UtX81eyqFsHPwUTLYSsJOEucH9iPZf7hT4T6D2Bh1d9L4h6S+BXzKUD8A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Team = _t, Group = _t, Description = _t, Oct = _t, Nov = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Team", type text}, {"Group", Int64.Type}, {"Description", type text}, {"Oct", Int64.Type}, {"Nov", Int64.Type}}),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type", each not Text.Contains([Team], "Total")),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Filtered Rows", {"Team", "Group", "Description"}, "Attribute", "Value"),
        #"Sorted Rows" = Table.Sort(#"Unpivoted Other Columns",{{"Attribute", Order.Descending}, {"Team", Order.Ascending}, {"Group", Order.Ascending}}),
        #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Team", "Group", "Description", "Attribute"}, {{"Total", each List.Sum([Value]), type number}}),
        #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Attribute]), "Attribute", "Total", List.Sum)
    in
        #"Pivoted Column"

     

     

    regards

     

    Phil