Power BI: Creating Your Own Narratives – Table-Type Studio Projects
Documentation for Arria NLG’s Add-in for Power BI Desktop
This documentation is organized as follows:
- Open the sample Power BI report and import Arria’s custom visual
- Add a narrative to describe the visuals
- Create a project in Arria NLG Studio
- Modify the starter project
- Publish the NLG Studio sample project
- Link your published NLG Studio project to your Power BI report
NLG Studio offers the ability to work with different data types in different ways. You select your project type based on the type of data you have and the functions you need to use on the data. Each project type provides a slightly different experience.
The two categories of data that NLG Studio allows you to work with are tabular data and JSON data.
For tabular data—that is, spreadsheet data—you can choose from the project types that follow.
Describe Each Row. Each row in your input data is used to generate a separate story. Your Composer scripts and variable definitions can only refer to data elements within a single row.
Describe the Table. An entire multi-row spreadsheet is used to generate a single story. Your Composer scripts and variable definitions can refer to data elements across rows.
Describe Each Row in Context. Each row in your input data is used to generate a separate story, and data in the other rows provides context. Your Composer scripts and variable definitions can refer to data elements across rows.
For JSON data, NLG Studio provides a project type that allows working with all facets of data in JSON format:
Describe a JSON Object. An entire JSON object is used to generate a single story. Your Composer scripts and variable definitions can refer to data elements within the object.
Arria’s add-in for Power BI Desktop supports JSON-type projects and Describe the Table projects. This documentation is about BI integration with the Describe the Table project type.
For more information on the Describe the Table project type, see Describing a Whole Table.
If you are working with JSON data, see this topic that explains creating narratives based on JSON data.
This guide explains how to get and use Arria NLG’s add-in for Power BI Desktop.
You’ll begin by downloading a sample Power BI report. Next, you’ll import Arria’s custom visual into the report. After that, you’ll provide some information about your data and the kind of things you would like to say about it in a narrative.
Using Arria’s custom visual, you’ll add a narrative widget to the report. Then, using NLG Studio, you’ll create a very simple NLG project, which you’ll publish to the cloud. Then, you’ll link the published Studio project to the narrative widget to bring its NLG into the report.
By moving through the steps of creating a very simple NLG project, you’ll get a glimpse of the amazing things you can do when you extend Power BI with Arria’s NLG technology.
- Office 365 license
- Power BI Desktop (free download)
- Arria NLG Studio account (free trial) https://app.studio.arria.com/
- Our .PBIVIZ file (the Arria Power BI add-in).
BI analysts, BI developers
1. Download this sample Power BI report.
2. In Power BI Desktop, open the report.
3. In the Visualizations pane (you may need to click Visualizations to open the pane), click the ellipsis (Import a custom visual).
4. You have two options for bringing the Arria add-in into your dashboard. Choose one of the following import options:
- Select Import from file if you have received the .PBIVIZ file directly from Arria (from this page). For this option, browse to find the file on your machine and click Open.
- Select Import from marketplace to get the add-in from the Microsoft AppSource. For this option, find the Arria add-in by searching on “arria”, then click Add.
You should now see the Arria logo in your Visualizations pane.
1. Make sure to click off the charts. (If you have a chart selected during the next step, the chart will be overwritten.)
2. In the Visualizations pane, click the Arria add-in icon to add the narrative widget to your report. Move it and resize it to fit to the left of the charts.
3. With the narrative widget selected, from the Fields pane, select the required fields: Product, Profit, Sales, Segment, Year.
4. In the narrative widget, you can now create a custom narrative. Click Create Custom Narrative.
5. Now that you’ve selected the data you’re working with in Power BI (the fields Product, Sales, Profit, Segment, Year), you’re ready to bring that specific set of data into NLG Studio.
Note: Scroll down in the widget or click the Focus Mode icon if you want to get a better look at the CSV data.
6. In the Arria NLG widget, make sure the Simple/Advanced toggle is set to Simple.
7. Make sure the Studio Project Type is set to Describe a Table. (This is because the NLG Studio starter project you are going to download is a table-type Studio project. If the Studio project you associate with your dashboard is based on JSON data, you would choose Describe a JSON Object here.)
8. Click in the title text for the View Dataset in CSV section and explore the data.
9. Click in the title text for the View CSV Payload section, and look at the data objects you are sending as part of the API call (described below).
Note: Describe the Table-type projects in NLG Studio require you to have a unique field as the first column. To accommodate this requirement, the Arria add-in by default adds an auto-increment field to the data as the first field. If your data already has an ID column and you want to use that instead of the default auto-increment field, de-select Add auto increment ID. If you do use your own ID column, make sure that in the Fields pane, it is the first column you select so the unique column is in the first position of the table data brought into NLG Studio.
10. In the View Dataset in CSV section, at the top right, click the download icon to download the data.
11. In the Open Browser dialog, click OK.
12. Check your machine’s Download folder for the file called
data.csv. In the next section, you’ll import this into the BI Starter project in NLG Studio.
You are going to create a very simple sample project in NLG Studio in order to get a basic understanding of how you can use NLG Studio to augment your analytics. (For instructions that show how to create a more elaborate narrative, see the NLG Studio documentation.)
1. Download the BI Starter project for NLG Studio. The BI Starter project contains some helper functions that you’ll see as sub-scripts under the Main script in the Compose view. These helper functions are user-defined functions created specifically for working with BI data.
2. Check your machine’s Download folder for the file called BI+Starter+Project+-+Table+Type+Data.zip and extract its contents.
3. Log in to NLG Studio.
4. In NLG Studio, in your dashboard, click the Import a Project icon to import the unzipped BI Starter Project file.
5. In the NLG Studio dashboard, click the starter project to open it.
Note: In the future, this project may become your starting point for all your BI projects created in NLG Studio. So if you like, you can rename the project to something generic. Or, you might want to give it a name that matches the name of the corresponding .PBIX file. To change the project’s name, just click in the name at the top left of the screen.
You’ll see the data that comes with the starter project. When you’re ready to modify this starter project, you’re going to replace this data with the data from your field selections in Power BI.
6. In the left navigation ribbon, click Compose to access the Compose editor.
In the editor, you’ll see the contents of the Main script, which is empty aside from a reference to a ReadMe sub-script. This ReadMe describes the BI data format used in Power BI projects, and it provides information about the helper functions in the project. The helper functions appear as a list of sub-scripts under the Main script. You can click on the sub-scripts to see their contents (the user-defined helper functions).
7. Make sure you’re in the Main script, then at the top right of the screen, click Preview. Read the ReadMe information.
You are going to compose a piece of narrative describing the total sales and total profit for the selected period. To generate the text for that, you’ll add some code in the Compose editor. But first, you need to import the data set that is specific to the fields you have selected in Power BI: Product, Profit, Sales, Segment, Year.
1. In the left navigation ribbon, click Data to go to the Data view.
2. At the top right of the screen, click the import icon to bring in the file called
data.json from your Downloads folder.
3. Click Compose to go to the Compose view.
4. Click Main to begin editing the Main script.
5. Remove the text that is already there ([[ReadMe]]).
6. Copy/paste the following:
For the [[inflectNoun('year',len(unique(Year)))]] [[unique(Year)]], the total sales were [[formatCurrency(totalVal(Sales),'USD','#.00')]] and the total [[direction(totalVal(Profit) ,'profit','loss' )]] was [[formatCurrency(abs(totalVal(Profit)),'USD','#.00')]].
7. In the top right of the screen, click Preview.
Your previewed output should look like this:
Now add another piece of narrative. You’ll add a sentence reporting on the sectors that contributed the most and least profit. As an exercise, you’ll create a variable that you’ll use to replace some of your existing code.
8. In the left navigation ribbon, click Variables then +NEW VARIABLE to add a variable called “currency” of type STRING and source USD, as shown here:
9. Click Done to save the new variable.
10. In Main, replace the existing script with the following:
For the [[inflectNoun('year',len(unique(Year)))]] [[unique(Year)]], the total sales were [[formatCurrency(totalVal(Sales),currency,'#.00')]] and the total [[direction(totalVal(Profit) ,'profit','loss' )]] was [[formatCurrency(abs(totalVal(Profit)),currency,'#.00')]]. The best-selling product was [[getKey(topNInstancesbyDimension(Product,Sales,1))]] with a total of [[formatCurrency(getValue(topNInstancesbyDimension(Product,Sales,1)),currency,'#.00')]]. The [[getKey(topNInstancesbyDimension(Segment,Profit,1))]] segment reported the most [[direction(getValue(topNInstancesbyDimension(Segment,Profit,1)), 'profit','loss')]] ([[formatCurrency(getValue(topNInstancesbyDimension(Segment,Profit,1)),currency,'#.00')]]).
11. Click Preview.
Your previewed output should look like this:
1. In NLG Studio, in your BI Starter Project, click Publish in the left navigation ribbon.
2. In the Publish view, click the Publish button.
3. At the Congratulations! message, make note of the URL for your project. (From inside the URL box, you can click the copy icon to copy the URL to your clipboard. Paste it to a notepad.)
4. Under Run Project, click API.
5. Scroll down, then click the Generate API Key button.
6. Make note of the API key generated. (Again, you can copy it to your clipboard from inside the box with the key. Paste it to a notepad.)
1. In Power BI Desktop, in your report, click the Focus mode icon to maximize the narrative widget.
2. In the first empty field in the widget, enter the NLG service URL (the URL you made note of after publishing your NLG Studio project).
3. In the second empty field in the widget, paste in your API key.
4. Click the Generate Text button, and then Back to Report.
5. Click around on different pieces of the charts to see how the narrative responds. To select multiple bars at the same time, use CTRL+Click.
6. (Optional) You can copy the narrative for pasting elsewhere by clicking the copy icon in the top right of the widget.
Note: Above, in Add a Narrative to Describe the Visuals, you selected the data you’re working with in Power BI (the fields Product, Sales, Profit, Segment, Year). The order in which you ticked those checkboxes in the Fields pane is reflected in the dataset you import into NLG Studio. So for example, if you went top-down and first clicked Product, Sales, Segment, and Year, and then went back up and clicked Profit, then in your dataset, Profit would appear after Year, not after Product. This is important to know because if you change the order of your selected fields in Power BI after creating and publishing your Studio project, you must re-download the sample data, bring it into your Studio project again, and republish the Studio project.
About the Methods Used:
getData(): This method returns the underlying JSON data (the data you see in the View Dataset in JSON section and View CSV Payload section).
setData(modifiedData): This method sets the
modifiedData variable (the transformed data object). After using this method, the transformed data object is available for posting to the NLG service URL.
The following example shows how you can assign some variable values to the input JSON data object that can be used in the narrative script:
To try out a custom mapping script:
1. Copy the above script and paste it in the Edit Mapping Script section of the narrative widget.
2. Click Generate Text in the Arria widget).
You should see the currencies expressed in sterling (£) instead of dollars (US$).
What you have done here is you have used a new mapping script to overwrite your design-time variable values.
For more information, see: