使用语义链接发现语义模型中的关系

本教程演示如何使用 Jupyter 笔记本与 Power BI 交互,并检测表与 SemPy 库之间的关系。

本教程介绍如何:

  • 使用语义链接的 Python 库(SemPy)发现语义模型中的关系(Power BI 数据集)。
  • 使用与 Power BI 集成的 SemPy 组件并自动执行数据质量分析。 这些组件包括:
    • FabricDataFrame - 使用语义信息增强的类似 pandas 的结构
    • 将语义模型从 Fabric 工作区拉取到笔记本中的函数
    • 测试函数依赖项并识别语义模型中的关系冲突的函数

先决条件

  • 转到导航窗格中的 工作区 ,然后选择工作区将其设置为当前工作区。

  • 构造示例 GitHub 存储库下载客户盈利示例.pbix客户盈利率示例(auto.pbix 语义模型),然后将其上传到工作区。

在笔记本中继续操作

使用 powerbi_relationships_tutorial.ipynb 笔记本继续作。

设置笔记本

使用所需的模块和数据设置笔记本环境。

  1. semantic-link使用%pip笔记本中的内联命令从 PyPI 安装包。

    %pip install semantic-link
    
  2. sempy导入稍后将使用的模块。

    import sempy.fabric as fabric
    
    from sempy.relationships import plot_relationship_metadata
    from sempy.relationships import find_relationships
    from sempy.fabric import list_relationship_violations
    
  3. pandas导入库并设置输出格式的显示选项。

    import pandas as pd
    pd.set_option('display.max_colwidth', None)
    

## Explore semantic models

This tutorial uses the Customer Profitability Sample semantic model [_Customer Profitability Sample.pbix_](https://github.com/microsoft/fabric-samples/blob/main/docs-samples/data-science/datasets/Customer%20Profitability%20Sample.pbix). Learn about the semantic model in [Customer Profitability sample for Power BI](/power-bi/create-reports/sample-customer-profitability).

- Use SemPy's `list_datasets` function to explore semantic models in your current workspace:

  ```python
  fabric.list_datasets()

对于此笔记本的其余部分,请使用两个版本的客户盈利率示例语义模型:

  • 客户盈利率示例:Power BI 示例中提供的语义模型,具有预定义的表关系
  • 客户盈利率示例(自动):相同的数据,但关系仅限于 Power BI 自动检测

从示例语义模型中提取预定义关系

  1. 使用 SemPy 的函数加载客户盈利率示例语义模型中的 list_relationships 预定义关系。 该函数列出表格对象模型(TOM)中的关系。

    dataset = "Customer Profitability Sample"
    relationships = fabric.list_relationships(dataset)
    relationships
    
  2. relationships使用 SemPy 的plot_relationship_metadata函数将 DataFrame 可视化为图形。

    plot_relationship_metadata(relationships)
    

    语义模型中表之间的关系图的屏幕截图。

    此图显示此语义模型中表之间的关系,如主题专家在 Power BI 中定义。

发现其他关系

如果从 Power BI 自动检测的关系开始,则设置较小。

  1. 可视化 Power BI 在语义模型中自动检测到的关系:

    dataset = "Customer Profitability Sample (auto)"
    autodetected = fabric.list_relationships(dataset)
    plot_relationship_metadata(autodetected)
    

    Power BI 在语义模型中自动检测到的关系的屏幕截图。

    Power BI 的自动检测错过了许多关系。 此外,两个自动检测的关系在语义上不正确:

    • Executive[ID] ->Industry[ID]
    • BU[Executive_id] ->Industry[ID]
  2. 将关系打印为表:

    autodetected
    

    行 3 和 4 显示与 Industry 表的关系不正确。 删除这些行。

  3. 删除错误识别的关系。

    # Remove rows 3 and 4 which point incorrectly to Industry[ID]
    autodetected = autodetected[~autodetected.index.isin([3, 4])]
    

    现在,你有正确但不完整的关系。 使用以下 plot_relationship_metadata方法可视化这些不完整的关系:

    plot_relationship_metadata(autodetected)
    

    删除不正确的关系后的可视化效果的屏幕截图。

  4. 使用 SemPy 和函数从语义模型加载所有表,然后使用 < a0/> 查找表之间的关系。 查看日志输出,深入了解此函数的工作原理:

    suggested_relationships_all = find_relationships(
        tables,
        name_similarity_threshold=0.7,
        coverage_threshold=0.7,
        verbose=2
    )
    
  5. 可视化新发现的关系:

    plot_relationship_metadata(suggested_relationships_all)
    

    新发现关系的可视化效果的屏幕截图。

    SemPy 检测所有关系。

  6. 使用 exclude 参数将搜索限制为以前未标识的其他关系:

    additional_relationships = find_relationships(
        tables,
        exclude=autodetected,
        name_similarity_threshold=0.7,
        coverage_threshold=0.7
    )
    
    additional_relationships
    

验证关系

  1. 首先,从 客户盈利率示例 语义模型加载数据。

    dataset = "Customer Profitability Sample"
    tables = {table: fabric.read_table(dataset, table) for table in fabric.list_tables(dataset)['Name']}
    
    tables.keys()
    
  2. 检查主键和外键与函数重叠 list_relationship_violations 。 将函数的 list_relationships 输出传递给 list_relationship_violations.

    list_relationship_violations(tables, fabric.list_relationships(dataset))
    

    结果显示有用的见解。 例如,其中七个值中的 Fact[Product Key] 一个不存在 Product[Product Key],缺少的键是 50

    探索性数据分析和数据清理是迭代的。 你学到的内容取决于你的问题以及如何浏览数据。 语义链接会添加有助于对数据执行更多作的工具。

浏览有关语义链接和 SemPy 的其他教程:

\n\n