首页 > 解决方案 > 使用重复数据创建 idex

问题描述

我有来自以 csv 格式下载的外部源的数据。此数据显示来自多个用户的交互,并且没有 id 列。我遇到的问题是我无法使用索引,因为多个条目代表交互和过程。交互将是特定用户执行的一组过程,并且该过程表示在特定交互中采取的每个动作。任何用户都可以在一天中的任何时间重复相同的交互。数据看起来像这样:

在此处输入图像描述

User1 有 2 个进程,但有 3 个交互。考虑到单个用户在同一天可能有多个进程,我如何为每个交互分配一个 ID。我尝试在 Power Query 中对它们进行分组,但它对整个过程进行了分组,我无法区分交互的数量。在 Dax 做这件事会更好吗?

编辑:

我注意到很难理解我需要什么,但我认为这将是一种更好的方式来看待它:

在此处输入图像描述

过程 2 是交互中完成的步骤。就像在黄色列中一样,我需要添加一个 ID,以考虑交互的开始位置和结束位置。

标签: powerbidaxpowerquery

解决方案


给定您的新示例,要创建您显示的交互 ID,您只需要表的前两列。如果不是原始数据的一部分,您可以轻松生成第三列(Process2))

看来您希望在流程更改时增加交互 ID

请阅读 M 代码中的注释并探索应用步骤以更好地理解算法:

M代码

let

//be sure to change table name in next row to your real table name
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"User", type text},
        {"Process", type text},
        {"Process2", type text}
    }), 

//add an index column
    idx = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),

//Custom column returns the Index if
//   the current Index is 0 (first row) or 
//       there has been no change in user or process comparing current/previous row
//   else return null
    #"Added Custom" = Table.AddColumn(idx, "Custom", 
            each if [Index]=0 
            then 0
            else 
                if [Process] <> idx[Process]{[Index]-1} or [User] <> idx[User]{[Index]-1} then [Index] 
                else null),

    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),

//Fill down the custom column
//  now have same number for each interactive group
    #"Filled Down" = Table.FillDown(#"Removed Columns",{"Custom"}),

//Group by the "filled down" custom column with no aggregation
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Custom"}, {
        {"all", each _, type table [User=nullable text, Process=nullable text, Process2=nullable text, Custom=number]}
        }),

//add a one-based Index column to the grouped table
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Interaction ID", 1, 1, Int64.Type),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Index",{"Custom"}),

//Re-expand the table
    #"Expanded all" = Table.ExpandTableColumn(#"Removed Columns1", "all", 
        {"User", "Process", "Process2"}, {"User", "Process", "Process2"})
in
    #"Expanded all"

资源
在此处输入图像描述

结果
在此处输入图像描述


推荐阅读