Currently, Power BI doesn’t include a connector to connect directly to Azure Machine Learning. This leaves you with two integration options:
Instead of publishing to a web service, save the predictive results to Azure Storage, such as to an Azure SQL Database. Then use Power BI Service’s Get Data to visualize the predictive results.
Publish the predictive model as a web service, and then use Power BI Desktop or Power Query to call the web service.
The second option allows you to implement flexible integration scenarios, such as to call the web service for each customer from a dataset you imported in Power BI Desktop.
Moreover, it demonstrates some of the advanced capabilities of Power BI Desktop queries and Power Query. This is the integration option I’ll demonstrate next.
Figure 10.20 The New Customers Excel file has a list of customers that need to be scored.
Understanding the input dataset
Let’s get back to our Adventure Works scenario. Now that the predictive web service is ready, you can use it to predict the probability of new customers who could purchase a bike, provided that you have the customer demographics details. The marketing
department has given you an Excel file with potential customers which they might have downloaded from the company’s CRM system.
1.In Excel, open the New Customers.xlsx file, which includes 20 new customers (see Figure 10.20).
2.Note that the last column BikeBuyer is always zero. That’s because at this point you don’t know if the customer is a potential bike buyer.
The predictive web service will calculate the probability for each customer to purchase a bike. This requires calling the web service for each row in the input dataset by sending a predictive query (a data mining query that predicts a single case is called a singleton query). To see the final solution, open the Predict Buyers.pbix file in Power BI Desktop.
Creating a query function
You already know from Chapter 6 how to use query functions. Similar to other
programming languages, a query function encapsulates common logic so that it can be called repeatedly. Next, you’ll create a query function that will invoke the Adventure Works predictive web service:
1. In Power BI Desktop, click Edit Queries to open the Query Editor.
1.In Query Editor, expand the New Source button and click Blank Query. Then in the
ribbon’s View tab, click Advanced Editor.
2.Copy the function code from the \source\ch10\fnPredictBuyer.txt file and paste in the query. The query function code follows:
1. let
2. PredictBikeBuyer = (Gender, YearlyIncome, TotalChildren, NumberChildrenAtHome, EnglishEducation, EnglishOccupation, HouseOwnerFlag, NumberCarsOwned, CommuteDistance, Region, Age, BikeBuyer) =>
3. let
4. //replace service Uri and serviceKey with your own settings
5. serviceUri=“https://ussouthcentral.services.azureml.net/workspaces/…/execute?api- version=2.0&details=true”,
6. serviceKey=”<your service key>
7. RequestBody = “ 8. {
9. ““Inputs””: { 10. ““input1””: {
11. ““ColumnNames””: [
12. ““Gender””, ““YearlyIncome””, ““TotalChildren””, ““NumberChildrenAtHome””, ““EnglishEducation””,
““EnglishOccupation””,
13. ““HouseOwnerFlag””, ““NumberCarsOwned””, ““CommuteDistance””, ““Region””, ““Age””,
““BikeBuyer””
14. ],
15. ““Values””: [ 16. [
17. ”””&Gender&”””, ”””&Text.From(YearlyIncome)&”””, ”””&Text.From(TotalChildren)&”””,
”””&Text.From(NumberChildrenAtHome)&”””, ”””&EnglishEducation&”””, ”””&EnglishOccupation&”””,
”””&Text.From(HouseOwnerFlag)&”””, ”””&Text.From(NumberCarsOwned)&”””,
”””&Text.From(CommuteDistance)&”””,
18. ”””&Region&”””, ”””&Text.From(Age)&”””, ”””&Text.From(BikeBuyer)&”””
19. ]]}},
20. ““GlobalParameters””: {}
21. } 22. “,
23. Source=Web.Contents(serviceUri, 24. [Content=Text.ToBinary(RequestBody),
25. Headers=[Authorization=“Bearer “&serviceKey,#“Content-Type”=“application/json; charset=utf-8”]]), 26. #“Response” = Json.Document(Source),
27. #“Results” = Record.ToTable(#“Response”) 28. in
29. #“Results”
30. in
31. PredictBikeBuyer
The code in line 2 defines a PredictBikeBuyer function with 12 arguments that correspond to the columns in the input dataset. Remember to update lines 5 and 6 with your web
service URI and service key. Then the code creates a request payload described in JSON, as per the web service specification (see Figure 10.19 again). Notice that the code obtains the actual values from the function arguments. Lines 23-25 invoke the web service. Line 26 reads the response, which is also described in JSON. Line 27 converts the response to a table.
3.Rename the query to fnPredictBuyer and save it.
Creating a query
Now that you have the query function, let’s create a query that will load the input dataset from the New Customers Excel file, and then call the function for each customer:
1. In Query Editor, expand New Source again and then click Excel.
1.Navigate to New Customers.xlsx and load Table1. Table1 is the Excel table that has the new customers.
2.Rename the query to PredictedBuyers.
3.Add a custom column called PredictedScore, as shown in Figure 10.21. This column calls the fnPredictBuyer function and passes the customer demographic details from each row in the input dataset.
Figure 10.21 The PredictedScore custom column calls the fnPredictBuyer function.
4.The results of the custom column will be a table, which includes other nested tables, as per the JSON response specification (see Figure 10.22). You need to click the “expand”
button four times until you see “List” showing in the PredictedScore column.
Figure 10.22 Expand the PredictedScore column four times until you get to the list of values.
5.Once you get to the list, add another custom column to get the last value of the list.
Because you have 12 input columns, the last value will be always at position 13. This value represents the score:
[PredictedScore.Value.output1.value.Values]{0}{13}
6.The final steps are to rename the column, change the column type to Decimal Number, and remove any errors.
7.Create a table report that shows the customer e-mail and the probability for each customer to purchase a bike, as shown in Figure 10.23.
Figure 10.23 A table report that shows the predicted results.