Category Archives: SQL RS

SSMS 連結 Power BI Report Server 可能要用 Web Services URL

不知哪個版本後的 SSMS 需要如此,我手中的是 17.4

image

透過 OData feed 以 Reporting Services Shared Dataset 提供資料給 PowerBI

https://docs.microsoft.com/en-us/power-bi/report-server/access-dataset-odata

REST API 的定義:

https://msdn.microsoft.com/en-us/library/mt704264(v=sql.105).aspx

REST API 的格式約略如下:

http://localhost/ReportS/api/v2.0/datasets(479DDB25-5D68-4EE3-A2E0-10E22C525BA7)/data

image

若要查 dataset 的 ID,可以透過

select Name,ItemID from ReportServer.dbo.Catalog
where type=8

選取資料源為 OData feed

image

image

取得紀錄

image

點選 More Columns 取得其他欄位

image

點選設定,去掉 Default column name prefix

image

image

企業內透過 RS 整合 Power BI 的解決方案可用了

2017年 11/1 的更新可能標誌著 Power BI 可以用在企業內 Solution 了,幾個期待已久的功能都做進來

以下是相關的連結:

https://powerbi.microsoft.com/en-us/blog/new-version-of-power-bi-report-server-now-available/

使用 Power BI 報表伺服器的內部部署報表 https://powerbi.microsoft.com/zh-tw/report-server/

image

Microsoft Power BI Desktop (最佳化後十分適合搭配 Power BI 報表伺服器 – 2017 年 10 月 使用) https://www.microsoft.com/zh-tw/download/details.aspx?id=56136

除了以往 Power BI 友善的互動外,這一版新增且企業非常需要的功能:

鑽研篩選

多種 Data Source

  • 非侷限 AS
  • 以 OData  吃 RS 的 Shared DataSet
  • 排程更新 Data Source

image

以 REST API 跟 RS 溝通

透過 URL 內嵌 PBI 時,可以帶過濾條件,例如:https://reportserver/reports/powerbi/Store Sales?rs:Embed=true&filter= Store/Territory eq ‘NC’ and Store/Chain eq ‘Fashions Direct’

 

 

 

 

另外,註記一下,一台機器現在偷偷跑起來的 AS 服務真多

image

從上到下,透過使用者名稱可以得知分別為獨立的 AS、Power BI Desktop 使用的 AS 和 RS 使用的 AS,後兩者是 Power BI Desktop 和 RS 利用來建立與解釋 Model,取得與更新資料用。

突然間 Power BI Report Server 面市了

感謝 Justin Li 今天告知

https://powerbi.microsoft.com/zh-tw/report-server/

image

image

image

image

RS 的過濾條件用 OR

但就介面只能設定 AND,可以透過 VB.NET 設定 OR

image

運算式以 VB.NET 定義 Or 條件

image

值要求 true

image

透過 RS 呈現 R 的圖形

參考:https://www.mssqltips.com/sqlservertip/4127/sql-server-2016-r-services-display-r-plots-in-reporting-services/

我寫的範例報表 ShowRImage.rdl

透過 T-SQL 呼叫 R Script 可以直接傳回圖檔的 binary 結果,就可以透過影像物件呈現

以如下的 R-Script 為例

EXEC   sp_execute_external_script
      @language = N’R’
     ,@script = N’    df <- inputDataSet; #read input data
                image_file = tempfile(); #create a temporary file
                jpeg(filename = image_file, width=500, height=500); #create a JPEG graphic device
                hist(df$Ages); #plot the histogram
                dev.off(); #dev.off returns the number and name of the new active device (after the specified device has been shut down). (device = graphical device)
                #file() opens a file, in this case the image. rb = read binary
                #readBin() reads binary data. what = described the mode of the data. In this case, it"s raw data. n = maximum number of records to read.
                #data.frame converts the data to a data frame, which is required as output by SQL Server. The result is written to the OutputDataset variable.
                OutputDataset <- data.frame(data=readBin(file(image_file,"rb"),what=raw(),n=1e6));
                    ‘
    ,@input_data_1 = N’SELECT Ages = DATEDIFF(YEAR,[BirthDate],GETDATE())
                        FROM [AdventureWorksDW].[dbo].[DimCustomer];’
    ,@input_data_1_name = N’inputDataSet’
    ,@output_data_1_name = N’OutputDataset’
