Remove columns from output if their total is 0

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a PQ process that takes an input table, removes some columns, reorders them, then produces an output table:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl_Input"]}[Content],
    #"Removed Columns" = Table.RemoveColumns(Source,{"NSV Estimate", "RPC","Total Retro", "Total", "NNSV / Case", "% TTS", "Investment", "% Investment"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Brand", "SKU", "Stocking Policy", "Stocking Policy Description", "Volume", "Retro", "Lump Sum", "A&P"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns",{{"Brand", type text}, {"SKU", type text}, {"Stocking Policy", type text}, {"Stocking Policy Description", type text}, {"Volume", Int64.Type}, {"Retro", Currency.Type}, {"Lump Sum", Currency.Type}, {"A&P", Currency.Type}})
in
    #"Changed Type"

I would like to exclude columns Lump Sum and A&P (the last two columns in the table), if the column total = 0, their values will always be non-negative.

How can this be done please?

TIA,
Jack
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Book2
ABCDEFG
1ABCDAB
2470047
3200020
4320032
5940094
6990099
7930093
8270027
9110011
10250025
1110200102
Sheet1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangedType = Table.TransformColumnTypes(Source,{{"D", type number}, {"C", type number}, {"B", type number}, {"A", type number}}),
    CSum = List.Sum(Table.Column(ChangedType, "C")),
    DSum = List.Sum(Table.Column(ChangedType, "D")),
    CRemove = if (CSum = 0) then Table.RemoveColumns(ChangedType,{"C"}) else ChangedType,
    DRemove = if (DSum = 0) then Table.RemoveColumns(CRemove,{"D"}) else CRemove

in
    DRemove

Book2
ABCDEFGH
1ABCDABD
24700470
32000200
43213323
59404944
699-15995
79300930
82700270
91100110
102500250
11102001020
Sheet1
 
