A Quick Guide to GeoJSONs in Power BI | by Lance McDiffett | Towards Data Science

Many cities have a wealth of safety data — crime, moving violations, crashes, etc.— but having data in and of itself does not always equate to having information. If it did, the world wouldn’t need analysts!
After repeatedly receiving requests to perform traffic safety analytics for our clients, I undertook the task of developing an interactive tool that would enable cities to explore their own safety data in a way that would easily allow them to extract relevant safety-related information and insights.
A key component to this tool was the ability to provide the cities with crash data that was as up-to-date as possible, without having to manually update that crash data each time it was refreshed. I chose to do this within Power BI and used Washington, DC as a prototype for the tool.
First off, I needed to find my data. From past analyses I knew that Washington, DC has a pretty impressive open data setup so I stumbled over there and began searching. Turns out, they had exactly what I needed — a data set containing all crashes and their locations maintained by the District DOT. Jackpot.
Next up, obtaining the data. The Open Data DC site allows you to download CSVs, KMLs and shapefiles, but these are only a snapshot of a point in time. The purpose of creating this tool is, in part, to build a product that replaces the need for me to perform the same analysis every month or so as data changes. Luckily, Open Data DC has an API.
If you’re new to this whole world of API’s like I am, this article by Petr Gazarov is very digestible and informative.
Accessing their API is simple enough, but figuring out how to utilize that data within Power BI was a bit of learning experience for me. I spent hours exploring the different API’s they provided, the data that they returned, and doing some research online. During my searching, I wasn’t able to find anything that really took me through the process from finding the data to loading it into the Power BI data model (especially for GeoJSON files), so I thought I would write up a quick article that walks through it step-by-step.
The Open Data DC API drop-down menu offers several URLs at which you can obtain the entire crash data set, but the one I will be working with for this post is the GeoJSON format. Before going into the details of connecting to the API, I’ll take a moment to discuss GeoJSON files.
JSON files are based on JavaScript and store information in a text format that is pretty easy for various types of programs to parse. GeoJSON files are essentially the same thing, but the information that they store corresponds to geographical data. Since the file for this particular data set is pretty large let’s take a quick look at the generic structure of a GeoJSON file to identify what elements we will need to extract once we load it into Power BI, instead of directly opening the file itself (an expanded version of this code can be found on the GeoJSON Wikipedia page here):
{  "type": "FeatureCollection",  "features": [    {      "type": "Feature",      "geometry": {        "type": "Point",        "coordinates": [102.0, 0.5]      },      "properties": {        "prop0": "value0"      }    }
It appears that there are two features: “geometry” and “properties”. It’s probably safe to guess that geometry contains the spatial information used to visualize the data set (coordinate pairs for points in this case, but they could also be lines or polygon boundaries). Properties (bold text in the above code) contains the various properties and values for each record (in our case, this corresponds to the different attributes of each crash). It’s worth noting here that by exploring the crash data set on the website, we can see that there are specific fields for Latitude and Longitude, which will fall under the properties feature — so we won’t have to extract them from the geometry feature.
This section will require that you have Power BI Desktop installed on your computer. It’s free to download and play around with!
From within Power BI Desktop, you’ll click the “Get Data” button on the top ribbon and choose “Web” from the drop down menu. This will pop-up a dialog box where you will paste in the URL for the GeoJSON file from the Open Data DC website.
Once the data set is imported, we’ll convert it into a table by clicking the “Into Table” button at the top left of the ribbon under the “Convert” tab.
Thinking back on our GeoJSON file structure exploration earlier, we expect there to be two features : geometry and properties, with properties containing the information we want for each of the crashes. In order to get to these, we’ll perform the following steps:
Filter the “Name” column down to “features”
Enumerate all of the individual crash records by clicking the two arrows at the top right of the “Values” column header, and choose “Expand to new rows”
Click the “Values” drop down again and choose the “properties” feature
You’ll now have something that looks like this:
We’re almost there. We’ve used that “Value” drop-down menu a few times, but...
After clicking the drop-down menu for the “Value” column once more (now named Value.properties), something beautiful happens — we find the attributes for the crashes. You can pick which columns to keep or keep them all, but for your own sanity deselect the “Use original column name as prefix” box (otherwise every column in your data set will be preceded with “Values.properties”).
There will be some follow up column transformations that you’ll likely want to do (a date field conversion, which can be found in the suggestions section below, or converting numeric fields to actual numbers instead of text), but the data is pretty much ready to use at this point! Below is a quick dashboard I put together with some of the most basic data contained within the GeoJSON file:
I would recommend creating a transformed “Report Date” column. The report date from the GeoJSON is in the following format: “YYYY-MM-DDTHH:MM:SS.000Z”. You can convert this by adding a custom column in the Power Query Editor with the following code:
DateTime.FromText(Text.Start( [REPORTDATE], 19))
Mapping the nearly 200k crashes as individual points is pretty pointless (no pun intended), as it will likely blanket the entire area. Additionally, the map visual that I used (ArcGIS Maps for Power BI) can only plot a limited number of points. Due to this, I’d recommend filtering the visual to only relevant dates and using the heat map as the map theme.
If you want to publish a dashboard containing this data, don’t forget to set the scheduled refresh through a gateway to keep it up to date!
That’s it! We’ve successfully connected to an API and transformed the provided GeoJSON files into a usable Power BI data set that can be refreshed periodically without having to manually replace the data when the source updates.