1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Fabric のセマンティックリンクでセマンティックモデルのメタデータ(プロパティ)をノートブックから編集する

1
Last updated at Posted at 2026-03-31

はじめに

Microsoft Fabric のセマンティック リンクを使うと、ノートブックからセマンティックモデルのメタデータを操作できます。
この記事では、テーブル名 / テーブルの説明 / 列名 / 列の説明/ 型変換 といったプロパティを、ノートブックから編集する方法を紹介します。

▽セマンティックモデルのプロパティ(テーブルの名前とその説明)
image.png

こうしたプロパティは UI から手作業で整備することもできますが、ノートブックを使えば、ルールに沿った一括更新や定型的なメンテナンスを行いやすくなります。
セマンティックモデルのメタデータ整備を効率化したい場面では、特に有効です。

また、セマンティックモデルのプロパティを充実させておくことで、Data Agent がそれらの情報を参照しやすくなり、回答精度の向上も期待できます。

セマンティックリンクを使用するための事前準備

まずはFabricでノートブックを新規作成してください。

その後以下を実行します。

%pip install semantic-link-labs
import sempy_labs as labs
from sempy_labs.tom import connect_semantic_model

その後、ワークスペースIDの設定と編集したいセマンティックモデルの名前を定義しておきます。

# ワークスペース名とセマンティックモデル名を記載する

workspace = '<your-workspace-id>'
dataset = '<your-semanticmodel-name>'

現状の確認

セマンティックモデルに接続して、すべてのテーブル名を取得

# セマンティックモデルに接続して、すべてのテーブル名を取得

with connect_semantic_model(dataset=dataset, readonly=True, workspace=workspace) as tom:
    for t in tom.model.Tables:
        print(t.Name)

セマンティックモデルに接続して、すべてのテーブル名・列名を取得

# 列名もとりたいならこっち

with connect_semantic_model(dataset=dataset, readonly=True, workspace=workspace) as tom:
    for t in tom.model.Tables:
        print(t.Name)
        for c in t.Columns:
            print(c.Name)
        print('----------------------------')
        

特定のテーブルの説明欄を確認

# テーブルのディスクリプションを見る

with connect_semantic_model(dataset=dataset, readonly=True, workspace=workspace) as tom:
    for t in tom.model.Tables:
        if t.Name == "<your-table>":
            print(f'Table: {t.Name}')
            print(f'Description: {t.Description}')
            break

特定のテーブルとその列の説明欄を確認

# テーブルと各列のディスクリプションを見る

with connect_semantic_model(dataset=dataset, readonly=True, workspace=workspace) as tom:
    for t in tom.model.Tables:
        if t.Name == "<your-table>":
            print(f"Table: {t.Name}")
            print(f"Table Description: {t.Description}")
            print("-" * 40)
            for c in t.Columns:
                print(f"Column: {c.Name}")
                print(f"Description: {c.Description}")
                print("-" * 20)
            break

セマンティックリンクを使ってプロパティを編集

テーブル名の変更(変更内容は一例)


# テーブルの名前を編集(_をスペースに一斉変換)
with connect_semantic_model(dataset=dataset, readonly=False, workspace=workspace) as tom:
    for t in tom.model.Tables:
        t.Name = t.Name.replace('_', ' ')
        

列名の変更(変更内容は一例)


# 列の名前も編集したいならこっち(_をスペースに一斉変換)
with connect_semantic_model(dataset=dataset, readonly=False, workspace=workspace) as tom:
    for c in tom.all_columns():
        c.Name = c.Name.replace('_', ' ')
        

テーブルの説明欄への書き込み


# テーブルの説明欄にこのノートブックから書き込む

from sempy_labs.tom import connect_semantic_model

target_table_name = "<your-table>"
new_description = "セマンテックリンクからの書き込み"

with connect_semantic_model(dataset=dataset, readonly=False, workspace=workspace) as tom:
    for t in tom.model.Tables:
        if t.Name == target_table_name:
            print(f'変更前: {t.Name} / Description = {t.Description}')
            t.Description = new_description
            print(f'変更後: {t.Name} / Description = {t.Description}')

列の説明欄への書き込み


target_table_name = "<your-table>"

target_column_name = "<column-name>"

new_description = "セマンテックリンクからの書き込み"

with connect_semantic_model(dataset=dataset, readonly=False, workspace=workspace) as tom:
    for t in tom.model.Tables:
        if t.Name == target_table_name:
            for c in t.Columns:
                if c.Name == target_column_name:
                    print(f'変更前: {c.Name} / Description = {c.Description}')
                    c.Description = new_description
                    print(f'変更後: {c.Name} / Description = {c.Description}')
                    break

型の変換(書式設定の変更)


# 型変換(書式設定の変更):整数型→テキスト型

from Microsoft.AnalysisServices.Tabular import DataType

target_table_name = "<your-table>"
target_column_name = "<column-name>"

with connect_semantic_model(dataset=dataset, readonly=False, workspace=workspace) as tom:
    for t in tom.model.Tables:
        if t.Name == target_table_name:
            for c in t.Columns:
                if c.Name == target_column_name:
                    print(f"変更前: {c.Name} / DataType = {c.DataType}")

                    c.DataType = DataType.String

                    print(f"変更後: {c.Name} / DataType = {c.DataType}")
                    tom.model.SaveChanges()
                    break

参考メモ

セマンティック リンクは、Fabric のデータサイエンスの機能です。データサイエンティスト向けです。
なので、今回紹介したような テーブル名・列名・説明の更新 に加えて、リレーションやメジャーの作成・編集 が行えます。

一方で、セマンティック リンクはセマンティックモデル上のデータそのものを直接書き換えるための機能ではありません
実データの編集は、元データが存在する Lakehouse や Warehouse など、ソース側で行う前提になります。(こっちの作業はデータエンジニア寄り)

そのため、セマンティック リンクは「データの中身を更新する機能」というより、セマンティックモデルの構造やメタデータを扱うための機能として理解すると分かりやすいと思います。

参考記事

Youtubeもやってます!

FabricやDatabricksについて学べる勉強会を毎月開催!

次回イベント欄から直近のMicrosoft Data Analytics Day(Online) 勉強会ページへ移動後、申し込み可能です!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?