I’ve come from a background of Qlikview development and I’m now learning Power BI. With all the information around Covid-19 being released I wanted to do some analysis myself. The Office of National Statistics (ONS) produce lots of extracts and it’s perfectly possible to download excel files off their website. However, that’s not as much fun or a challenge as automating the process. This blog post walks you through the process of building the Power BI ONS custom data connector.
With Power BI it’s possible to build custom data connectors with the SDK.
The Office of National Statistics have a Beta API (https://developer.beta.ons.gov.uk/) which is open and unrestricted (no oAuth2 etc). As it’s in Beta it should not be relied upon and as their site says may ‘occasionally be breaking’.
Before you get too excited, I should warn you the data available here appears to be quite limited, only data from the current year is present. However, you can find previous years’ data in excel files on their site.
If you just want to use it and are not interested in the detail, follow these steps:
- Download ONS.zip from here https://www.blueoceanweb.co.uk/wp-content/uploads/2020/06/ONS.zip
- Extract the contents (ONS.mez) to C:Users\DocumentsPower BI DesktopCustom Connectors
- Open Power BI Desktop and search for ‘ONS’ and select connect on ‘ONS Data Source Path (Beta)
- Click continue to ignore the warning message
- Select the data you’d like
- This will return all deaths registered by the ONS, split by age group, sex, and week up until the latest available week. (This is all from the weekly-deaths-age-sex dataset).
- If you’d like to automate the load you’ll need to install the on-premises gateway, which is out of the scope of this blog, but there are lots of articles on it. The gateway is available here: https://powerbi.microsoft.com/en-us/gateway/
For more of the detail read on
The data in the API is provided in JSON format and requesting using HTTP GET requests.I was unfamiliar with JSON & GET requests and found the best way I found to quickly send GET requests was to use postman (www.postman.com).
An example if you want to see get all the available datasets the ONS has on this site send the following GET request: https://api.beta.ons.gov.uk/v1/datasets. To do that in Postman just copy and paste the above text into the app and press send.
The response you receive back is the data in JSON format. It’s a little tricky to read so I found it easier to copy and paste it into a text file and save it with the .json file extension. You can then open it in Power BI. Below I’ve selected the two datasets (weekly-deaths-age-sex and weekly-deaths-local-authority).
Each dataset has several dimensions and allowed options, think of them as filters and filter values (i.e. time = 2020 and sex = male).
For each request all dimensions must be used with valid options. There is a wildcard option however this is only allowed to be used on one dimension per request.
This means you cannot send one request to get all the data from a dataset. However, you can send many requests to get around this and we’ll get to that in a bit.
My connector uses the weekly-deaths-age-sex dataset.
To get a list of all required dimensions run this command in postman:
For each dimension run this command to get a list of valid options:
I’m using the following options:
- 2020 (that’s the only valid option at present)
- week (week-1, week-2, week-3 etc)
- All weeks up to the present week.
- Here I use the wild card * (to get all values)
- I just use total-registered-deaths
- All values (i.e. 1-4, 10-14, 15-19…)
- male and female
If we want to return all the data up to week 20
To return all the data for a dataset you must run a request each permutation of the different options.
If today is week 20 (week ending May 17th), you must run 800 requests get the complete dataset.
800 = week (20 options) * agegroups (20 options) * sex (2 options) * all other dimensions (1 option).
(This is pretty inefficient but I can’t see any other way to get all the data).
How to get this data connector working:
- PowerBI Desktop
- Visual Studio 2019
- Power Query SDK
- My code from GitHub https://github.com/mrsheraton/ONSPowerBIConnector
Click clone or download and then Download ZIP
Extract the content then double click the solution file (ONS.sln) to load visual studio.
Open ONS.pq and note the ONS.Contents() function. This is what will be called by PowerBI
When you run the program you’ll get this screen, set the Credential Type to anonymous and click ‘Set Credential’ and close the message box. Now rerun the program and it should display the query result where name is weekly-deaths-age-sex
At this point the connector has been built, you’ll get a complied .mez file in your debug folder.
ONSbinDebugONS.mez you can copy the steps above from 2 to get this working in Power BI Destop.
You’ll be able to create some visultions. Here is one I built (this also uses histroic data available in spreadsheets). It updates weekly on my website: https://www.blueoceanweb.co.uk/portfolio/
If you found the Power BI Custom connector post useful please leave me a comment! 🙂