Preserving Power Query Table structure in case Source data not available

naira

New Member
Joined
May 7, 2013
Messages
16
Office Version
  1. 2013
Platform
  1. Windows
hi!

I am using below code to pull data from an online website into my excel sheet using Excel Power query. The power query is automatically refreshed every few minutes using an Excel Macro.

The resulting table is a table of 2 rows, (one header row and another data row). The table structure is maintained in case the internet connection is working and the data is coming properly.

In case the internet connection is down and the data source is not available, I have 2 issues:

1. The Excel gives an error message and stops any other work, until I manually acknowledge the excel error messages.
- I managed to address this issue by wrapping the code in try let and otherwise 0;

2. If I used the code in serial no.1 above, the excel continues to operate and refreshes other data from other data sources, but the table structure for the specific website is converted into a single cell with value 0. This results in the #Ref! error for other cells, which were looking at the data in the above columns and rows. Also, the subsequent columns of that table are automatically deleted, even though I have selected the option of "Overwrite existing cells with new data, clear unused cells, in case of data refresh, in Table Properties.

Normal Table:

Capture1.JPG



Null Table in case Data Source not available:

Capture2.JPG


Request:
I need to modify the below code to ensure that even in case the data source is not available during the Query/ Table refresh, the table structure should not get broken. In case there is no source data during a refresh, the cells could be filled with 0. I have tried the powerquery error handling options such as MissingField.UseNull or MissingField.Ignore, but I have been unable to get them to work in my case.

Power Query:
try let
    Source = Web.Page(Web.Contents("https://www.investing.com/commodities/real-time-futures")),
    Data0 = Source{0}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Data0,{{"", type text}, {"Commodity", type text}, {"Month", type date}, {"Last", type number}, {"High", type number}, {"Low", type number}, {"Chg.", type number}, {"Chg. %", Percentage.Type}, {"Time", type date}, {"2", type text}}),
    #"Removed Top Rows" = Table.Skip(#"Changed Type",7),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Removed Top Rows",26),
    #"Removed Columns" = Table.RemoveColumns(#"Removed Bottom Rows",{"", "Time", "2"})
in
    #"Removed Columns"
otherwise 0
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,223,941
Messages
6,175,537
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top