首页 > 解决方案 > 使用当前行值对 Power BI 中具有匹配列名的表执行计算?

问题描述

我有两张正在使用的桌子。一个是大约 50 列的大型数据集。第二个表是Table.Schema()第一个数据集的表。

我正在尝试构建一个类似于Table.Profile()但具有更多自定义功能的数据质量报告。但是,我什至无法为每个源列重新创建 Table.Profile 的基本元素,例如 Counta 或 Count of null 。

如何使用架构Name值对架构名称表示的数据源列执行计数计算?请参阅下面的架构表以获取所需的输出,计算数据源中非空值的数量。

注意:由于我希望构建此公式的大量列,因此它是基于名称值的动态,而不是将 50 多个列名称硬编码到公式中

数据源

ID | Status | Created | Modified
1  | Active | 1/1/19  | null
2  | null   | 1/5/15  | 1/6/15
3  | Active | null    | null

架构

Name     | Type   | Counta 
ID       | Number | 3
Status   | Text   | 2
Created  | Date   | 2
Modified | Date   | 1

标签: powerbipowerquery

解决方案


我将给出计数、非空计数和不同计数的示例,因此您可以看到该模式的几个实例。

我假设您的表数据位于名为Table. 下面是添加我上面提到的计数的完整高级编辑器视图。

let
     srcTable = Table // This is an arg to all steps below, so useful to have here
    ,schema = Table.Schema(srcTable) // you've used this
    ,count =
        Table.AddColumn( // self-descriptive
             schema      // the source table to add this new column to
            ,"Count"     // the name of the new column we are adding
            // below is a function to evaluate per row in the source table
            // The way to reference a table column by name is
            // Table.Column(table, "column"). That returns a list.
            ,each List.Count(Table.Column(srcTable, _[Name])))
    ,nonNullCount =
        Table.AddColumn(
             count
             ,"Non-null count"
             ,each List.NonNullCount(Table.Column(srcTable, _[Name])))
    ,distinctCount =
        Table.AddColumn(
             nonNullCount
             ,"Distinct Count"
             ,each List.Count(List.Distinct(Table.Column(srcTable, _[Name]))))
in
    distinctCount

实际上,我不会尝试重新实现Table.Profile. 我会加入结果,Table.Schema然后Table.Profile添加额外的分析。

let
     srcTable = Opportunity // This is an arg to all steps below, so useful to have here
    ,schema = Table.Schema(srcTable) // you've used this
    ,profile = Table.Profile(srcTable)
    ,schemaAndProfile =
        Table.NestedJoin(
             schema
            ,"Name"
            ,profile
            ,"Column"
            ,"profile"
        )
    ,expandProfile =
        Table.ExpandTableColumn(
             schemaAndProfile
            ,"profile"
            ,{"Min", "Max", "Average", "StandardDeviation", "Count", "NullCount", "DistinctCount"}
            ,{"profile.Min", "profile.Max", "profile.Average", "profile.StandardDeviation", "profile.Count", "profile.NullCount", "profile.DistinctCount"})
in
    expandProfile

引用列与上面的示例相同,但这会让您处于更稳健的状态。


推荐阅读