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
Guidera
Regular Visitor

2 data sources - How to Catch ERROR dynamically and and execute alternative query?

 

Hi guys,

 

I am new to M Code and I was wondering if anyone could give me a help. 

 

I wrote the code below to connect to a data source and look for the column "ForexExchange". If the column is not found the ERROR should be caught and it should connect to a second data source to extract the information from there.

 

 

let
    get_bidAsk = (file as text) =>
let
    Source = Web.Page(Web.Contents(file)),
             Data3 = Source{3}[Data],
	     #"Changed Type" = Table.TransformColumnTypes(Data3,{{"", type text}, {"ForexExchange", type text}, {"Last", type number}, {"Bid", type number}, {"Ask", type number}, {"Volume", type number}, {"Change %", Percentage.Type}, {"Currency", type text}, {"Time", type time}, {"2", type text}}),
										
    TestForError = try Source,
	   
    Output = if TestForError[HasError] then 
                 get_soleBidASK(file)
             else 
                 #"Changed Type" = Table.TransformColumnTypes(Data3,{{"", type text}, {"ForexExchange", type text}, {"Last", type number}, {"Bid", type number}, {"Ask", type number}, {"Volume", type number}, {"Change %", Percentage.Type}, {"Currency", type text}, {"Time", type time}, {"2", type text}}),
                 #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{""}),
		 #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"ForexExchange", "Last", "Bid", "Currency", "Ask", "Volume", "Change %", "Time", "2"}),
		 #"Removed Columns1" = Table.RemoveColumns(#"Reordered Columns",{"2"})					
in
    Output
in
    get_bidAsk

 

This is print ERROR I get from the function output. 

 

It connects to the first data source and doesnt find the column "ForexExchange". I would need it to identify the ERROR using the try<> and then connect to the second datasource to extract the records there.

 

 

error.PNG

 

Thanks

1 REPLY 1
v-huizhn-msft
Employee
Employee

Hi @Guidera,

I am not specific about Power Query, I personally suggest you post your thread to Power Query forum to get dedicated support. And you can share the solution if you get workaround from the Power Query forum, more people will benefit from here.

Best Regards,
Angelia

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.