cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
danielodievich Frequent Visitor
Frequent Visitor

How to check for CSV file existence before trying to load it with File.Contents?

Hello everyone,

 

 

I build a PowerBI file that targets certain set of CSV files. 

 

I have defined a text parameter called ReportDataFolderPath that defines the folder in which the CSV files exist.

 

I load the data into Csv.Document data source using File.Contents like that:

 

 

let
    Source = Csv.Document(File.Contents(ReportDataFolderPath & "\subfolder\filewithdata.csv"),[Delimiter=",", Columns=18, Encoding=65001, QuoteStyle=QuoteStyle.Csv]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
... the rest is snipped in #"Changed Type3"

 

 

This works very well when "filewithdata.csv" exists.

 

 

However, sometimes the input set of CSV files does not have "filewithdata.csv". This causes template-to-report loading to fail.

 

I would like to write some sort of check that validates that "filewithdata.csv" is present in the file system before attempting to load it into Csv.Document function. Can someone suggest a way to do it?

 

Thank you

Daniel

 

1 ACCEPTED SOLUTION

Accepted Solutions
abbasabdulla Frequent Visitor
Frequent Visitor

Re: How to check for CSV file existence before trying to load it with File.Contents?

Hi,

Can you give this code a try!

 

let
    Source = Folder.Files("ReportDataFolderPath & "\subfolder\"),
    #"Filtered Rows" = Table.SelectRows(Source, each ([Name] = "filewithdata.csv")),
    #"Counted Rows" = if Table.RowCount(#"Filtered Rows")= 1 then
let
SourceFile = Csv.Document(File.Contents("ReportDataFolderPath & "\subfolder\filewithdata.csv"),[Delimiter=",", Columns=18, Encoding=65001, QuoteStyle=QuoteStyle.Csv]),
    #"Promoted Headers" = Table.PromoteHeaders(SourceFile, [PromoteAllScalars=true])
in
#"Promoted Headers" 
else 
"File is not found"
in
    #"Counted Rows"

OR check this post it has more sophisticated method: https://community.powerbi.com/t5/Integrations-with-Files-and/How-to-check-if-a-file-exists/m-p/66259...

View solution in original post

1 REPLY 1
abbasabdulla Frequent Visitor
Frequent Visitor

Re: How to check for CSV file existence before trying to load it with File.Contents?

Hi,

Can you give this code a try!

 

let
    Source = Folder.Files("ReportDataFolderPath & "\subfolder\"),
    #"Filtered Rows" = Table.SelectRows(Source, each ([Name] = "filewithdata.csv")),
    #"Counted Rows" = if Table.RowCount(#"Filtered Rows")= 1 then
let
SourceFile = Csv.Document(File.Contents("ReportDataFolderPath & "\subfolder\filewithdata.csv"),[Delimiter=",", Columns=18, Encoding=65001, QuoteStyle=QuoteStyle.Csv]),
    #"Promoted Headers" = Table.PromoteHeaders(SourceFile, [PromoteAllScalars=true])
in
#"Promoted Headers" 
else 
"File is not found"
in
    #"Counted Rows"

OR check this post it has more sophisticated method: https://community.powerbi.com/t5/Integrations-with-Files-and/How-to-check-if-a-file-exists/m-p/66259...

View solution in original post

Helpful resources

Announcements
Ask Amanda Anything Q&A

Ask Amanda Anything Q&A

Learn the answers to some of the questions asked during the Amanda Triple A event.

October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 71 members 1,094 guests
Please welcome our newest community members: