How to integrate Power BI to the Facebook Graph API

How to integrate Power BI to the Facebook Graph API

How to integrate Power BI to the Facebook Graph API

SQLShack

SQL Server training Español

How to integrate Power BI to the Facebook Graph API

May 4, 2018 by Esat Erkec

The challenge

With the popularity of social networking and sharing sites and their widespread availability, internet marketing techniques have also been rapidly influenced. Sharing sites with high visitor traffic offers great potential for advertisers, so much so that it is almost unheard of now that brands or products do not have a profile or group on a social networking site. Markets have the opportunity to communicate directly with consumers by creating profiles or groups in sharing sites. Thanks to the comments and forums on the pages, they are able to follow both positive and negative attitudes of the target groups about themselves and thus have more precise ideas about what to do next. Due to all of this above and more, in this article we will integrate Facebook Graph API with Power BI to see if we can tap into this wealth of information. Power BI offers a Facebook connector and this connector is capable of mining data from Facebook (posts, likes, comments etc…). This connector uses Facebook Graph API. This API provides read and write access to the Facebook social graph. With the help of these APIs, we will create a SQLShack Facebook analysis dashboard.

Power BI Facebook Connector

With this connector, we can connect to and get data from Facebook. When you click “Get Data” and then “More”, we can find Facebook connector. We will write the account name of the person on Facebook for whom we want to get the data. But, in this article we won’t use the Power BI standard Facebook connector. We will create Facebook Graph API query and then get the data with JSON (JavaScript Object Notation; JSON is a lightweight data-interchange format) format from Facebook. Because JSON format is always more flexible than other connection types because it is HTTP-based data and has wide usage.

Facebook Graph API Explorer

Facebook Graph API provides to get data out of Facebook. Facebook Graph API Explorer help us to test and create query data. Graph API provides JSON-based data, and for this reason, we can use it in Power BI. In our Power BI dashboard, we will need Post caption Post name Post message Post id Post created time How many people like this post We have to write the query that will return us the above fields. With this query, we can get above fields. sqlshack/posts?fields=id,likes.limit(10).summary(true),message,name,caption,created_time Access Token provides a secure connection between Facebook and requested program.

Create Facebook Analyze Dashboard on Power BI

Our Facebook Graph API query is ready for getting data over Facebook. In this step, we will prepare data for Power BI dashboard. We will open Power BI and select ‘Get Data’ then click ‘Blank Query’ We will open “Advanced Editor” and paste our function script to query editor. In this script web URL request data from Facebook. 12345 let MyJsonRecord = Json.Document(Web.Contents("https://graph.facebook.com/v2.12/sqlshack/posts?fields=id,likes.summary(true),message,name,caption,created_time&access_token=WRITEACCESSTOKENHERE ")), MyJsonTable= Table.FromRecords( { MyJsonRecord } ) in MyJsonTable This step is very complex because we will convert JSON data to data table, split some of the text values and make some data conversions. For this reason, we will follow step by step. Get Facebook data with JSON format Click list data and open record data Convert record dataset to table Chose “Select or enter delimiter” property as None and chose “How to handle extra columns” as “Show as errors” Select all columns for data table Select summary fields on Column1.likes. Because incoming data from Facebook summary fields includes total_count. This field defines a total number of post likes.
Select total_count field on Column1.likes.summary After these steps Facebook raw data is ready. Now, we will split SQLShack Facebook post to find article writer. Because SQLShack Facebook post includes the article writer name between /w and # characters. Through this logic will split Column1.message and get the name of the article writer. Click column1.message and select “split column” Select custom delimiter and write /w to text box. Click “OK” Repeat the previous step over Column1.2 and split “Left-most delimiter” In this step, we will add year and month to the column over created_time Change created_time column data type to “Date/Time” Transform changed field to year and month Finally, we will rename columns and then click “Close & Apply” You can use the below script but pay attention to add access token 12345678910111213141516171819 let MyJsonRecord = Json.Document(Web.Contents("https://graph.facebook.com/v2.12/sqlshack/posts?fields=id,likes.summary(true),message,name,caption,created_time&access_token=writetokenhere ")), data = MyJsonRecord[data], #"Converted to Table" = Table.FromList(data, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"likes", "message", "name", "caption", "created_time"}, {"Column1.likes", "Column1.message", "Column1.name", "Column1.caption", "Column1.created_time"}), #"Expanded Column1.likes" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1.likes", {"summary"}, {"Column1.likes.summary"}), #"Expanded Column1.likes.summary" = Table.ExpandRecordColumn(#"Expanded Column1.likes", "Column1.likes.summary", {"total_count"}, {"Column1.likes.summary.total_count"}), #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1.likes.summary",{{"Column1.created_time", type datetimezone}}), #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Column1.created_time", "Column1.created_time - Copy"), #"Extracted Year" = Table.TransformColumns(#"Duplicated Column",{{"Column1.created_time", Date.Year, Int64.Type}}), #"Extracted Month" = Table.TransformColumns(#"Extracted Year",{{"Column1.created_time - Copy", Date.Month, Int64.Type}}), #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Month", "Column1.message", Splitter.SplitTextByDelimiter("w/", QuoteStyle.Csv), {"Column1.message.1", "Column1.message.2"}), #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column1.message.1", type text}, {"Column1.message.2", type text}}), #"Split Column by Delimiter1" = Table.SplitColumn(#"Changed Type1", "Column1.message.2", Splitter.SplitTextByEachDelimiter({"#"}, QuoteStyle.Csv, false), {"Column1.message.2.1", "Column1.message.2.2"}), #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Column1.message.2.1", type text}, {"Column1.message.2.2", type text}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type2",{{"Column1.likes.summary.total_count", "Total Likes"}, {"Column1.message.1", "Post Message"}, {"Column1.message.2.1", "Post Writer"}, {"Column1.message.2.2", "Post Hashtag"}, {"Column1.created_time", "Post Year"}, {"Column1.created_time - Copy", "Post Month"}, {"Column1.caption", "Post Caption"}, {"Column1.name", "Post Cap2"}}), #"Changed Type3" = Table.TransformColumnTypes(#"Renamed Columns",{{"Post Year", type text}, {"Post Month", type text}, {"Total Likes", type number}})in #"Changed Type3" In this step, we will click “Close & Apply” and will focus on Power BI design screen. In this screen, we will add different types of charts and tables. Now, we will add “Stacked Column Chart” to visualize the writers and likes of the article on the graph. We will drag and drop “Post Writer” column to Axis field and “Total Likes” column to “Value” field. In this step, we will change some of stacked column chart format properties. We will increase “Text-size” property under “X-Axis” tab and change Font-Family to “Arial Black” because in x-axis we will show the writer names. Finally, we can look at stacked column chart. This chart defines which writer get how many likes. When we will drag and drop total likes to legend field. This graphic will define a number of likes per individual article. Note: In the above chart we see a “blank” writer. It means the split function cannot split article writer name. We will add a table to show post writer and post a message. We will drag and drop “Post Message” and “Post Writer” columns to values field. We will add a card to show a total number of likes.
In this last step, we will add “Donut Chart” and percentage of post likes. We will add donut chart and will drag and drop “Post Month” to “Legend” field and “Total Likes” to “Values” field. Finally, our dashboard is ready.

