【问题标题】:Use current row value to perform calculations on table with matching column name in Power BI?使用当前行值对 Power BI 中具有匹配列名的表执行计算?
【发布时间】:2019-08-24 02:54:18
【问题描述】:

我正在使用两张桌子。一个是大约 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

【问题讨论】:

  • DAX 没有设施来做你想做的事情。在 Power Query/M 中,您可以使用 Table.SelectColumns(table, {"columnname1", "columnname2"})Record.Field(record, "fieldname") 等函数按名称引用。就目前而言,您的问题过于笼统,无法有效回答。您能否详细说明您已构建的查询以及您想要实现的特定功能?
  • @greggyb 是对的。你可以在 M 中做一些这样的事情,但 DAX 根本不会做。
  • @greggyb 我将问题更新为更具体。 M很好用。我正在寻找许多自定义函数,实际上我只是想了解如何正确创建参考的基础方面,但为简单起见,我选择了 Counta。

标签: powerbi powerquery


【解决方案1】:

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

我假设您的表数据位于名为 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.SchemaTable.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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 2020-05-03
    • 1970-01-01
    • 2022-12-05
    • 2017-06-24
    • 2017-08-25
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多