Upvote 0
In your code

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl_Input"]}[Content],
    #"Removed Columns" = Table.RemoveColumns(Source,{"NSV Estimate", "RPC","Total Retro", "Total", "NNSV / Case", "% TTS", "Investment", "% Investment"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Brand", "SKU", "Stocking Policy", "Stocking Policy Description", "Volume", "Retro", "Lump Sum", "A&P"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Reordered Columns",{{"Brand", type text}, {"SKU", type text}, {"Stocking Policy", type text}, {"Stocking Policy Description", type text}, {"Volume", Int64.Type}, {"Retro", Currency.Type}, {"Lump Sum", Currency.Type}, {"A&P", Currency.Type}}),
    LumpSumSum = List.Sum(Table.Column(#"Changed Type", "Lump Sum")),
    APSum = List.Sum(Table.Column(#"Changed Type", "A&P")),
    LSRemove = if (LumpSumSum = 0) then Table.RemoveColumns(#"Changed Type",{"Lump Sum"}) else #"Changed Type",
    APRemove = if (APSum = 0) then Table.RemoveColumns(LSRemove,{"A&P"}) else LSRemove
in
    APRemove

may have a few typos because I can't test it without your spreadsheet
 
Upvote 0
Solution
Hi @JGordon11 thank you for the suggestion, I'll modify it accordingly but I can see what you've tried to do and how it's meant to work.
I'll mark this solution as complete after I've had a chance to test, but this looks like the solution I was hoping for - thank you!
 
Upvote 0
Hi @JGordon11

Needed to replace nulls with 0 and then it worked, thank you very much and Happy Easter!

Final code (changed order of some of the other steps whilst trying to get it to work):

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl_Input"]}[Content],
    #"Removed Columns" = Table.RemoveColumns(Source,{"NSV Estimate", "RPC","Total Retro", "Total", "NNSV / Case", "% TTS", "Investment", "% Investment"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Brand", type text}, {"SKU", type text}, {"Stocking Policy", type text}, {"Stocking Policy Description", type text}, {"Volume", Int64.Type}, {"Retro", Currency.Type}, {"Lump Sum", Currency.Type}, {"A&P", Currency.Type}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Changed Type",{"Brand", "SKU", "Stocking Policy", "Stocking Policy Description", "Volume", "Retro", "Lump Sum", "A&P"}),
    #"Replaced Value" = Table.ReplaceValue(#"Reordered Columns",null,0,Replacer.ReplaceValue,{"Lump Sum", "A&P"}),
    LumpSumSum = List.Sum(Table.Column(#"Replaced Value", "Lump Sum")),
    APSum = List.Sum(Table.Column(#"Replaced Value", "A&P")),
    LSRemove = if (LumpSumSum = 0) then Table.RemoveColumns(#"Replaced Value",{"Lump Sum"}) else #"Replaced Value",
    APRemove = if (APSum = 0) then Table.RemoveColumns(LSRemove,{"A&P"}) else LSRemove
in
    APRemove
 
Upvote 0
Power Query:
= Table.PromoteHeaders(Table.FromColumns(List.Select(Table.ToColumns(Table.DemoteHeaders(Source)),each List.Sum(List.Skip(_))<>0)))
1617623745914.png
 
Upvote 0
Thank you @shaowu459, I think I understand what you're suggesting, but I only need to test two columns "Lump Sum" and "A&P" - or I have implemented your suggestion incorrectly.

This is what I have:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl_Input"]}[Content],
    /*#"Removed Columns" = Table.RemoveColumns(Source,{"NSV Estimate", "RPC","Total Retro", "Total", "NNSV / Case", "% TTS", "Investment", "% Investment"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Brand", type text}, {"SKU", type text}, {"Stocking Policy", type text}, {"Stocking Policy Description", type text}, {"Volume", Int64.Type}, {"Retro", Currency.Type}, {"Lump Sum", Currency.Type}, {"A&P", Currency.Type}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Changed Type",{"Brand", "SKU", "Stocking Policy", "Stocking Policy Description", "Volume", "Retro", "Lump Sum", "A&P"}),
    #"Replaced Value" = Table.ReplaceValue(#"Reordered Columns",null,0,Replacer.ReplaceValue,{"Lump Sum", "A&P"}),*/
    #"Output" = Table.PromoteHeaders(Table.FromColumns(List.Select(Table.ToColumns(Table.DemoteHeaders(Source)),each List.Sum(List.Skip(_))<>0)))
    /*#"Step 6" = Table.FromColumns(List.Select(Table.ToColumns(#"Replaced Value"), each List.Sum(List.Skip(_)<>0)))
    LumpSumSum = List.Sum(Table.Column(#"Replaced Value", "Lump Sum")),
    APSum = List.Sum(Table.Column(#"Replaced Value", "A&P")),
    LSRemove = if (LumpSumSum = 0) then Table.RemoveColumns(#"Replaced Value",{"Lump Sum"}) else #"Replaced Value",
    APRemove = if (APSum = 0) then Table.RemoveColumns(LSRemove,{"A&P"}) else LSRemove*/
in
    /*APRemove*/
    #"Output"

However, I get an expression error: We cannot apply operator - to types Text and Text. I believe this is because it's testing a non numerical column.

I tried to change Step 6 to have a list of {"Lump Sum", "A&P"} but I must have had the syntax wrong as I couldn't get it to work.
 
Upvote 0
Hi, if you only have two specific columns to test, JGordon11's solution is a good choice. I made my code using the sample data in post #2, so my solution may be not suitable to you source data.
 
Upvote 0
Thanks for the clarification yes I thought it was looping over all the columns but appreciate posting as given me something else to learn.

Condescend it a bit more, for completeness, final code:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tbl_Input"]}[Content],
    #"Remove Columns" = Table.RemoveColumns(Source,{"NSV Estimate", "RPC","Total Retro", "Total", "NNSV / Case", "% TTS", "Investment", "% Investment"}),
    #"Force Data Type" = Table.TransformColumnTypes(#"Remove Columns",{{"Brand", type text}, {"SKU", type text}, {"Stocking Policy", type text}, {"Stocking Policy Description", type text}, {"Volume", Int64.Type}, {"Retro", Currency.Type}, {"Lump Sum", Currency.Type}, {"A&P", Currency.Type}}),    
    #"Replace null values" = Table.ReplaceValue(Table.ReorderColumns(#"Force Data Type",{"Brand", "SKU", "Stocking Policy", "Stocking Policy Description", "Volume", "Retro", "Lump Sum", "A&P"}),null,0,Replacer.ReplaceValue,{"Lump Sum", "A&P"}),        
    #"Lump Zero Sum Remove" = if (List.Sum(Table.Column(#"Replace null values", "Lump Sum")) = 0) then Table.RemoveColumns(#"Replace null values",{"Lump Sum"}) else #"Replace null values",
    #"AP Zero Sum Remove" = if (List.Sum(Table.Column(#"Replace null values", "A&P")) = 0) then Table.RemoveColumns(#"Lump Zero Sum Remove",{"A&P"}) else #"Lump Zero Sum Remove",
    Output = #"AP Zero Sum Remove"
in
    Output
 
Upvote 0

Forum statistics

Threads
1,223,721
Messages
6,174,096
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top