Conclusion

In this article we talked about Power BI and Facebook integration. This integration is one of the Power BI advantages. But Power BI offers various capabilities. It can use Power BI multiple platforms Easy report development Simple usage experience Marketplace support for third-party component. Various data connector support Interactive reports and geo-map visualizations Leader of Gartner Magic Quadrant for Business Intelligence Due to above descriptions Power BI development team seems very motivated to offer new features.

References

Facebook analytics using Power BI Desktop Graph API – Facebook for Developers Create Power BI Reports from JSON Data Exposed by REST Service
Author Recent Posts Esat ErkecEsat Erkec is a SQL Server professional who began his career 8+ years ago as a Software Developer. He is a SQL Server Microsoft Certified Solutions Expert.

Most of his career has been focused on SQL Server Database Administration and Development. His current interests are in database administration and Business Intelligence. You can find him on LinkedIn.

View all posts by Esat Erkec Latest posts by Esat Erkec (see all) Five beneficial Azure Data Studio Extensions for SQL developers - July 19, 2022 How to build custom widgets on Azure Data Studio - July 7, 2022 How to obtain SQL Execution Plans using different methods - June 30, 2022

Related posts

Using Power BI Desktop to connect to SQL Server and Facebook How to integrate Power BI data alerts into Slack How to plot a SQL Server 2017 graph database using PowerBI Understanding Graph Databases in SQL Server Using Power BI Free License to Embed Power BI into Dynamics 365 13,712 Views

Follow us

Popular

SQL Convert Date functions and formats SQL Variables: Basics and usage SQL PARTITION BY Clause overview Different ways to SQL delete duplicate rows from a SQL Table How to UPDATE from a SELECT statement in SQL Server SQL Server functions for converting a String to a Date SELECT INTO TEMP TABLE statement in SQL Server SQL WHILE loop with simple examples How to backup and restore MySQL databases using the mysqldump command CASE statement in SQL Overview of SQL RANK functions Understanding the SQL MERGE statement INSERT INTO SELECT statement overview and examples SQL multiple joins for beginners with examples Understanding the SQL Decimal data type DELETE CASCADE and UPDATE CASCADE in SQL Server foreign key SQL Not Equal Operator introduction and examples SQL CROSS JOIN with examples The Table Variable in SQL Server SQL Server table hints – WITH (NOLOCK) best practices

Trending

