As @ScottCraner implied, you can obtain your desired output using Power Query, available in Windows Excel 2010+ and Office 365 Excel
- Select some cell in your original table
Data => Get&Transform => From Table/Range
- When the PQ UI opens, navigate to
Home => Advanced Editor
- Make note of the Table Name in Line 2 of the code.
- Replace the existing code with the M-Code below
- Change the table name in line 2 of the pasted code to your "real" table name
- Examine any comments, and also the
Applied Steps window, to better understand the algorithm and steps
Note that your "conditions", when applied to the unPivot table, are the same as
- Filter out the zero values
- Multiple the "Pay" values by
-1
M Code
let
Source = Excel.CurrentWorkbook(){[Name="Table33"]}[Content],
//delete unneeded columns
#"Removed Columns" = Table.RemoveColumns(Source,{"Name", "State"}),
//set data type
typeIt = Table.TransformColumnTypes(#"Removed Columns",{
{"Type", Text.Type},
{"ID", Text.Type},
{"X", Int64.Type},
{"Y", Int64.Type}
}),
//unPivot, then remove the rows with zeros's
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(typeIt, {"Type", "ID"}, "Attribute", "Value"),
#"Filtered Rows" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> 0)),
//add column where Pay amount are multiplied by -1
//remove unneeded Value column
//Sort and reorder the columns
#"Added Custom" = Table.AddColumn(#"Filtered Rows", "#", each if [Type]="Pay" then [Value] * -1 else [Value]),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Value"}),
#"Sorted Rows" = Table.Sort(#"Removed Columns1",{{"Attribute", Order.Ascending}, {"ID", Order.Ascending}}),
#"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"Type", "ID", "#", "Attribute"})
in
#"Reordered Columns"
