-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandleDataSourceError
More file actions
24 lines (21 loc) · 1.41 KB
/
HandleDataSourceError
File metadata and controls
24 lines (21 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
For source errors, Power Query sometimes refuses to return the error, instead preferring to leave the previous version
of the data unrefreshed which makes macros go crazy because it cannot detect when the sheet is empty due to a bad query pull.
The code below adds a query test to see if the source returns a value or not and if it does, runs an error handler basically, that
allows you to manage the error the way you see fit.
Code copied shamelessly from Chris Webb's BI blog at:
https://blog.crossjoin.co.uk/2014/09/18/handling-data-source-errors-in-power-query/
let
//This is the original code generated by Power Query
Source = Csv.Document(File.Contents("C:\Users\Chris\Documents\SampleData.csv"),null,",",null,1252),
#"First Row as Header" = Table.PromoteHeaders(Source),
#"Changed Type" = Table.TransformColumnTypes(#"First Row as Header",{{"Month", type text}, {"Product", type text}, {"Sales", Int64.Type}}),
//End of original code
//Define the alternative table to return in case of error
AlternativeOutput=#table(type table [Month=text,Product=text,Sales=Int64.Type],{{"Error", "Error", 0}}),
//Does the Source step return an error?
TestForError= try Source,
//If Source returns an error then return the alternative table output
//else return the value of the #"Changed Type" step
Output = if TestForError[HasError] then AlternativeOutput else #"Changed Type"
in
Output