Forum Discussion
Power Query by Default Excludes First Blank Column
- Mar 24, 2025
With Excel is practically the same, the only you have to define sheet name to combine
let SheetToSelect = "Test", NumberOfColumns = 4, NewNames = List.Zip( { List.Transform( {1..NumberOfColumns-1}, (q) => "Column" & Text.From(q) ), List.Transform( {2..NumberOfColumns}, (q) => "Column" & Text.From(q) ) } ), Source = Excel.Workbook(Parameter1, null, true){[Item=SheetToSelect,Kind="Sheet"]}[Data], n = Table.ColumnCount(Source), AdjustColumns = if n < NumberOfColumns then Table.RenameColumns( Source, NewNames ) else Source in AdjustColumns
If to assume you combine csv files, for them Power Query removes blank columns before first one with data. And after last one with data if number of columns is not specified. If you check Transform Sample File query, it could look like
let
Source = Csv.Document(Parameter1,[Delimiter=",", Columns=4, Encoding=65001, QuoteStyle=QuoteStyle.None])
in
Source
where options, in particular number of columns, are automatically taken from the analysis of first sample file. Resulting table by default has headers Column1, Column2, etc.
We may modify that query so if resulting table has one column less, headers will be Column2, Column3, etc. In combined table columns will be arranged by their column names, thus we will have correct order of columns. Modified Transform Sample File query could be
let
NumberOfColumns = 4,
NewNames = List.Zip( {
List.Transform( {1..NumberOfColumns-1}, (q) => "Column" & Text.From(q) ),
List.Transform( {2..NumberOfColumns}, (q) => "Column" & Text.From(q) )
} ),
Source = Csv.Document(Parameter1,[Delimiter=",", Encoding=65001, QuoteStyle=QuoteStyle.None]),
n = Table.ColumnCount(Source),
AdjustColumns =
if n < NumberOfColumns
then Table.RenameColumns( Source, NewNames )
else Source
in
AdjustColumns
Don't forget to remove Columns=4 option in Csv.Document(), that's the key. And use your actual number of columns instead of 4.
Most probably similar could be done if you have another setup.
SergeiBaklan,
Thank you for your response. I should have stated what file types I was importing. They are .xlsx files.
I will try the same query modifying it from csv to xlsx and see if that works.
- SergeiBaklanMar 24, 2025Diamond Contributor
With Excel is practically the same, the only you have to define sheet name to combine
let SheetToSelect = "Test", NumberOfColumns = 4, NewNames = List.Zip( { List.Transform( {1..NumberOfColumns-1}, (q) => "Column" & Text.From(q) ), List.Transform( {2..NumberOfColumns}, (q) => "Column" & Text.From(q) ) } ), Source = Excel.Workbook(Parameter1, null, true){[Item=SheetToSelect,Kind="Sheet"]}[Data], n = Table.ColumnCount(Source), AdjustColumns = if n < NumberOfColumns then Table.RenameColumns( Source, NewNames ) else Source in AdjustColumns