In today’s data-driven world, understanding customer sentiment is crucial for enhancing services, products, and customer experiences. Power BI is a powerful tool for visualizing data, but what if you could perform sentiment analysis on text data directly within Power BI? Imagine being able to analyze customer feedback, product reviews, or social media posts and instantly visualize whether your brand is viewed positively or negatively.
In this blog post, we’ll walk through performing sentiment analysis in Power BI using Python scripts and NLTK’s Sentiment Intensity Analyzer. The final outcome will be visuals that represent the polarity of your text data, ranging from positive to negative, with scores between -1 and 1.
The Natural Language Toolkit (NLTK) is a comprehensive Python library for processing human language data. It includes various tools for text analysis, such as tokenization, classification, stemming, tagging, parsing, and, most relevant to this tutorial, sentiment analysis. NLTK’s Sentiment Intensity Analyzer (VADER) is ideal for analyzing short pieces of text, like social media posts or product reviews, where sentiment intensity is scored from -1 (most negative) to 1 (most positive).
Step 1: Preparing Your Data in Power BI
Import Data:
Before diving into sentiment analysis, ensure your text data is loaded into Power BI. This data can come from various sources, such as customer feedback, survey responses, or product reviews stored in databases or files. In our case, we will load the data from a locally saved CSV file. You can find the link to the dataset here.
Extract Feedback Data
Once your CSV file is loaded into Power BI, review the imported data to locate the columns relevant to sentiment analysis. Focus on extracting only the necessary columns, such as review text or ratings, which are essential for accurate sentiment analysis.
Clean the Data:
Data cleanliness is crucial for meaningful sentiment analysis. Use the Power Query Editor to clean your data by removing duplicates, replacing values, and filtering out irrelevant rows. Ensure that the text column you’ll be analyzing is clean—remove unnecessary characters, duplicates, and irrelevant data points.
Prepare for Python Integration:
After cleaning the data, it’s time to prepare it for sentiment analysis using a Python Script in Power BI. The Python Script will be used to transform and analyze the sentiment of the text data.
Step 2: Set Up Python Integration in Power BI
Install and Configure Python:
To run Python scripts in Power BI, you need to have Python installed and properly configured. Ensure that Python and necessary libraries (like NLTK) are installed. You can configure Power BI to recognize Python by navigating to File > Options and settings > Options > Python scripting and setting the path to your Python installation.
Install Necessary Libraries:
Before performing sentiment analysis, ensure that the required Python libraries (such as NLTK for Natural Language Processing) are installed in your environment. Use the following command to install NLTK.
https://gist.github.com/salmah52/59990645d637a63cb0561358bc652d09
Download the Sentiment Analysis Lexicon:
NLTK uses the VADER (Valence Aware Dictionary for Sentiment Reasoning) lexicon to perform sentiment analysis. Make sure to download it in your Python environment by running:
https://gist.github.com/salmah52/32c3818a0827a5600ba48e55c4cfd816
Run Python Script in Power BI:
In the Transform tab in Power Query, select Run Python Script. A dialogue box will appear where you can enter your Python script. Use the following script to perform sentiment analysis on your text data:
https://gist.github.com/salmah52/7be532f36e1945a767d0f4c6abfd5524
Explanation of the Script:
- Imports Required Libraries: The script imports pandas for handling the dataset and SentimentIntensityAnalyzer from nltk.sentiment.vader for performing sentiment analysis.
- Initializes the Sentiment Analyzer: An instance of SentimentIntensityAnalyzer is created (Sia), which will be used to calculate the sentiment polarity of the text data.
- Applies Sentiment Analysis: The script applies the polarity scores method to each row in the Message column. This method analyzes the sentiment of the text and returns a compound score that measures the overall sentiment. The result is stored in a new column called polarity scores.
- Returns the Results: The updated dataset with the polarity scores column is returned to Power BI for further analysis and visualization.
Step 3: Categorizing Sentiment in Power Query
Once the sentiment scores have been calculated and added to the dataset using the Python script, the next step is to create a new column that categorizes these scores into Positive, Neutral, and Negative reactions. This makes it easier to visualize sentiment trends in Power BI.
Create a Conditional Column:
In Power Query, you can create a new column that classifies the sentiment based on the polarity score generated in the previous step. Here’s how you can do it:
- In Power Query, go to the Add Column tab and click on Conditional Column.
- Name the new column something like Reaction.
- Use the following conditions to categorize the sentiment:
- If polarity score is less than 0, then output “Negative”.
- Else if polarity score is less than 0.5, then output “Neutral”.
- Else if polarity score is greater than 0.5, then output “Positive”.
Power BI will automatically classify each comment based on its sentiment score, making the data more interpretable.
Step 4: Visualizing the Sentiment Scores in Power BI
Once you have the sentiment scores in your dataset, the next step is to visualize the results in Power BI. Here are some options for displaying the sentiment analysis results:
1. Bar/Column Charts:
Create a bar or column chart to show the average sentiment per category, such as customer or product ID. This gives a clear view of whether different categories have more positive or negative sentiments.
2. Line Charts:
Use a line chart to track sentiment trends over time. This is especially useful if your data includes a time dimension (such as review dates) to visualize changes in sentiment.
3. Pie/Donut Charts:
Create a pie or donut chart to display the distribution of positive, negative, and neutral feedback. This provides a high-level overview of overall customer sentiment.
4. Word Clouds:
Power BI offers custom visuals like word clouds. You can create a word cloud to highlight the most frequently used terms in positive or negative reviews.
Conclusion
By leveraging Python and NLTK in Power BI, you can transform raw text data into valuable insights. Sentiment analysis allows businesses to understand customer feedback at a glance and make data-driven decisions to improve customer satisfaction and overall performance. Whether it’s product reviews, survey responses, or social media comments, integrating Python-powered sentiment analysis into Power BI is a powerful way to elevate your data analytics and visualization capabilities.

Your blog is a treasure trove of valuable insights and thought-provoking commentary. Your dedication to your craft is evident in every word you write. Keep up the fantastic work!