1

My data gets updated every month so I'm trying to create a power query table that would show the sum of the pivoted (N) columns that I created but I can't seem to figure out how to do it in power query.

I have this code currently:

enter image description here

enter image description here

2 Answers 2

3
  • After Pivoting:
  • Create a list of the columns to sum
  • Add an Index column to restrict to each row
  • Add a column which Sums the columns for just that row
  • Remove the Index colum
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Month Yr", Date.Type}, {"Attribute", type text}, {"Value", Currency.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "MonthYear", each Date.ToText([Month Yr],"MMMM yyyy")),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Month Yr"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[MonthYear]), "MonthYear", "Value", List.Sum),

//NEW code added after your Pivoted Column line

//Get List of columns to sum
//  Assumes this list all columns **except the first** in the Pivot table
//  There are other methods of generating this list if this assumption is incorrect
colToSum = List.RemoveFirstN(Table.ColumnNames(#"Pivoted Column"),1),

//Add Index Column
IDX = Table.AddIndexColumn(#"Pivoted Column","Index",0,1),

//Sum each row of "colToSum"
totals = Table.AddColumn(IDX, "Sum", each List.Sum(
        Record.ToList(
            Table.SelectColumns(IDX,colToSum){[Index]})
    ), Currency.Type),
    #"Removed Columns1" = Table.RemoveColumns(totals,{"Index"})


in
#"Removed Columns1"

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Do i have to hard code this part? "Add a column which Sums the columns for just that row"
@ZyreSoriano What do you mean by "hard code"? That usually refers to creating, for example, a constant with the value of a Column Name. I don't do any of that. If you mean can that code be entered from the User Interface?, then the answer is no. You have to enter it using the Advanced Editor (from the Home tab). If you mean something else, please clarify.
@ZyreSoriano Or maybe you missed seeing this line of code below the comments explaining what that line does? colToSum = List.RemoveFirstN(Table.ColumnNames(#"Pivoted Column"),1),?
I understand how the code works now, I am now able to replicate the steps using my actual file:D
0

You can group and then merge into the table after pivoting

#"Grouped Rows" = Table.Group(#"Changed Type", {"Atribute"}, {{"Sum", each List.Sum([Value]), type number}}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Changed Type", {{"Month Year", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Changed Type", {{"Month Year", type text}}, "en-US")[#"Month Year"]), "Month Year", "Value", List.Sum),
#"Merged Queries" = Table.NestedJoin(#"Pivoted Column",{"Atribute"}, #"Grouped Rows",{"Atribute"},"Table2",JoinKind.LeftOuter),
#"Expanded Table" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Sum"}, {"Sum"})
in #"Expanded Table"

Or you can group, add it to the table, then pivot the combined new set

#"Grouped Rows" = Table.Group(#"Changed Type", {"Atribute"}, {{"Value", each List.Sum([Value]), type number}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Month Year", each "Sum"),
#"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Month Year", "Atribute", "Value"}),
combined = #"Reordered Columns" & #"Changed Type",
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(combined, {{"Month Year", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(combined, {{"Month Year", type text}}, "en-US")[#"Month Year"]), "Month Year", "Value", List.Sum)
in  #"Pivoted Column"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.