0
votes

Hey I need help with my code, I am trying to figure out how to deal with different files having different number of pages.

I am actually using power bi to extract data from a folder of pdf's but the deal is that those files have different number of pages, in the code below you can see that I have 3 tables, each of them is on a page(Table1 for the table of the first page..), so for pdf files having only one page the tables 2 and 3 do not exist, so I am looking for a way to ignore parts of code where there will be errors

let

 Source = Pdf.Tables(#"Paramètre de l'exemple de fichier1"),
 Table1 = Source{[Id="Table1"]}[Data],
 #"En-têtes promus1" = Table.PromoteHeaders(Table1, [PromoteAllScalars=true]),
 Table2=Source{[Id="Table2"]}[Data],
 #"En-têtes promus2" = Table.PromoteHeaders(Table2, [PromoteAllScalars=true]),
 Table3=Source{[Id="Table3"]}[Data],
 #"En-têtes promus3" = Table.PromoteHeaders(Table3, [PromoteAllScalars=true]),
 #"Table" = Table.Combine({#"En-têtes promus1", #"En-têtes promus2",#"En-têtes promus3"}),
 #"Index ajouté" = Table.AddIndexColumn(Table, "Index", 1, 1),
 #"Colonnes renommées" = Table.RenameColumns(#"Index ajouté",{{"Index", "Page"}})
in
 #"Colonnes renommées"
2

2 Answers

1
votes
 let
 Source = Pdf.Tables(#"Paramètre de l'exemple de fichier1"),
Table005 = Source{[Id="Table005"]}[Data],
#"En-têtes promus1" = Table.PromoteHeaders(Table005, [PromoteAllScalars=true]),

#"Table010"=try Source{[Id="Table010"]}[Data] otherwise null,
#"En-têtes promus2" = Table.PromoteHeaders(Table010, [PromoteAllScalars=true]),
Table015=try Source{[Id="Table015"]}[Data] otherwise null ,
#"En-têtes promus3" = Table.PromoteHeaders(Table015, [PromoteAllScalars=true]),
#"Table" =  try Table.Combine({#"En-têtes promus1", #"En-têtes promus2"}) otherwise  #"En-têtes promus1",
#"Table2" =  try Table.Combine({#"Table",#"En-têtes promus3"}) otherwise  #"Table",
#"Index ajouté" = Table.AddIndexColumn(Table2, "Index", 1, 1),
#"Colonnes renommées" = Table.RenameColumns(#"Index ajouté",{{"Index", "Page"}})


in
 #"Colonnes renommées"
0
votes

Yes. You can use try and otherwise for steps where errors may occur.

More details: https://docs.microsoft.com/en-us/power-query/handlingerrors