Forum Discussion
Using FRED Data
- 7 years ago
API can be tough. Basically, it's like getting from a web source. Check out these instructions: https://blogs.msdn.microsoft.com/charles_sterling/2016/05/25/how-to-call-rest-apis-and-parse-json-with-power-bi/
I had been trying to get more than one economic indicator in power bi at the same time through their API so my friend and I created a python script to collect whatever number of series you want and appends them already making it really easy to slice when in power bi. I hope you guys enjoy this it took me a long time. It works with python 3.77 and make sure you have the modules requests, matplotlib and pandas. Oh and make sure you add your API token(key)
Enjoy and don't take credit for it.
#Modules
import urllib.request as urllib2
import urllib, json
import numpy as np
import pandas as pd
#Static Variables
TOKEN = 'YourAPItoken'
#initialzing array for tickers
tickers= []
#open file and create one array of tickers IDs
tickers =['mspnhsus'
]
#temporary variable store values from "for loop"
d = []
#creating list of URL based on tickers list
for ticker in tickers:
tdata = '['
url = str(f'https://api.stlouisfed.org/fred/series/observations?series_id={ticker.strip()}&api_key={TOKEN}&file_type=json')
#Opening web api and read JSON data
response = urllib2.urlopen(url)
#create JSON object
json_obj = json.load(response)
for i in json_obj['observations']:
d.append({'ticker': ticker, 'date': i['date'], 'value': i['value']})
#create variable "df" using format dataframe
df = pd.DataFrame(data=d)
#print data ready to use for PowerBi
print(df)
- rverner4 years agoFrequent Visitor
You are awesome!