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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.