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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Query Editor - Try query, if error, skip/move on?

So I'm trying to bring in PDF pages into my data model. In order to keep the data ingestion dynamic, I need to setup a try/otherwise statement as some PDF's might have 5 pages, but others only 3. I would have queries setup for each page, but the queries for pages 4 and 5 would fail for a 3 page PDF causing a refresh to fail. 

 

How can I setup m code to try the query, and if it fails, do nothing or just keep going so the entire refresh doesn't fail?

5 REPLIES 5
v-yingjl
Community Support
Community Support

Hi @Anonymous ,

You can try to use List.RemoveNulls() function and add try otherwise statement in it, here is the sample you can refer as your need:

let
    Source =
    Table.Combine(
        List.RemoveNulls({
            Table.FromRecords({[CustomerID = 1, Name = "Bob"]}),  //Query 1
            Table.FromRecords({[CustomerID = 2, Name = "Jim"]}),  //Query 2
            try
            Table.FindText()  //Query 3 which represents error
            otherwise
            null,
            Table.FromRecords({[CustomerID = 3, Name = "AAA"]}),  // Query 4
            try
            Table.FirstValue()  //Query 5 which represents error
            otherwise
            null,
            Table.FromRecords({[CustomerID = 4, Name = "BBB"]})   // Query 6
        })
    )
in
    Source

 

Best Regards,
Yingjie Li

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

@parry2k 

 

I'm just using a basic import of a page in PDF from SharePoint. No data transformation really at all.

 

Original Query: 

let
    Source = SharePoint.Files("https://xxxxx.sharepoint.com/personal/xxxxx_com/", [ApiVersion = 15]),
    #"Filtered Rows2" = Table.SelectRows(Source, each Text.StartsWith([Name], "DailyStatement")),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows2",{{"Date created", Order.Descending}}),
    #"Kept First Rows" = Table.FirstN(#"Sorted Rows",1),
    #"Filtered Hidden Files1" = Table.SelectRows(#"Kept First Rows", each [Attributes]?[Hidden]? <> true),
    #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File (4)", each #"Transform File (4)"([Content])),
    #"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File (4)"}),
    #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File (4)", Table.ColumnNames(#"Transform File (4)"(#"Sample File (4)")))
in
    #"Expanded Table Column1"

 

Going this route with SharePoint creates the 4 helper queries:

SampleFile

let
    Source = SharePoint.Files("https://xxxxxxmy.sharepoint.com/personal/xxxxxxx_com/", [ApiVersion = 15]),
    #"Filtered Rows2" = Table.SelectRows(Source, each Text.StartsWith([Name], "DailyStatement")),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows2",{{"Date created", Order.Descending}}),
    #"Kept First Rows" = Table.FirstN(#"Sorted Rows",1),
    Navigation1 = #"Kept First Rows"{0}[Content]
in
    Navigation1

Parameter

#"Sample File (4)" meta [IsParameterQuery=true, BinaryIdentifier=#"Sample File (4)", Type="Binary", IsParameterQueryRequired=true]

Transform File

 

let
    Source = (Parameter4) => let
        Source = Pdf.Tables(Parameter4, [Implementation="1.1"]),
        Page1 = Source{[Id="Page002"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(Page1, [PromoteAllScalars=true])
    in
        #"Promoted Headers"
in
    Source

 

Transform Sample File

let
    Source = Pdf.Tables(Parameter4, [Implementation="1.1"]),
    Page1 = Source{[Id="Page002"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Page1, [PromoteAllScalars=true])
in
    #"Promoted Headers"
Anonymous
Not applicable

@v-yingjl Thanks for the reply. Based on my post above, I think the queries would fail there instead of when I combine the queries (pages) together?

Anonymous
Not applicable

Any ideas on how to implement this?

parry2k
Super User
Super User

@Anonymous can you share M code you are using and I will put the logic to catch the error.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.