WITH RESULT SETS ((plot varbinary(max)));

從 SSMS 的執行結果可以取到 varbinary(max) 的結果:

image

在 Report Builder 就以這段 T-SQL 當作資料集

image

在報表中加入影像物件,設定來源就是資料庫傳回:

image

透過 Report Builder 和 Report Server 都可以正常執行:

image

image

RS 訂閱報表採用 Windows 檔案共用時,輸出的檔案名稱包含日期

感謝聽課的朋友提醒,在檔案名稱欄位加上 @Timestamp

image

輸出如下的檔案

image

在 SharePoint 上透過 Power View 存取 AS Tabular Model 時要換身分

感謝微軟 Ray 告知的解法,在 SharePoinrt 上要用 Report Data Source(Data Source Type 選擇 Microsoft BI Semantic Model for Power View) 而非 BI Semantic Model Connection

image

詳細作法參考以下網址:

http://whitepages.unlimitedviz.com/2012/04/fixing-access-errors-when-creating-a-bism-connection-for-powerview/

讓 RS 可以存取 AS 的 Tabular Model

參考以下的網址

https://www.simple-talk.com/sql/database-administration/using-dax-to-create-ssrs-reports-the-basics/

補充一點,若要設定 DAX,也可以直接點 Expression 按鈕,而不必如文中叫起 Query Designer

image

image

我的 Sample Code

RS 呼叫自訂的組件,產生背景圖

範例程式:CustomAssemblyGenerateBackgroundImage

聽課的朋友需要 Report 的報表自動帶某些文字的浮水印,所以我建立一個 .NET 自訂組件如下

簡單建立輸入文字後產生 Bitmap 圖的 byte array 函數

using System;
using System.Text;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
namespace GenerateImages
{
    public class Class1
    {
        public static byte[] CreateBitmapImage(string sImageText)
        {
            Bitmap objBmpImage = new Bitmap(1, 1);

            int intWidth = 0;
            int intHeight = 0;

            Font objFont = new Font("Arial", 40, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
            Graphics objGraphics = Graphics.FromImage(objBmpImage);
            intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
            intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
            objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
            objGraphics = Graphics.FromImage(objBmpImage);
            objGraphics.Clear(Color.White);
            objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
            objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
            objGraphics.Flush();

            //傳回 Bitmap 物件 RS 不能用,需要 byte array
            //objBmpImage.Save(@"C:\inetpub\images\test.bmp");
            //return (objBmpImage);
            MemoryStream ms = new MemoryStream();
            objBmpImage.Save(ms,ImageFormat.Bmp);
            Byte[] bt=ms.ToArray();
            return (bt);
        }

    }
}

透過 gacutil 註冊

image

並修改 在 C:\Program Files\Microsoft SQL Server\MSRS12.MSSQLSERVER\Reporting Services\ReportServer\rssrvpolicy.config 設定可信賴的 Assembly

<CodeGroup
               class="UnionCodeGroup"
               version="1"
               PermissionSetName="FullTrust"
               Name="GenerateImages.Class1"
               Description="產生 image">
                          <IMembershipCondition
                               class="UrlMembershipCondition"
                               version="1"
                               Url="C:\Program Files\Microsoft SQL Server\MSRS12.MSSQLSERVER\Reporting Services\ReportServer\bin\GenerateImages.dll" />

</CodeGroup>

RS 2014 似乎只能參照 NET Framework 2.0

image

報表背景呼叫組件

image

透過參數設定背景要產生的文字

image