Forum

If total columns &g...
 
Notifications
Clear all

If total columns > x then

4 Posts
2 Users
0 Reactions
60 Views
(@lanser)
Posts: 61
Estimable Member
Topic starter
 

Is it possible to do if then in power query i.e. 

If there are more than 4 columns then delete column names >4

else continue processing

?

regards

John

 
Posted : 02/02/2021 11:51 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi John,

Yes, you don't even need to check if there are more than 4 columns, just select the columns you want and then right click on a selected column header -> Remove other columns

See this example file.

Regards

Phil

.

remove other columns

 
Posted : 02/02/2021 7:23 pm
(@lanser)
Posts: 61
Estimable Member
Topic starter
 

Hi Phil delete was probably the wrong action to include, as a follow up question how about the opposite.

If there are 6 columns do x if there are > 6 do y or if 4 columns do z

 

Basically I want to merge every two columns after the first two so 3+4 5+6 the files are pdfs and I have no control over how many columns it might have so at the moment it errors out if 5 and 6 are missing and there is always a final total column so an odd number and I delete that final column using your suggestion.

 

John

 
Posted : 05/02/2021 9:46 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi John,

See attached file - This code merges pairs of columns as you describe : 3+4, 5+6 etc and will work for any number of columns providing it's an odd number as you said the last col will always be a total. It also removes the last column regardless of how many columns there are in the table.

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Names = Table.ColumnNames(Source),

MergedCols = List.Generate(

() => [x = Source, y = 2 ],

each [y] < List.Count(Names),

each [x = Table.CombineColumns([x], {Names{[y]}, Names{[y]+1}}, Combiner.CombineTextByDelimiter("", QuoteStyle.None), "Merged"&Text.From(y)), y = [y] + 2],

each [x]

),

MergedTable = MergedCols{List.Count(MergedCols)-1},
#"Removed Columns" = Table.RemoveColumns( MergedTable, Table.ColumnNames(MergedTable){List.Count( Table.ColumnNames(MergedTable)) -1})
in
#"Removed Columns"

 

Regards

Phil

 
Posted : 10/02/2021 11:22 pm
Share: