Forum Discussion

Troyvan's avatar
Troyvan
Helper I
4 years ago
Solved

Adding columns subtotal

Hi All, 

 

Apologies if this has been answered previously. I'm trying to creat a column showing total per quarter but I can't seem to add selected column totals here. My data looks like this: 

Job NoClientLedgerJan-22Feb-22Mar-22Apr-22May-22Jun-22Jul-22Aug-22Sep-22Oct-22Nov-22Dec-22
1Client1Travel 50505050505050100100100100100
2Client2Subcontractors100100100100100100100600600600600600
3Client3Subcontractors100100100100100100100200200200200200
4Client4Travel & Accommodation 1501501501501501501502,5002,5002,5002,5002,500
5Client5Subcontractors15,00015,00015,00015,00015,00015,00015,000100100100100100
Total  15,40015,40015,40015,40015,40015,40015,4003,5003,5003,5003,5003,500

 

How can I simply add total of every 3 months to create quarterly totals? Thanks! 

 

Cheers,
Troy

  • See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXLOyUzNKwGxQooSy1JzgAxTA4KEoQFuMlYnWskIbjKIFVyalJyfV1KUmFySX1RMQDsqaYaHBFlkDLfImDKLjPCQIItM4BaZwMNKIabUwMDITMExOTk/Nzc/JbEkMz8PZCIkjIggjXRMDQjTIPtN4fabYvWoqY4BxDfkMfDFZywA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Job No" = _t, Client = _t, Ledger = _t, #"Jan-22" = _t, #"Feb-22" = _t, #"Mar-22" = _t, #"Apr-22" = _t, #"May-22" = _t, #"Jun-22" = _t, #"Jul-22" = _t, #"Aug-22" = _t, #"Sep-22" = _t, #"Oct-22" = _t, #"Nov-22" = _t, #"Dec-22" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Job No", Int64.Type}, {"Client", type text}, {"Ledger", type text}, {"Jan-22", Int64.Type}, {"Feb-22", Int64.Type}, {"Mar-22", Int64.Type}, {"Apr-22", Int64.Type}, {"May-22", Int64.Type}, {"Jun-22", Int64.Type}, {"Jul-22", Int64.Type}, {"Aug-22", Int64.Type}, {"Sep-22", Int64.Type}, {"Oct-22", Int64.Type}, {"Nov-22", Int64.Type}, {"Dec-22", Int64.Type}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Ledger", "Client", "Job No"}, "Attribute", "Value"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Attribute", type date}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type1", "Quarter", each "Q"&Text.From(Number.RoundUp(Date.Month([Attribute])/3,0))),
        #"Grouped Rows" = Table.Group(#"Added Custom", {"Quarter"}, {{"Total", each List.Sum([Value]), type number}})
    in
        #"Grouped Rows"

     

2 Replies

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXLOyUzNKwGxQooSy1JzgAxTA4KEoQFuMlYnWskIbjKIFVyalJyfV1KUmFySX1RMQDsqaYaHBFlkDLfImDKLjPCQIItM4BaZwMNKIabUwMDITMExOTk/Nzc/JbEkMz8PZCIkjIggjXRMDQjTIPtN4fabYvWoqY4BxDfkMfDFZywA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Job No" = _t, Client = _t, Ledger = _t, #"Jan-22" = _t, #"Feb-22" = _t, #"Mar-22" = _t, #"Apr-22" = _t, #"May-22" = _t, #"Jun-22" = _t, #"Jul-22" = _t, #"Aug-22" = _t, #"Sep-22" = _t, #"Oct-22" = _t, #"Nov-22" = _t, #"Dec-22" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Job No", Int64.Type}, {"Client", type text}, {"Ledger", type text}, {"Jan-22", Int64.Type}, {"Feb-22", Int64.Type}, {"Mar-22", Int64.Type}, {"Apr-22", Int64.Type}, {"May-22", Int64.Type}, {"Jun-22", Int64.Type}, {"Jul-22", Int64.Type}, {"Aug-22", Int64.Type}, {"Sep-22", Int64.Type}, {"Oct-22", Int64.Type}, {"Nov-22", Int64.Type}, {"Dec-22", Int64.Type}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Ledger", "Client", "Job No"}, "Attribute", "Value"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Attribute", type date}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type1", "Quarter", each "Q"&Text.From(Number.RoundUp(Date.Month([Attribute])/3,0))),
        #"Grouped Rows" = Table.Group(#"Added Custom", {"Quarter"}, {{"Total", each List.Sum([Value]), type number}})
    in
        #"Grouped Rows"

     

  • It worked! Thank you so much Vijay 🙂

     

    Cheers,
    Troy