如何提取Power BI仪表板所有数据源列表?求Python脚本方案
获取Power BI仪表板所有表格及对应数据源清单的方法
下面是几种可行的方案,涵盖API调用、Python脚本和可视化工具:
1. Power BI REST API + Python脚本
这是通用方案,适用于大多数Power BI工作区(需具备Dataset.Read.All权限)。
步骤:
- 在Azure AD注册应用,获取租户ID、客户端ID、客户端密钥,并授予
Dataset.Read.All权限。 - 用Python调用API获取数据集元数据,关联表格与数据源。
示例脚本:
import requests import json # 替换为你的实际配置 tenant_id = "你的租户ID" client_id = "你的客户端ID" client_secret = "你的客户端密钥" workspace_id = "目标工作区ID" # 获取访问令牌 token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token" token_payload = { "grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret, "scope": "https://analysis.windows.net/powerbi/api/.default" } token_response = requests.post(token_url, data=token_payload) access_token = token_response.json()["access_token"] headers = {"Authorization": f"Bearer {access_token}"} # 获取工作区内所有数据集 datasets_url = f"https://api.powerbi.com/v1.0/myorg/groups/{workspace_id}/datasets" datasets = requests.get(datasets_url, headers=headers).json()["value"] # 遍历数据集提取关联信息 for dataset in datasets: dataset_id = dataset["id"] print(f"\n=== 数据集: {dataset['name']} ===") # 获取数据源映射(ID -> 名称) datasources_url = f"https://api.powerbi.com/v1.0/myorg/groups/{workspace_id}/datasets/{dataset_id}/datasources" datasources = {ds["id"]: ds["name"] for ds in requests.get(datasources_url, headers=headers).json()["value"]} # 获取所有表格及关联数据源 tables_url = f"https://api.powerbi.com/v1.0/myorg/groups/{workspace_id}/datasets/{dataset_id}/tables" tables = requests.get(tables_url, headers=headers).json()["value"] print("表格-数据源清单:") for table in tables: source_info = table.get("source", {}) datasource_name = datasources.get(source_info.get("datasourceId"), "未知数据源") print(f"- 表格名称: {table['name']} | 数据源: {datasource_name}")
2. XMLA端点 + pyadomd查询(仅Premium工作区)
如果你的工作区使用Power BI Premium容量,可直接通过XMLA端点查询模型元数据。
示例脚本:
from pyadomd import Pyadomd # 替换为你的工作区和数据集名称 connection_string = "Provider=MSOLAP;Data Source=powerbi://api.powerbi.com/v1.0/myorg/你的工作区名称;Initial Catalog=你的数据集名称;" with Pyadomd(connection_string) as conn: with conn.cursor() as cur: # 执行MDX查询获取关联数据 cur.execute(""" SELECT TABLE_NAME, DATASOURCE_NAME FROM $SYSTEM.TMSCHEMA_TABLES JOIN $SYSTEM.TMSCHEMA_DATASOURCES ON TABLES.DATASOURCE_ID = DATASOURCES.ID """) results = cur.fetchall() print("\n表格-数据源清单:") for table_name, ds_name in results: print(f"- 表格名称: {table_name} | 数据源: {ds_name}")
3. 可视化工具:Tabular Editor(无需编码)
- 下载安装Tabular Editor免费社区版
- 打开后选择
File->Connect->Power BI Dataset,登录后选择目标数据集 - 在左侧导航栏分别展开
Tables和Datasources,右键点击Tables选择Export->Export to CSV,同时导出Datasources列表,最后通过数据源ID关联两个列表即可生成完整清单
内容的提问来源于stack exchange,提问作者Daniel Rocamora Betanzos
相关产品推荐
相关产品推荐