SQL Server Transaction Log Backup, Truncate and Shrink Operations Six different methods to copy tables between databases in SQL Server How to implement error handling in SQL Server Working with the SQL Server command line (sqlcmd) Methods to avoid the SQL divide by zero error Query optimization techniques in SQL Server: tips and tricks How to create and configure a linked server in SQL Server Management Studio SQL replace: How to replace ASCII special characters in SQL Server How to identify slow running queries in SQL Server SQL varchar data type deep dive How to implement array-like functionality in SQL Server All about locking in SQL Server SQL Server stored procedures for beginners Database table partitioning in SQL Server How to drop temp tables in SQL Server How to determine free space and file size for SQL Server databases Using PowerShell to split a string into an array KILL SPID command in SQL Server How to install SQL Server Express edition SQL Union overview, usage and examples

Solutions

Read a SQL Server transaction logSQL Server database auditing techniquesHow to recover SQL Server data from accidental UPDATE and DELETE operationsHow to quickly search for SQL database data and objectsSynchronize SQL Server databases in different remote sourcesRecover SQL data from a dropped table without backupsHow to restore specific table(s) from a SQL Server database backupRecover deleted SQL data from transaction logsHow to recover SQL Server data from accidental updates without backupsAutomatically compare and synchronize SQL Server dataOpen LDF file and view LDF file contentQuickly convert SQL code to language-specific client codeHow to recover a single table from a SQL Server database backupRecover data lost due to a TRUNCATE operation without backupsHow to recover SQL Server data from accidental DELETE, TRUNCATE and DROP operationsReverting your SQL Server database back to a specific point in timeHow to create SSIS package documentationMigrate a SQL Server database to a newer version of SQL ServerHow to restore a SQL Server database backup to an older version of SQL Server

Categories and tips

►Auditing and compliance (50) Auditing (40) Data classification (1) Data masking (9) Azure (295) Azure Data Studio (46) Backup and restore (108) ▼Business Intelligence (482) Analysis Services (SSAS) (47) Biml (10) Data Mining (14) Data Quality Services (4) Data Tools (SSDT) (13) Data Warehouse (16) Excel (20) General (39) Integration Services (SSIS) (125) Master Data Services (6) OLAP cube (15) PowerBI (95) Reporting Services (SSRS) (67) Data science (21) ►Database design (233) Clustering (16) Common Table Expressions (CTE) (11) Concurrency (1) Constraints (8) Data types (11) FILESTREAM (22) General database design (104) Partitioning (13) Relationships and dependencies (12) Temporal tables (12) Views (16) ►Database development (418) Comparison (4) Continuous delivery (CD) (5) Continuous integration (CI) (11) Development (146) Functions (106) Hyper-V (1) Search (10) Source Control (15) SQL unit testing (23) Stored procedures (34) String Concatenation (2) Synonyms (1) Team Explorer (2) Testing (35) Visual Studio (14) DBAtools (35) DevOps (23) DevSecOps (2) Documentation (22) ETL (76) ►Features (213) Adaptive query processing (11) Bulk insert (16) Database mail (10) DBCC (7) Experimentation Assistant (DEA) (3) High Availability (36) Query store (10) Replication (40) Transaction log (59) Transparent Data Encryption (TDE) (21) Importing, exporting (51) Installation, setup and configuration (121) Jobs (42) ►Languages and coding (686) Cursors (9) DDL (9) DML (6) JSON (17) PowerShell (77) Python (37) R (16) SQL commands (196) SQLCMD (7) String functions (21) T-SQL (275) XML (15) Lists (12) Machine learning (37) Maintenance (99) Migration (50) Miscellaneous (1) ►Performance tuning (869) Alerting (8) Always On Availability Groups (82) Buffer Pool Extension (BPE) (9) Columnstore index (9) Deadlocks (16) Execution plans (125) In-Memory OLTP (22) Indexes (79) Latches (5) Locking (10) Monitoring (100) Performance (196) Performance counters (28) Performance Testing (9) Query analysis (121) Reports (20) SSAS monitoring (3) SSIS monitoring (10) SSRS monitoring (4) Wait types (11) ►Professional development (68) Professional development (27) Project management (9) SQL interview questions (32) Recovery (33) Security (84) Server management (24) SQL Azure (271) SQL Server Management Studio (SSMS) (90) SQL Server on Linux (21) ►SQL Server versions (177) SQL Server 2012 (6) SQL Server 2016 (63) SQL Server 2017 (49) SQL Server 2019 (57) SQL Server 2022 (2) ►Technologies (334) AWS (45) AWS RDS (56) Azure Cosmos DB (28) Containers (12) Docker (9) Graph database (13) Kerberos (2) Kubernetes (1) Linux (44) LocalDB (2) MySQL (49) Oracle (10) PolyBase (10) PostgreSQL (36) SharePoint (4) Ubuntu (13) Uncategorized (4) Utilities (21) Helpers and best practices BI performance counters SQL code smells rules SQL Server wait types © 2022 Quest Software Inc. ALL RIGHTS RESERVED. GDPR Terms of Use Privacy
Share:
0 comments

Comments (0)

Leave a Comment

Minimum 10 characters required

* All fields are required. Comments are moderated before appearing.

No comments yet. Be the first to comment!