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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Netrelemo
Helper IV
Helper IV

How do I trap a function error?

I'm using PowerQuery to try get the contents of a list on 1,000 plus SharePoint sites. 

 

So I have retrieved the list of site URLs and constructed the list URL, and now want to get  the contents of each list, merged into one long aggregated list. 

 

So I have written a function, and I'm using that mostly just fine. The problem is that sometimes a particular site doesn't have the list I am looking for. It doesn't exist. And I cannot find a way to trap the error. 

Expression.Error: We cannot convert a value of type Table to type Function

 

My function returns a "table", andsometimes "error". If I try "replace errors" it can't because it's a table, not just a text or number. 

 

 

 

 

 

 

let
    Source = (SiteURL as any) => 
    let
        aSource = OData.Feed(SiteURL , null, [Implementation="2.0"]),
        "Removed Other Columns" = Table.SelectColumns(aSource,{ "CostTypeFree1"},MissingField.UseNull)
        "Distincted" = Table.Group(#"aSource", {"CostTypeFree1"}, {{"Count", each Table.RowCount(_), type number}}),
        "Removed Columns" = Table.RemoveColumns(Distincted,{"Count"}),
    in
        #"Removed Other Columns"
in
    Source

 

 

 

 

 

 

So... my question: How do I handle the fact that sometimes a list doesn't exist, and then return something useful.

I'm thinking maybe some sort of 
    Try this web request. If I get a 404 then "No" else "ok"

 

It would be so nice if I could just use something like this:

 

 

let
    response = Web.Contents(
        "https://api.census.gov/data/2018/pep/population?get=POP&for=state"
    ),
    json = Json.Document(response)
in
    json

 

 

 ... but unfortunately in a SSO MFA environment, such simple efficient solutions don't seem possible. 

 

 

Here's another attempt:

 

let
    Source = (theurl as any) => 
    let
        aTest = try OData.Feed(theurl, null, [Implementation="2.0"]),
        test = Table.AddColumn(aTest, "ListCheck", each if aTest [HasError] then "List doesnt exist" else "ok")
    in
        test
in
    Source


// An error occurred in the query. Expression.Error: We cannot convert a value of type Record to type Table.
// Details:
//     Value=[Record]
//     Type=[Type]

 

 

 

 

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

You need an otherwise to go with your try

 

aSource = try OData.Feed(SiteURL , null, [Implementation="2.0"]) otherwise null

 

If it is the Removed Other Columns step that generates the error, you could add a step to add a column with Table.RowCount( ) after aSource, and then filter out any rows where the table does not have >=1 rows.

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

1 REPLY 1
mahoneypat
Microsoft Employee
Microsoft Employee

You need an otherwise to go with your try

 

aSource = try OData.Feed(SiteURL , null, [Implementation="2.0"]) otherwise null

 

If it is the Removed Other Columns step that generates the error, you could add a step to add a column with Table.RowCount( ) after aSource, and then filter out any rows where the table does not have >=1 rows.

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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