<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/sql-server-power-bi-and-r/"></div></div><div><a data-url="http://www.r-bloggers.com/sql-server-power-bi-and-r/" data-counturl="http://www.r-bloggers.com/sql-server-power-bi-and-r/" data-count="horizontal" data-text="SQL Server, Power BI, and R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2016/06/sql-server-power-bi-and-r.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>by Sheri Gilley, Microsoft Senior Software Engineer</p>
<p>SQL Server 2016 has reached general availability recently and one of the top new capabilities it features is<a target="_blank" href="https://www.microsoft.com/en-us/cloud-platform/sql-server-r-services"> SQL Server R Services </a>—  advanced analytics in-database with the R language.   R has also been <a title="R in PowerBI" target="_blank" href="https://powerbi.microsoft.com/en-us/blog/r-visuals-in-powerbi-service/">integrated into Power BI</a>, allowing you to create fully integrated visualizations with the power of the R language.  In this blog post I will show an example using R in SQL Server to create a model and  batch score testing data, then use Power BI Desktop to create visualizations of the scored data.</p>
<p>Rather than moving your data from the database to an external machine running R,  you can now run R scripts directly inside the SQL Server database – train your model inside the database with the full power of CRAN R packages plus the additional features of speed and scalability from the RevoScaleR package.  And once you have a model, you can perform batch scoring inside the database as well.</p>
<p>But what do you do when it comes to visualizing this newly scored data?  How would you create an interactive experience that supports sharing and embedding in applications? One great option is using PowerBi which gives you a quick and easy way to create visualizations that interact to help you explore your data.   It also easily allows you to share your dashboards and reports, and you can embed the PowerBI visuals in your applications. And PowerBI can access your SQL data.   </p>
<p>Here is an <a title="Fraud Detection Template" target="_blank" href="https://gallery.cortanaintelligence.com/Tutorial/Online-Fraud-Detection-Template-with-SQL-Server-R-Services-1?share=1">example of model building and scoring for Fraud Detection </a>performed in SQL Server.  The data contains 200,000 online transactions, with a second spreadsheet containing  information for 9000 known fraud transactions.  <a title="Fraud Detection Template on Github" target="_blank" href="https://github.com/Microsoft/SQL-Server-R-Services-Samples/tree/master/FraudDetection/SQLR">This template </a> details the steps to prepare data, create a model and perform batch scoring.  The template includes R code inside stored procedures that allows you to train and save the model and score test data using the model. </p>
<p>While this example covers a fairly small dataset, R Services can scale to billions of rows – learn more about performance tuning for these operations <a title="Performance Tuning for SQL Server" target="_blank" href="https://msdn.microsoft.com/en-us/library/mt723573.aspx?f=255&MSPPError=-2147217396">here</a>.</p>
<p>Once the scoring is complete, you can access this data in either PowerBI or PowerBI Desktop to examine the scores. In PowerBI Desktop, use Get Data>SQL Server Database, and you can connect directly to a database table or can add a query to access the data you wish to visualize.</p>
<p><a target="_blank" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d1ffaf06970c-pi"><img title="Query" alt="Query" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d1ffaf06970c-800wi" border="0"></a></p>
<p>In this example the scored data is saved in the table sql_predict_score with just the account Id and the predicted scores, so we will add a query to join this with the rest of the input data.</p>
<p>This particular scoring was performed on test data so we also have the known values available to help us see how well the model has performed. We'll use these values in a few different displays to learn more about the scoring of the test data.</p>
<p><a target="_blank" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb091950aa970d-pi"><img title="Scores" alt="Scores" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb091950aa970d-800wi" border="0"></a></p>
<p>Note the first two bar charts – the first shows the known values, the second shows the predicted values. Since we are predicting a rare event (in our sample, only 372 of the 58,924 transaction) it would be difficult to view counts on the bar heights. So instead, the bar heights here represent the average transaction amount, an important attribute to track when dealing with fraud. Right away we see that the average transactions are slightly higher in the fraud transactions than the non-fraud.</p>
<p><a target="_blank" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c875e09a970b-pi"><img title="Bars" alt="Bars" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b7c875e09a970b-800wi" border="0"></a></p>
<p>While we can see some interesting information in this display, the real power (and fun) of Power BI is in the interactions. When you click on a bar in the graph, the rest of the display is filtered by that selection. For example, click on the first bar to display only the transactions predicted as fraud in all the other displays:</p>
<p><a target="_blank" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d1ffaf7b970c-pi"><img title="Predfraud" alt="Predfraud" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01b8d1ffaf7b970c-800wi" border="0"></a></p>
<p>Likewise, click on the first bar of the second graph to view the displays for the 372 known fraud transactions.</p>
<p><a target="_blank" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb091950da970d-pi"><img title="Fraud" alt="Fraud" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb091950da970d-800wi" border="0"></a></p>
<p>In the first barchart the light bars show all transactions, while the darker bars in the middle now show just the fraud transactions. Note from the table that the miss-classified non-fraud transactions represent just 17 cases, while 355 cases were correctly classified In both instances, the average amount for fraud transactions are higher than the average transaction amounts for all data.</p>
<p>In Power BI Desktop, you can also create visualizations with R directly in the report – in this example I've used R to create both the Box &amp; Whisker plot and the Density Plot using trellis. For example:</p>
<p><a target="_blank" href="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb0919510d970d-pi"><img title="Rcode" alt="Rcode" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b01bb0919510d970d-800wi" border="0"></a></p>
<p>The integration of R into SQL Server 2016 and Power BI makes it easy for you to create and share rich visualizations with advanced insights and predictions for your business use-case. </p>
<p><em>Editors Note: The Power BI Service now also supports <a target="_blank" href="https://powerbi.microsoft.com/en-us/blog/r-visuals-in-powerbi-service/">reports and dashboards with R visuals</a>.</em></p>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/sql-server-power-bi-and-r/"></div></div><div><a data-url="http://www.r-bloggers.com/sql-server-power-bi-and-r/" data-counturl="http://www.r-bloggers.com/sql-server-power-bi-and-r/" data-count="vertical" data-text="SQL Server, Power BI, and R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2016/06/sql-server-power-bi-and-r.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ECfj5InAzl0"></a></div>