power-bi-modeling について
pbi-cliでPower BIのセマンティックモデル構造を管理するスキルです。テーブルや列、メジャー、リレーションシップなどの操作に加え、DAXとモデリングの実践的な指針を扱います。
主な機能
- 01テーブルと列の管理
- 02DAXメジャーの作成と編集
- 03モデルのリレーションシップ管理
- 04階層と計算グループの操作
- 05日付テーブルとモデリングの指針
活用シーン
- 01Power BIのスター スキーマ構築
- 02メジャーと計算グループの追加
- 03モデルのリレーションシップ確認と調整
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ julianobarbosa/ claude-code-skills --skill power-bi-modelingインストール後、Claude Code で/power-bi-modelingを入力して読み込まれたか確認します。
詳細
Power BI モデリングスキル
pbi-cli を使用して、セマンティックモデルの構造を管理します。pipx install pbi-cli-tool、pbi-cli skills install、および pbi connect が必要です。
前提条件
pipx install pbi-cli-tool
pbi-cli skills install
pbi connect
テーブル
pbi table list # List all tables
pbi table get Sales # Get table details
pbi table create Sales --mode Import # Create table
pbi table delete OldTable # Delete table
pbi table rename OldName NewName # Rename table
pbi table refresh Sales --type Full # Refresh table data
pbi table schema Sales # Get table schema
pbi table mark-date Calendar --date-column Date # Mark as date table
列
pbi column list --table Sales # List columns
pbi column get Amount --table Sales # Get column details
pbi column create Revenue --table Sales --data-type double --source-column Revenue # Data column
pbi column create Profit --table Sales --expression "[Revenue]-[Cost]" # Calculated
pbi column delete OldCol --table Sales # Delete column
pbi column rename OldName NewName --table Sales # Rename column
メジャー
pbi measure list # List all measures
pbi measure list --table Sales # Filter by table
pbi measure get "Total Revenue" --table Sales # Get details
pbi measure create "Total Revenue" -e "SUM(Sales[Revenue])" -t Sales # Basic
pbi measure create "Revenue $" -e "SUM(Sales[Revenue])" -t Sales --format-string "\$#,##0" # Formatted
pbi measure create "KPI" -e "..." -t Sales --folder "Key Measures" --description "Main KPI" # With metadata
pbi measure update "Total Revenue" -t Sales -e "SUMX(Sales, Sales[Qty]*Sales[Price])" # Update expression
pbi measure delete "Old Measure" -t Sales # Delete
pbi measure rename "Old" "New" -t Sales # Rename
pbi measure move "Revenue" -t Sales --to-table Finance # Move to another table
メジャー式の複数行 DAX: -e フラグは DAX をシェル引数として渡すため、改行が削除されます。SUM(Sales[Amount]) や DIVIDE([A] - [B], [B]) のような単純な式では問題ありません。VAR/RETURN を使う複雑な式の場合は、代わりに標準入力からパイプしてください。
echo 'VAR TotalSales = SUM(Sales[Amount])
VAR TotalCost = SUM(Sales[Cost])
RETURN TotalSales - TotalCost' | pbi measure create "Profit" -e - -t Sales
詳しい説明や回避策については、power-bi-dax スキルを参照してください。
リレーションシップ
pbi relationship list # List all relationships
pbi relationship get RelName # Get details
pbi relationship create \
--from-table Sales --from-column ProductKey \
--to-table Products --to-column ProductKey # Create relationship
pbi relationship delete RelName # Delete
pbi relationship find --table Sales # Find relationships for a table
pbi relationship activate RelName # Activate
pbi relationship deactivate RelName # Deactivate
階層
pbi hierarchy list --table Date # List hierarchies
pbi hierarchy get "Calendar" --table Date # Get details
pbi hierarchy create "Calendar" --table Date # Create
pbi hierarchy delete "Calendar" --table Date # Delete
計算グループ
pbi calc-group list # List calculation groups
pbi calc-group create "Time Intelligence" --description "Time calcs" # Create group
pbi calc-group items "Time Intelligence" # List items
pbi calc-group create-item "YTD" \
--group "Time Intelligence" \
--expression "CALCULATE(SELECTEDMEASURE(), DATESYTD(Calendar[Date]))" # Add item
pbi calc-group delete "Time Intelligence" # Delete group
日付/カレンダーテーブルの作成
日付テーブルは、時間インテリジェンス関数(TOTALYTD、SAMEPERIODLASTYEAR、DATEADD など)に不可欠です。
# Create a calculated date table with DAX (covers full calendar years)
pbi table create Calendar \
--dax-expression "ADDCOLUMNS(CALENDAR(DATE(2023,1,1), DATE(2024,12,31)), \"Year\", YEAR([Date]), \"MonthNumber\", MONTH([Date]), \"MonthName\", FORMAT([Date], \"MMMM\"), \"Quarter\", \"Q\" & FORMAT([Date], \"Q\"))"
# Mark it as a date table (required for time intelligence)
pbi table mark-date Calendar --date-column Date
# Verify it's recognized as a date table
pbi calendar list
ワークフロー:スター スキーマの作成
# 1. Create fact table
pbi table create Sales --mode Import
# 2. Create dimension tables
pbi table create Products --mode Import
pbi table create Calendar --mode Import
# 3. Create relationships
pbi relationship create --from-table Sales --from-column ProductKey --to-table Products --to-column ProductKey
pbi relationship create --from-table Sales --from-column DateKey --to-table Calendar --to-column DateKey
# 4. Mark date table
pbi table mark-date Calendar --date-column Date
# 5. Add measures
pbi measure create "Total Revenue" -e "SUM(Sales[Revenue])" -t Sales --format-string "\$#,##0"
pbi measure create "Total Qty" -e "SUM(Sales[Quantity])" -t Sales --format-string "#,##0"
pbi measure create "Avg Price" -e "AVERAGE(Sales[UnitPrice])" -t Sales --format-string "\$#,##0.00"
# 6. Verify
pbi table list
pbi measure list
pbi relationship list
ベストプラクティス
- 通貨(
$#,##0)、パーセンテージ(0.0%)、整数(#,##0)のメジャーには、書式文字列を使用します - ビジネス領域ごとに表示フォルダーを作成して、メジャーを整理します
- 時間インテリジェンスを使用する場合は、必ずカレンダーテーブルに
mark-dateを設定します - スクリプトでは
--jsonフラグを使用します:pbi --json measure list - バージョン管理用に TMDL をエクスポートします:
pbi database export-tmdl ./model/
注意事項
mark-dateには、欠損のない連続した日付列が必要です: カレンダーテーブルに1日でも欠けていると(うるう年や手動フィルターによる場合など)、mark-dateはエラーなく設定されますが、SAMEPERIODLASTYEAR のような時間インテリジェンス関数は診断情報なしに黙って失敗し、BLANK を返します。- 一部の pbi-cli バージョンでは、
relationship createの既定値が双方向です: 作成直後に必ずpbi --json relationship get RelNameで確認してください。ファクトテーブルと小さなディメンションテーブル間の双方向結合は、無関係なメジャーでのフィルターコンテキストの伝播を気付かれないまま壊すことがあります。 --dax-expressionを使用して作成した計算テーブルは、データ更新時に更新されません: プロセスの再計算時にのみ再評価されます。基になるテーブルを編集しても、FullまたはCalculateの更新を行うまで反映されません。部分更新のパイプラインでは見落としやすい点です。measure move --to-tableはホームテーブルを更新しますが、DAX 内の参照は更新しません: メジャーはモデル全体がスコープなので、Sales[Margin]を参照する既存のメジャーは引き続き動作します。しかし、ビジュアルのreport.jsonには古いテーブル参照が残る場合があり、後で元のテーブル名を変更すると動作しなくなることがあります。- リテラルの
$を含むformat-stringでは、シェルでのエスケープが必要です:--format-string "$#,##0"はシェルの変数展開後に#,##0になります。--format-string "\$#,##0"を使うか、単一引用符で囲んでください。エラーが出ないまま、メジャーの表示が「誤った」ものになります。 - 計算グループの作成順序は並べ替えに影響します: 最初に作成したアイテムの序数は 0 となり、既定値になります。最初に「No Time Calc」のプレースホルダーを追加しないと、何も選択していないときにメジャーへ YTD が黙って適用されます。
コメント
Markdown 対応