Power BI workout - Rebind Report In Group

Power BI レポートのコピー操作ではない。Power BI レポートにバインドされているデータセットを再バインドする。
たとえば、サブセットのデータセットで作成したレポートをPower BI サービス 上ででの操作(Power BI REST API)で本来のデータセットにバインドする。

# Power BI Cmdlets for Windows PowerShell and PowerShell Core
- [Power BI Cmdlets for Windows PowerShell and PowerShell Core | Docs](https://docs.microsoft.com/en-us/powershell/power-bi/overview)
# Power BI REST API
- [Power BI REST APIs for embedded analytics and automation - Power BI REST API | Microsoft Docs](https://docs.microsoft.com/en-US/rest/api/power-bi/)

## Reports - Rebind Report In Group
- [Reports - Rebind Report In Group - REST API (Power BI Power BI REST APIs) | Microsoft Docs](https://docs.microsoft.com/en-us/rest/api/power-bi/reports/rebind-report-in-group)

Rebinds the specified report from the specified workspace to the specified dataset.

If the specified dataset resides in a different new workspace experience workspace from the report or in My workspace, then a shared dataset will be created in the report's workspace.
On rebind, reports with a live connection will lose that connection and instead have a direct binding to the target dataset.

### Permissions
- Write permission on the specified report.
- Build permission on the target dataset.

ワークスペースの Member ロールで充分
### Required Scope
Report.ReadWrite.All

```
POST https://api.powerbi.com/v1.0/myorg/groups/{groupId}/reports/{reportId}/Rebind
```
### URI Parameters
|Name|In|Required|Type|Description|
|:--|:--|:--|:--|:--|
|groupId|path|True|string uuid|The workspace ID|
|reportId|path|True|string uuid|The report ID|
### Request Body
|Name|Required|Type|Description|
|:--|:--|:--|:--|
|datasetId|True|string|The new dataset for the rebound report. If the dataset resides in a different workspace than the report, a shared dataset will be created in the report's workspace.|
### Responses
|Name|Type|Description|
|:--|:--|:--|
|200 OK||OK|
## DEMO | Rebind Power BI report
動作を確認する程度で
DEMO2 (Power BI ワークスペース)に配置された Report2 (Power BI レポート)を Dataset1 (Power BI データセット)にバインドする。
### Define
```powershell
# Report in workspace
$WorkspaceName_report = "DEMO2"
$ReportName = "Report2"

# Dataset in workspace
$WorkspaceName_dataset = $null
$DatasetName = "Dataset1"
```
### Login Power BI
- [Connect-PowerBIServiceAccount (MicrosoftPowerBIMgmt.Profile) | Microsoft Docs](https://docs.microsoft.com/en-us/powershell/module/microsoftpowerbimgmt.profile/connect-powerbiserviceaccount)
```powershell
Login-PowerBI | Out-Null
```
### Get Power BI workspace | Get Power BI report | Get Power BI dataset
- [Get-PowerBIWorkspace (MicrosoftPowerBIMgmt.Workspaces) | Microsoft Docs](https://docs.microsoft.com/en-us/powershell/module/microsoftpowerbimgmt.workspaces/get-powerbiworkspace)
- [Get-PowerBIReport (MicrosoftPowerBIMgmt.Reports) | Microsoft Docs](https://docs.microsoft.com/en-us/powershell/module/microsoftpowerbimgmt.reports/get-powerbireport)
- [Get-PowerBIDataset (MicrosoftPowerBIMgmt.Data) | Microsoft Docs](https://docs.microsoft.com/en-us/powershell/module/microsoftpowerbimgmt.data/get-powerbidataset)
```powershell
$Workspace = Get-PowerBIWorkspace -Name $WorkspaceName_report
$Report = Get-PowerBIReport -Workspace $Workspace -Name $ReportName

if (
    $WorkspaceName_dataset -eq $WorkspaceName_report -or 
        $null -eq $WorkspaceName_dataset
) {
    $Dataset = Get-PowerBIDataset -Workspace $Workspace
        | Where-Object Name -EQ $DatasetName
} else {
    $Dataset = Get-PowerBIDataset `
        -Workspace (Get-PowerBIWorkspace -Name $WorkspaceName_dataset)
        | Where-Object Name -EQ $DatasetName
}
```
### Invoke Power BI REST API
- [Invoke-PowerBIRestMethod (MicrosoftPowerBIMgmt.Profile) | Microsoft Docs](https://docs.microsoft.com/en-us/powershell/module/microsoftpowerbimgmt.profile/invoke-powerbirestmethod)
- [Reports - Rebind Report In Group - REST API (Power BI Power BI REST APIs) | Microsoft Docs](https://docs.microsoft.com/en-us/rest/api/power-bi/reports/rebind-report-in-group)
```powershell
$Url = "groups/$($Workspace.Id)/reports/$($Report.Id)/rebind"
$Body = @"
{"datasetId": "$($Dataset.Id)"}
"@

Invoke-PowerBIRestMethod -Method Post -Url $Url -Body $Body
```
### Logout Power BI
- [Disconnect-PowerBIServiceAccount (MicrosoftPowerBIMgmt.Profile) | Microsoft Docs](https://docs.microsoft.com/en-us/powershell/module/microsoftpowerbimgmt.profile/disconnect-powerbiserviceaccount)
```powershell
Logout-PowerBI
```

# 思ったこと🙄
Power BI コマンドレットを使うのは サービスへのログインが簡単だし、用意されていないコマンドレットは Invoke-PowerBIRestMethod で対応できるから。完全な自動化が必要がないのであればスクリプト用意して適宜で使えばいいよね。

# その他
https://powerbi.connpass.com/

- [Power BI 勉強会 - connpass](https://powerbi.connpass.com/)
- [Power BI Desktop を使い始めるには](http://qiita.com/PowerBIxyz/items/6c4735cb5b5712e70981)
- [Excel で Power Query を使い始めるには](http://qiita.com/PowerBIxyz/items/52e563037b855a099ca9)