Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Anonymous
Not applicable

Power Query M - Dinamic Number of Columns

Hi!

 

I have a folder with lots of Excel files with data for each month, the format of those files is almost always the same:

 

Headers start in line 9 --> this happens in every file.

Two sheets with 12 or 13 columns --> in most files it is 13 but sometimes it has only 12.

Name of columns and data is always structured correctly and in the same way.

Column 12 has garbage data and when the file has Column 13 with data is the same case, later I´ll delete both columns.

 

The issue is that when I load these Excel files importing from a Folder if every file doesn´t have 13 columns then it won´t allow to load the data to the model.

 

I´ve tried implementing some M code checking if Table.ColumnCount is less than 13 then add a dummy column Table.AddColumn with null values, but it doesn´t seem to work.

 

let
    Source = Excel.Workbook(#"Sample File Parameter2", null, true),
    CDF_Sheet = Source{[Item="ABC",Kind="Sheet"]}[Data],
    number_columns = Table.ColumnCount(ABC_Sheet),
    #"Removed Top Rows" = Table.Skip(ABC_Sheet,9),
    #"Fix missing column" = if number_columns < 13 then Table.AddColumn(#"Removed Top Rows", "Column13", null) else #"Removed Top Rows",
    #"Renamed Columns" = Table.RenameColumns(#"Removed Top Rows",{{"Column1", "Department"}, {"Column2", "Subject"}, {"Column3", "Shift"}, {"Column4", "Student_LastName"}, {"Column5", "Student_Name"}, {"Column6", "CUF"}, {"Column7", "Note1"}, {"Column8", "Note2"}, {"Column9", "Note3"}, {"Column10", "Final_Note"}, {"Column11", "Presentism"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Column12", "Column13"})
in
     #"Removed Columns"

Thanks in advance!

1 ACCEPTED SOLUTION
ElenaN
Resolver V
Resolver V

Hello,

 

I have done very few changes to your code to make it work (highlighted with blue):

 

let
    Source = Excel.Workbook(#"Sample File Parameter2", null, true),
    ABC_Sheet = Source{[Item="ABC",Kind="Sheet"]}[Data],
    number_columns = Table.ColumnCount(ABC_Sheet),
    #"Removed Top Rows" = Table.Skip(ABC_Sheet,9),
    #"Fix missing column" = if number_columns < 13 then Table.AddColumn(#"Removed Top Rows", "Column13", each null) else #"Removed Top Rows",
    #"Renamed Columns" = Table.RenameColumns( #"Fix missing column",{{"Column1", "Department"}, {"Column2", "Subject"}, {"Column3", "Shift"}, {"Column4", "Student_LastName"}, {"Column5", "Student_Name"}, {"Column6", "CUF"}, {"Column7", "Note1"}, {"Column8", "Note2"}, {"Column9", "Note3"}, {"Column10", "Final_Note"}, {"Column11", "Presentism"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Column12", "Column13"})
in
     #"Removed Columns"

Regards,

ElenaN

 

View solution in original post

1 REPLY 1
ElenaN
Resolver V
Resolver V

Hello,

 

I have done very few changes to your code to make it work (highlighted with blue):

 

let
    Source = Excel.Workbook(#"Sample File Parameter2", null, true),
    ABC_Sheet = Source{[Item="ABC",Kind="Sheet"]}[Data],
    number_columns = Table.ColumnCount(ABC_Sheet),
    #"Removed Top Rows" = Table.Skip(ABC_Sheet,9),
    #"Fix missing column" = if number_columns < 13 then Table.AddColumn(#"Removed Top Rows", "Column13", each null) else #"Removed Top Rows",
    #"Renamed Columns" = Table.RenameColumns( #"Fix missing column",{{"Column1", "Department"}, {"Column2", "Subject"}, {"Column3", "Shift"}, {"Column4", "Student_LastName"}, {"Column5", "Student_Name"}, {"Column6", "CUF"}, {"Column7", "Note1"}, {"Column8", "Note2"}, {"Column9", "Note3"}, {"Column10", "Final_Note"}, {"Column11", "Presentism"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Column12", "Column13"})
in
     #"Removed Columns"

Regards,

ElenaN

 

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors