SVG Country Flags in Power BI
Discover how to display country flags in your Power BI reports with my quick guide. Learn to connect to an API that provides sharp, scalable SVG images for every nation, perfect for enhancing tables, slicers, and more
Have you got a requirement to display world country flags in Power BI? If so, you need to know about this free resource.
There is a website called restcountries.com, who maintain a list of every country in the world and their flags.
By using their API, we can get a nice list of every country in the world, along with their ISO 2/3 digit codes, numeric codes, region, and a URL to the SVG flag images.
Once you have this table in Power BI you can use the flags in tables, slicers, cards etc. As the images are SVG, they are quick to load, look really sharp and scale to as big as you need them to go.
There is a tonne of information in the API, but you can filter out anything you don't want or need.
How to Connect to the API in Power Query
Here's the Power Query code to get the country names (common & official), country codes, region and flag SVG URL's. Copy and paste this into the advanced editor to get what you need:
let
Source = Json.Document(Web.Contents("https://restcountries.com/v3.1/all")),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"name", "cca2", "ccn3", "cca3", "region", "flags"}, {"name", "cca2", "ccn3", "cca3", "region", "flags"}),
#"Expanded flags" = Table.ExpandRecordColumn(#"Expanded Column1", "flags", {"svg"}, {"svg"}),
#"Expanded name" = Table.ExpandRecordColumn(#"Expanded flags", "name", {"common", "official"}, {"common", "official"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded name",{{"svg", type text}, {"region", type text}, {"cca3", type text}, {"cca2", type text}, {"official", type text}, {"common", type text}, {"ccn3", Int64.Type}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"common", "Country Name (Common)"}, {"official", "Country Name (Official)"}, {"cca2", "Alpha-2 Code"}, {"ccn3", "Numeric Code"}, {"cca3", "Alpha-3 Code"}, {"region", "Region"}, {"svg", "Flag URL"}}),
#"Sorted Rows" = Table.Sort(#"Renamed Columns",{{"Country Name (Common)", Order.Ascending}})
in
#"Sorted Rows"
How to Set Flag URL Column as Image
This is an important step. For the SVG files to display correctly, we need to change the data type of the Flag URL column to Image.
Without doing this, you will just see a load of text when trying to use the column in visuals.
Once you set this, you can use the column in a table, matrix, slicers etc.
Download Readymade PBIX Instead
If you want a ready made PBIX file with the flags included, download it below. Enjoy!