Forum Discussion
Can you add a Grand Total in Power Query
- 4 years ago
Not sure how to fit this in with the rest of your query, but to add a GT row to the table you show in your most recent response, you just need to construct the row, then add it to the table:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMlQIcFEwMFTSUTI1MTQ2MlOK1UESNwKKm5hbGplZooobg8QtDMwtTFHFTcDqDSwtjZViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Year/Period" = _t, #"LA Gateway" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year/Period", type text}, {"LA Gateway", Int64.Type}}), //create grand total row gt = [#"Year/Period"="Grand Total", LA Gateway = List.Sum(#"Changed Type"[LA Gateway])], //Add the row to the bottom of the table withGT=Table.FromRecords(Table.ToRecords(#"Changed Type") & {gt}) in withGTYou could certainly create a function using this algorithm, for example, and use it to add subtotals for different groups -- perhaps feeding it the Table and the column(s) to total as a variable
I used the GUI and did the following:
1. added a new custom column with the value "1." Nothing special about 1, just wanted a constant value
2. grouped by my new column and created a count (for my purposes I wanted a count, but it could've been a sum also)
3. Appended that row to my other query
Nice to see it written out in M code but I wanted to share a beginner-friendly approach for others who stumble onto this thread...