计算 Power BI 中的唯一列值并用它们的名称替换它们并计算总数

Count unique column values in Power BI and replace them with their name and count total

在 Power BI 中,如何计算列中的唯一值并将这些值替换为值名称和总数?以这种方式做事似乎是在视觉图例中获取总数的唯一方法。

起始数据示例:

Type
Example1
Example1
Example2
Example3
Example2
Example5
Example3
Example4
Example1
Example5
Example1
Example1
Example2
Example3
Example2
Example5
Example3
Example4
Example1
Example5
Example1

想要的结果:

Type
Example1 (7)
Example1 (7)
Example2 (4)
Example3 (4)
Example2 (4)
Example5 (4)
Example3 (4)
Example4 (2)
Example1 (7)
Example5 (4)
Example1 (7)
Example1 (7)
Example2 (4)
Example3 (4)
Example2 (4)
Example5 (4)
Example3 (4)
Example4 (2)
Example1 (7)
Example5 (4)
Example1 (7)

您可以像那样使用 Powerquery。

在示例中,我从 Excel 中获取了一个输入 table,这意味着您必须相应地调整 Source

let
    Source = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
    #"Hinzugefügter Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    groupedbyType = Table.Group(#"Hinzugefügter Index", {"Type"}, {{"CountIt", each _, type table [Type=text, Index=number]}}),
    addCol = Table.AddColumn(groupedbyType, "Count", each List.Count([CountIt][Type])),
    #"Erweiterte CountIt" = Table.ExpandTableColumn(addCol, "CountIt", {"Index"}, {"CountIt.Index"}),
    #"Hinzugefügte benutzerdefinierte Spalte" = Table.AddColumn(#"Erweiterte CountIt", "Result", each [Type] & " (" & Text.From([Count]) & ")"),
    #"Sortierte Zeilen" = Table.Sort(#"Hinzugefügte benutzerdefinierte Spalte",{{"CountIt.Index", Order.Ascending}}),
    #"Entfernte Spalten" = Table.RemoveColumns(#"Sortierte Zeilen",{"Type", "CountIt.Index", "Count"})
in
    #"Entfernte Spalten"

Power Query 的第一步看起来像

最后一步看起来像

我做的是

  • 我加个索引,以后恢复排序
  • 我按类型分组但保留行
  • 我统计每组的行数
  • 我扩展结果
  • 我按要求合并Type和计数结果
  • 我恢复排序
  • 我删除所有其他列

更新:下面是相同的步骤和翻译后的步骤

let
    Source = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
    addedIndex = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    groupedbyType = Table.Group(addedIndex, {"Type"}, {{"CountIt", each _, type table [Type=text, Index=number]}}),
    addCol = Table.AddColumn(groupedbyType, "Count", each Table.RowCount([CountIt])),
    extendCountIt = Table.ExpandTableColumn(addCol, "CountIt", {"Index"}, {"CountIt.Index"}),
    addedColResult = Table.AddColumn(extendCountIt, "Result", each [Type] & " (" & Text.From([Count]) & ")"),
    sortAgain = Table.Sort(addedColResult,{{"CountIt.Index", Order.Ascending}}),
    removedCol = Table.RemoveColumns(sortAgain,{"Type", "CountIt.Index", "Count"})
in
    removedCol

Power BI 更新:对于 Power BI,M 代码可能看起来像 Excel 文件用作数据源。

let
    Source = Excel.Workbook(File.Contents("Excelfilename.xlsx"), null, true),
    Table1 = Source{[Item="Tabelle1",Kind="Table"]}[Data],
    addedIndex = Table.AddIndexColumn(Table1, "Index", 1, 1, Int64.Type),
    groupedbyType = Table.Group(addedIndex, {"Type"}, {{"CountIt", each _, type table [Type=text, Index=number]}}),
    addCol = Table.AddColumn(groupedbyType, "Count", each Table.RowCount([CountIt])),
    extendCountIt = Table.ExpandTableColumn(addCol, "CountIt", {"Index"}, {"CountIt.Index"}),
    addedColResult = Table.AddColumn(extendCountIt, "Result", each [Type] & " (" & Text.From([Count]) & ")"),
    sortAgain = Table.Sort(addedColResult,{{"CountIt.Index", Order.Ascending}}),
    removedCol = Table.RemoveColumns(sortAgain,{"Type", "CountIt.Index", "Count"})
in
    removedCol

替代方法

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source,"count",(i)=>Table.RowCount(Table.SelectRows(Source, each [Type]=i[Type])), type number),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each [Type] & " (" & Text.From([count]) & ")"),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Custom"})
in  #"Removed Other Columns"