Sentiment Analysis in Power BI

How to use natural language sentiment analysis in your text data with Power BI.

I am really starting to fall in love with Power BI now that I have the ability to use Python scripts to transform my data and bring my visuals to a whole new level. We will use the NLTK Sentiment Intensity Analyzer that will iterate over each of our comments and provide a polarity score that ranges from 1 to -1.

How to Use Sentiment Analysis in Power BI using Python’s Natural Language Processing Tool Kit

This is a super easy script to write. We will perform the following steps prior to opening up our Power BI desktop. Here are the steps will take to use NLTK’s sentiment analyzer. Visualize Data with Python. Interpret Large Datasets. A/B Test Your Hypothesis. Save 50%!

Load our data into Power BI. Write 4 lines of Python script. Create a conditional column with our polarity scores.

For the first part, I didn’t use a special dataset. I just wrote 6 lines of varying sentiment that our script will analyze. Let’s take a look at the data:

From the data you can see that some are negative, some are positive and some are neutral. Once this data is loaded into Power BI, we can initiate the Python script.

Access the Python Script

Once your Script Window is open, you will import the following script.

#load in our dependencies import pandas as pd from nltk.sentiment.vader import SentimentIntensityAnalyzer #load in the sentiment analyzer sia=SentimentIntensityAnalyzer() #apply the analyzer over each comment dataset['polairty scores'] =dataset['Message'].apply(lambda x: sia.polarity_scores(x)['compound'])

If you look at the code, I have annotated or documented the code using the hashtag. Also, remember that the default variable for the dataset in Power BI is called dataset. You will need to expand the new table to get the resulting output of the sentiment analysis function.

Once you expand the table you will have the results of the polarity scores.

Remember that sentiment ranges from -1 to 1. With -1 being the most negative and 1 being the most positive. Now I am going set up a conditional column with the words “positive”, “negative”, and “neutral”.

Visualize your Results

The last part of the adventure is to visualize and communicate your results using the Power BI visuals.

Check out the video Instructions: