In this Power BI desktop project, I will explore data from Statistics Sweden’s (Sw: Statistikmyndigheten SCB) Statistical database via their API. The database contains official statistics from Statistics Sweden and 15 other government agencies. The statistics cover many subject areas, and often over a long period of time.
I have added step-by-step instructions on how to establish a “live connection” between Power BI and the Statistic Sweden API. At the bottom of the page I have embedded the final Power BI dashboard.
Step-by-step instructions
Step 1 – find an appropriate table in Statistics Sweden’s Statistical database
In the statistical database it is easy to browse through the available subject areas and tables. Yeo reach the statistical database on this link: https://www.statistikdatabasen.scb.se/pxweb/en/ssd/
When you have found the subject area you are interested in, you can drill-down to a specific table that contain the information you want.
Step 2 – Find the table’s API URL and JSON query
- Click on the link to the table as shown above.
- Choose which variables you want to include in the table (see image below).
- Click on “Continue”
- Click on “API for this table”
- We will need the URL and JSON query information as shown below. This will be specific for each table and each selection of variables.
Step 3 – Connect Power BI to Statistics Sweden’s API
- Copy the JSON query as shown in the example above and paste it in an editor such as “Notepad++”.
- Replace all “ with “” as shown in the example below.
- Open Power BI
- On the “Get Data” window, select “Blank Query” and click on “Connect“.
- In the “Power Query Editor“, click on “Advanced Editor“.
- In the “Power Query Editor“, click on “Advanced Editor“.
- Delete any default text in the “Advanced Editor” and paste the example code shown below.
let PostContents= " { ""query"": [ { ""code"": ""Anvandningstyp"", ""selection"": { ""filter"": ""item"", ""values"": [ ""BNPM"" ] } }, { ""code"": ""ContentsCode"", ""selection"": { ""filter"": ""item"", ""values"": [ ""NR0103BV"" ] } } ], ""response"": { ""format"": ""CSV"" } } ", Source = Web.Contents("http://api.scb.se/OV0104/v1/doris/en/ssd/START/NR/NR0103/NR0103A/NR0103ENS2010T01Kv", [Content=Text.ToBinary(PostContents)]), #"Imported CSV" = Csv.Document(Source,null,",",null,1252) in #"Imported CSV"
- The sections marked with a different color in the above code example need to be replaced with your JSON query and URL
- Note that you need to change the part “”format””: “”px”” to “”format””: “”CSV”” in order to get the data in a format which easy can be read by Power BI.
- When you have made the above changes, click on “Done“.
- Now the data will be loaded into Power BI. Before we can start visualizing the data, we need to do some cleaning and formatting changes. E.g. promoting the headers, transpose rows and columns, split some text, replace some text/values and hide and rename a few columns.
- In building the final dashboard shown below, I have gone through this process five times (i.e. one for each table in the statistical database I would like to receive data from)
The final dashboard
Related Posts
January 11, 2021
World population estimates and projections from 1960 to 2050
In this Power BI desktop project, I…
January 7, 2021
World Economic Outlook, October 2020: A Long and Difficult Ascent
In this Power BI desktop project, I…