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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Applicable88
Impactful Individual
Impactful Individual

Load multiple csv files, but different row counts until column heads

Hello,

I have multiple csv files that I want to load at once and combined with each other. These tables aren't typically formatted. I need to extract the date from the first second row as a additional column (to distinguish which csv I have) and afterwards delete empty rows and then put the columns into the head. There are sevaral empty rows until I reach the column line:

  Date Timestamps        
  01/01/2022         
            
            
Column A Column B Column C Column D Column E Column F 
xxx xxx xxx xxx xxx xxx 
            
            
            

 

The problem I encounter now is that I have multiple csv files, where the column header sometimes has 4 rows that i need to delete to reach the top and sometimes more and less. Is there some command like delete only empty rows or until row =Column A? 

 

Thank you very much in advance. 

Best. 

1 ACCEPTED SOLUTION
KNP
Super User
Super User

Hi @Applicable88,

 

So, for your second issue, here's a function I use regularly to combine multiple CSV files where the number of unwanted rows varies.

 

// fProcessFiles
let
  fProcessFiles = (myFile as binary) =>
    let
      CSV = Csv.Document(myFile, [Encoding=1252]),
      ConditionalBlankIndex = Table.AddColumn(CSV, "Custom", each if [Column2] = "" then 0 else 1),
      HeaderPosition = List.PositionOf(ConditionalBlankIndex[Custom], 1),
      RemoveRows = Table.Skip(CSV, HeaderPosition),
      PromotedHeaders = Table.PromoteHeaders(RemoveRows, [PromoteAllScalars = true])
    in
      PromotedHeaders
in
  fProcessFiles

 

 

Change the column reference '[Column2]' to a column that will be all blank until you hit a header.

 

Here's the other function I used in the main code.

// fNoHeader
let
  fProcessFiles = (myFile as binary) =>
    let
      CSV = Csv.Document(myFile, [Encoding=1252])
    in
      CSV
in
  fProcessFiles

 

Main code:

let
  Source = Folder.Files("H:\My Drive\Power BI\Community Solutions\Files"),
  #"Filtered Rows" = Table.SelectRows(Source, each ([Name] = "headers_filereference.csv")),
  #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows", {"Content"}),
  #"Invoked Custom Function" = Table.AddColumn(
    #"Removed Other Columns",
    "Query1",
    each fProcessFiles([Content])
  ),
  #"Invoked Custom Function1" = Table.AddColumn(
    #"Invoked Custom Function",
    "NoHeader",
    each fNoHeader([Content])
  ),
  #"Added Custom" = Table.AddColumn(
    #"Invoked Custom Function1",
    "Custom",
    each [NoHeader]{1}[Column3]
  ),
  #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom", {"Query1", "Custom"}),
  #"Expanded Query1" = Table.ExpandTableColumn(
    #"Removed Other Columns1",
    "Query1",
    {
      "Column A",
      "",
      "Column B",
      "_1",
      "Column C",
      "_2",
      "Column D",
      "_3",
      "Column E",
      "_4",
      "Column F",
      "_5"
    },
    {
      "Column A",
      "Column1",
      "Column B",
      "_1",
      "Column C",
      "_2",
      "Column D",
      "_3",
      "Column E",
      "_4",
      "Column F",
      "_5"
    }
  )
in
  #"Expanded Query1"

 

Basically...

  • Connect to the folder with your files
  • Remove all columns except your content
  • Invoke both functions using the Add Column >> Invoke Custom Function
  • Add a custom column referencing the detail you want to keep from each file
  • Remove unwanted columns again
  • Expand the column that has the table with filtered headers

Let me know if this requires further explanation.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
xOIEmaj

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
image
fabric-SUbadge
Proud to be a Super User!

View solution in original post

3 REPLIES 3
KNP
Super User
Super User

Hi @Applicable88,

 

So, for your second issue, here's a function I use regularly to combine multiple CSV files where the number of unwanted rows varies.

 

// fProcessFiles
let
  fProcessFiles = (myFile as binary) =>
    let
      CSV = Csv.Document(myFile, [Encoding=1252]),
      ConditionalBlankIndex = Table.AddColumn(CSV, "Custom", each if [Column2] = "" then 0 else 1),
      HeaderPosition = List.PositionOf(ConditionalBlankIndex[Custom], 1),
      RemoveRows = Table.Skip(CSV, HeaderPosition),
      PromotedHeaders = Table.PromoteHeaders(RemoveRows, [PromoteAllScalars = true])
    in
      PromotedHeaders
in
  fProcessFiles

 

 

Change the column reference '[Column2]' to a column that will be all blank until you hit a header.

 

Here's the other function I used in the main code.

// fNoHeader
let
  fProcessFiles = (myFile as binary) =>
    let
      CSV = Csv.Document(myFile, [Encoding=1252])
    in
      CSV
in
  fProcessFiles

 

Main code:

let
  Source = Folder.Files("H:\My Drive\Power BI\Community Solutions\Files"),
  #"Filtered Rows" = Table.SelectRows(Source, each ([Name] = "headers_filereference.csv")),
  #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows", {"Content"}),
  #"Invoked Custom Function" = Table.AddColumn(
    #"Removed Other Columns",
    "Query1",
    each fProcessFiles([Content])
  ),
  #"Invoked Custom Function1" = Table.AddColumn(
    #"Invoked Custom Function",
    "NoHeader",
    each fNoHeader([Content])
  ),
  #"Added Custom" = Table.AddColumn(
    #"Invoked Custom Function1",
    "Custom",
    each [NoHeader]{1}[Column3]
  ),
  #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom", {"Query1", "Custom"}),
  #"Expanded Query1" = Table.ExpandTableColumn(
    #"Removed Other Columns1",
    "Query1",
    {
      "Column A",
      "",
      "Column B",
      "_1",
      "Column C",
      "_2",
      "Column D",
      "_3",
      "Column E",
      "_4",
      "Column F",
      "_5"
    },
    {
      "Column A",
      "Column1",
      "Column B",
      "_1",
      "Column C",
      "_2",
      "Column D",
      "_3",
      "Column E",
      "_4",
      "Column F",
      "_5"
    }
  )
in
  #"Expanded Query1"

 

Basically...

  • Connect to the folder with your files
  • Remove all columns except your content
  • Invoke both functions using the Add Column >> Invoke Custom Function
  • Add a custom column referencing the detail you want to keep from each file
  • Remove unwanted columns again
  • Expand the column that has the table with filtered headers

Let me know if this requires further explanation.

 

Have I solved your problem?
Please click Accept as Solution so I don't keep coming back to this post, oh yeah, others may find it useful also ;).
xOIEmaj

If you found this post helpful, please give Kudos.
It gives me a sense of instant gratification and, if you give me Kudos enough times, magical unicorns will appear on your screen.
If you find my signature vaguely amusing, please give Kudos.
image
fabric-SUbadge
Proud to be a Super User!
Applicable88
Impactful Individual
Impactful Individual

@KNP very appreciate the thorough answer. Thanks!

AlexisOlson
Super User
Super User

The simplest approach would be to filter blanks from one of the columns but this requires that column to not have blanks in any rows you want to preserve.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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 Kudoed Authors