# Microsoft Power BI: integration with Wolfram Language and Mathematica

**URL:** <https://community.wolfram.com/t/microsoft-power-bi-integration-with-wolfram-language-and-mathematica/17940>\
**Category:** Staff Picks\
**Tags:** business-analytics, data-science, mathematica, wolfram-add-ons, external-interfaces, graphics-and-visualization, packages-and-paclets, staff-pick\
**Created:** [April 7, 2021, 8:34pm UTC](https://community.wolfram.com/t/microsoft-power-bi-integration-with-wolfram-language-and-mathematica/17940 "2021-04-07T20:34:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![beny74483](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/beny74483/32/14307_2.png) [@beny74483](https://community.wolfram.com/u/beny74483)\
**Post date:** [April 7, 2021, 8:34pm UTC](https://community.wolfram.com/t/microsoft-power-bi-integration-with-wolfram-language-and-mathematica/17940/1 "2021-04-07T20:34:31Z")

</div>

> GitHub repository: [GitHub - ben-izd/Power-BI-Mathemaica-integration: Use Mathematica visualization capabilities inside Power BI. · GitHub](https://github.com/ben-izd/Power-BI-Mathemaica-integration)

* * *

Since 2018 Microsoft Power BI has the ability to connect with R and python. Now you can use Mathematica to create visualization inside Power BI or send data from Mathematica to Power BI.

This post consists of two code file which are also included at the end of this post:

- [`viz_server.nb`](https://github.com/beny74483/Power-BI-Mathemaica-integeration/blob/main/viz_server.nb) to use Mathematica visualization inside Power BI
- [`data_server.nb`](https://github.com/beny74483/Power-BI-Mathemaica-integeration/blob/main/data_server.nb) to send Mathematica data to Power BI

## How sending Mathematica Plots/Visualization to Microsoft Power BI works

Power BI supports python visualization made by `matplotlib`, this solution will send Mathematica code from Power BI, receive the result as an image, and store it inside a `matplotlib` object.

## How sending Mathematica data directly to Microsoft Power BI works

Power BI supports many data sources, we could either use python or a web interface. This solution will deploy a local server that redirects to your data as `JSON` format.

# Sending Mathematica Plots to Power BI

## Requirements:

- Python
- Python `matplotlib` module
- Mathematica

> Warning: Since Mathematica code uses the `ToExpression` function which can easily be manipulated to harm your computer, only run and open script files that you trust.

## Setup

Before starting, make sure the python Power BI uses, is the correct one by going to `File` \> `Options and Settings` \> `Options` \> `Python Scripting`

Either download [`viz_server.nb`](https://github.com/beny74483/Power-BI-Mathemaica-integeration/blob/main/viz_server.nb) or copy the code and evaluate them. Now you’ll have a local server running on port 38000 (you can change it to any number you like as long as it’s accessible)

Inside Power BI insert a new `Python Visual`.

 ![enter image description here](https://global.discourse-cdn.com/wolfram/original/3X/5/d/5d491ef1eb0fab2065a46e3fa56ca9dbc2809588.png)

If asks you to enable `Enable script visuals`, click `Enable`.

 ![enter image description here](https://global.discourse-cdn.com/wolfram/original/3X/e/0/e0b8530b0283bd7adf7ebf259a5da85f6fcc2c37.png)

You should also provide at least one Field to be able to write python code so here I’ll import a sample data file and drag `Price` Field to `Python Visual`’s `Values` section. If you need any field in your calculation, drag them into `Values` section.

 ![enter image description here](https://global.discourse-cdn.com/wolfram/original/3X/4/0/402b6c883377885de8d23eb8b8d44ebe44dac630.png)

In the `Python Script Editor`, paste these codes:

```
import io
import base64
import matplotlib.pyplot as plt
import json
from urllib import request

# Change this section
PORT = 38000

mathematica_code = 'ListLinePlot[Flatten[arg1]]'

pdata = json.dumps({'script':mathematica_code,'data':{'arg1':dataset.values.tolist()}})
# end section

req = request.Request(f'http://localhost:{PORT}/evaluate',data= pdata.encode('utf-8'),method='POST')
res = request.urlopen(req).read()
result = io.BytesIO(base64.b64decode(res))

img = plt.imread(result)
img_dim = img.shape

imgplot = plt.imshow(img,extent=[0,img_dim[1],0,img_dim[0]])
plt.axis('scaled')
plt.axis('off')
plt.tight_layout()
plt.show()

```

Mathematica will receive the request and will evaluate `script` with the data provided in `data` (if is available). You can use any variable name, here we used `arg1` therefore I should use `arg1` in the Mathematica too.

The result of our sample code is:

 ![enter image description here](https://global.discourse-cdn.com/wolfram/original/3X/4/e/4eb0b61b1f5ae0588f1acb72d6efa7a3201c37cd.png)

You could use only the script part without providing any `data`:

 ![enter image description here](https://global.discourse-cdn.com/wolfram/original/3X/5/c/5c2e8348912eacb6f637655057be6d78aa23f8bd.png)

### Example 2 - Clustering

Use mathematica `FindClusters` to find clusters for `price` and `quantity` fields:

 ![enter image description here](https://global.discourse-cdn.com/wolfram/original/3X/e/0/e0e21e0ff9d149fb405919474b3d423b778f895c.png)

## Possible Issues

If your script failed to evaluate, error messages will be sent as images to Power BI. Here you see the result of plotting `Sin[x]/0`:

 ![enter image description here](https://global.discourse-cdn.com/wolfram/original/3X/2/1/21e43eb29eaa31d2dca6070f42bc38ffe995c933.png)

# Sending Mathematica data directly to Power BI

## Requirements:

- Python
- Python `matplotlib` module
- Mathematica
- Enabling `Json Table Inference` in Power BI preview features

## Setup

We are using the web interface to load a JSON table, you need to enable a preview feature to load JSON data as a table by going to `File` \> `Options and Settings` \> `Options` \> `Preview features`, enable `Json Table Inference`.

Either download or copy the [`data_server.nb`](https://github.com/beny74483/Power-BI-Mathemaica-integeration/blob/main/data_server.nb) code and evaluate them. Use the following function to create a JSON data source:

```mathematica
sendToPowerBI[data, headers]

```

Now you have a server redirecting any URL to your `data` as JSON with column names in `headers` on the default port (39000)

In Power BI, add new data source `Web`, type `http://localhost:39000/file.json` or `http://127.0.0.1:39000/file.json` and press `OK`:

 ![enter image description here](https://global.discourse-cdn.com/wolfram/original/3X/2/c/2cfc07fb312530e1ee2875945d7e7ab5eafe5943.png)

In the `Power Query Editor` you can preview your data, here data consists of 5 columns with different types (`Date`, `Float`, `Boolean`, `Integer`, `String`):

 ![enter image description here](https://global.discourse-cdn.com/wolfram/original/3X/2/d/2dd8a065331f1b92e5a838dab45511f280eca631.png)

Click `Close & Apply`

> Keep in mind that your data should be a 2-dimensional list in Mathematica.

# Code

[`viz_server.nb`](https://github.com/beny74483/Power-BI-Mathemaica-integeration/blob/main/viz_server.nb) file:

```
parse[text_String] := 
 Module[{output, 
   json = ImportString[Last@StringSplit[text, "\r\n"], "RawJSON"]},
  
  If[MissingQ@json["data"], json["data"] = <||>];
  
  (* remove underscore in variable names in script *)
  json["script"] = 
   StringReplace[json["script"], 
    Normal@AssociationMap[StringReplace[#, "_" -> ""] &, 
      Keys@json["data"]]];
  
  (* remove underscore in variable names *)
  json["data"] = KeyMap[StringReplace[#, "_" -> ""] &, json["data"]];
  
  (* set precision for all the real numbers *)
  json["data"] = 
   json["data"] /. 
    x_Real :> ToString@NumberForm[x, DefaultPrintPrecision -> 20];
  
  output = 
   ToExpression[
    StringRiffle[#, {"With[{", ",", "},"}, "="] &@(List @@@ 
        Normal@json["data"]) <> json["script"] <> "]"];
  
  Return[output];
  ]

server = SocketListen[38000, Function[{assoc},
    Module[{client = assoc["SourceSocket"], imgData, output, data, 
      evalData},
     
     (* print each request *)
     Print[assoc];
     
     (* evaluate script *)
     evalData = EvaluationData[data = parse@assoc["Data"];];
     
     (* check whether the evaluation was successful *)
     If[Not@evalData["Success"], 
      data = Column[{Text[Style["Error", 30]], 
         Column[Rasterize /@ 
           DeleteDuplicates@evalData["MessagesText"]]}]];
     
     imgData = 
      StringReplace[#, "\n" -> ""] &@
       ExportString[
        Rasterize[data, RasterSize -> 600], {"Base64", "PNG"}];
     
     output = "HTTP/1.1 200 OK" <>
       "\r\nServer: Mathematica/" <> ToString@$VersionNumber <>
       "\r\nContent-Type: text/image; charset=utf-8" <>
       "\r\nDate: " <> DateString[TimeZone -> 0] <> " GMT" <>
       "\r\nContent-Length: " <> ToString@StringLength@imgData <>
       "\r\nVary: Accept-Encoding\r\n\r\n" <> imgData;
     
     WriteString[client, output];
     
     ]]];

server["Socket"]

```

[`data_server.nb`](https://github.com/beny74483/Power-BI-Mathemaica-integeration/blob/main/data_server.nb) file:

```
generateJSON[data_, headers_ : {}] := 
 Module[{tHeaders, tdata, firstRow, output},
  tHeaders = headers;
  firstRow = First@data;
  
  (* convert data\datetime and Missing to javascript equivalent *)
  tdata = 
   data /. {d_DateObject :> DateString[d, "ISODateTime"], _Missing -> 
      "null"};
  
  
  (* if headers length doesnt match data length, 
  generate header names *)
  If[Length@firstRow != Length@tHeaders, 
   tHeaders = Array["C" <> ToString@# &, Length@firstRow]];
  
  output = 
   ExportString[
    AssociationThread[tHeaders[[;; Length@#]], #] & /@ tdata, "JSON", 
    "Compact" -> True];
  
  (* remove double quotes around javascript generated date type *)
  Return@StringReplace[output, "\"null\"" -> "null"]
  ]

setupJSONServer[data_, options___] := Module[{headers, port},
   
   {headers, port} = 
    Values[Association[options][[{Key["Headers"], Key["Port"]}]]];
   
   If[MissingQ@headers, headers = {}];
   If[MissingQ@port, port = 39000];
   
   (* close any existing port runned by mathematica *)
   Close@SelectFirst[Sockets[], #["DestinationPort"] == port &] // 
    Quiet;
   
   Return[SocketListen[port, Function[{assoc},
       Module[{client = assoc["SourceSocket"], answer, output},
        
        answer = generateJSON[data, headers];
        
        Print[answer];
        
        output = "HTTP/1.1 200 OK" <>
          "\r\nServer: Mathematica/" <> ToString@$VersionNumber <>
          "\r\nContent-Type: application/json" <>
          "\r\nDate: " <> DateString[TimeZone -> 0] <> " GMT" <>
          "\r\nContent-Length: " <> ToString@StringLength@answer <>
          "\r\n\r\n" <> answer;
        WriteString[client, output];
        
        ]
       ]]["Socket"]]];

```

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [April 8, 2021, 3:14pm UTC](https://community.wolfram.com/t/microsoft-power-bi-integration-with-wolfram-language-and-mathematica/17940/2 "2021-04-08T15:14:28Z")

</div>

![news-congrads-kkluyshnik-02-04-19.jpg](https://global.discourse-cdn.com/wolfram/original/3X/6/a/6a310c2d0904616faf4e6e148260c9f29ab9f4eb.jpeg) – you have earned _ **Featured Contributor Badge** _ ![2015-11-02_10-07-11.png](https://global.discourse-cdn.com/wolfram/original/3X/5/0/50e59c9ead62f729e7130d3ea1482555a75602b6.png)  
Your exceptional post has been selected for our editorial column _ **Staff Picks** _ [http://wolfr.am/StaffPicks](http://wolfr.am/StaffPicks) and [Your Profile](http://community.wolfram.com/web/beny74483) is now distinguished by a _ **Featured Contributor Badge** _ and is displayed on the [Featured Contributor Board](https://community.wolfram.com/people). Thank you!
