AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1779078
Accepted
User1974
User1974
Asked: 2023-04-15 14:38:25 +0800 CST2023-04-15 14:38:25 +0800 CST 2023-04-15 14:38:25 +0800 CST

将组 ID 分配给行(标签)

  • 772

我有一个 Excel 365 日志条目表。日志按开始标记和结束标记分组。

在此处输入图像描述

Column C: =IF(   LEFT([@LOGS],6)="<Event","start",   IF(LEFT([@LOGS],7)="</Event","end",   ""))

原始数据:

TAG_START_END   LOGS
start           <Event time="Sat Apr 15 1:13:17.750" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0"
                Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase
                      SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO
end             </Event>
start           <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1"

end             </Event>
start           <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2"
                Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase
                      SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO
                      Number of features returned: 100
end             </Event>
start           <Event time="Sat Apr 15 1:13:17.749" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0"

end             </Event>
start           <Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0"
                Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase
                      SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO
end             </Event>
start           <Event time="Sat Apr 15 1:13:17.747" type="Debug" thread="2fec: Main CIM worker thread" elapsed="2"
                Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase
                      SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO
                      Number of features returned: 100
end             </Event>
start           <Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1"

end             </Event>
start           <Event time="Sat Apr 15 1:13:17.746" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0"

end             </Event>
start           <Event time="Sat Apr 15 1:13:17.744" type="Debug" thread="2fec: Main CIM worker thread" elapsed="0"
                Database: C:\2023 Files\GDBs_4\New Mobile Geodatabase.geodatabase
                      SQL: SELECT main.ACTIVE_TRANSPORTATION.OBJECTID,main.active_transportation_nt_flag_vw.flag FRO
end             </Event>
start           <Event time="Sat Apr 15 1:13:17.743" type="Debug" thread="2fec: Main CIM worker thread" elapsed="1"

end             </Event>

对于每个标签组,我想填充 GROUP_ID 列。

在上面的屏幕截图中,我在该字段中手动输入了值。现在,我想找到一种使用公式或 Power Query 填充字段的方法。


如何填充 GROUP_ID 列?

原因:我想最终找到一种将 LOGS 信息转换为列的方法: https: //i.stack.imgur.com/EfTUV.png。我认为 GROUP_ID 列会有所帮助。


有关的:

将 Diagnostic Monitor 日志复制为 Excel 表格,而不是垂直标记


编辑:

这是如何使用 SQL 完成的:

Oracle SQL -根据开始/结束标记将 GROUP_ID 分配给行

sum(case when log_tags like '<Event%' then 1 else 0 end) over (order by id)

可以在 Excel 中完成类似的操作吗?

microsoft-excel
  • 1 1 个回答
  • 45 Views

1 个回答

  • Voted
  1. Best Answer
    Frédéric Loyer
    2023-04-17T15:42:54+08:002023-04-17T15:42:54+08:00

    使用 PowerQuery,如果结果定义明确,您可以定义引用自身的步骤AddGroup。AddSeq此类递归定义@在引用它们时需要在每个步骤的名称中使用。

    let
        Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
        TypeChanged = Table.TransformColumnTypes(Source,{{"TAG_START_END", type text}, {"LOGS", type text}}),
        IndexAdded = Table.AddIndexColumn(TypeChanged, "Index", 0, 1),
        AddSeq = Table.AddColumn(IndexAdded, "Seq", 
           each if [TAG_START_END]="start" then 1 else @AddSeq[Seq]{[Index]-1} +1),
        AddGroup = Table.AddColumn(AddSeq, "GroupID", 
           each if [TAG_START_END]="start" 
                then (if [Index]=0 then 1 else @AddGroup[GroupID]{[Index]-1}+1) 
                else @AddGroup[GroupID]{[Index]-1})
    in
        AddGroup
    

    此处 IndexAdded 插入引用表格上一个单元格所需的索引列 ( {[Index]-1})

    在这里,我使用了 C 列(开始/结束),但Text.StartsWith可用于计算 PowerQuery 中的开始条件。

    可以增强该脚本以重新排序列,或抑制索引列。


    这个查询很容易写(如果我们知道表的递归构造),但效率不高。

    这是一个新的优化版本(2000 行 3 秒)。它直接取一LOGS列,验证它是否以 开头<Event,如果是,则开始一个新的组序列。

    let
        Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
        TypeChanged = Table.TransformColumnTypes(Source,{{"LOGS", type text}}),
        Lines = Table.ToRecords(TypeChanged),
        Result = List.Accumulate(Lines, [table={}, group=0, seq=0],
            (state, current) => if Text.StartsWith(current[LOGS],"<Event")
                then let g = state[group]+1, s = 1 in
                     [ table= state[table] & {[group=g,seq=s] & current},
                       group=g, seq=s] 
                else let g = state[group], s = state[seq]+1 in
                     [ table= state[table] & {[group=g,seq=s] & current},
                       group=g, seq=s] ),
        Table1 = Result[table],
        Table2 = Table.FromRecords(Table1)
    in
        Table2
    
    • 3

相关问题

  • 带有“和”运算符的 Excel 数据透视表

  • 如何对整列使用 Excel 的 LENGTH 函数?

  • Excel 数组(2 个变量)

  • 如何从 WSL 打开 office 文件

  • VBA根据文件名重命名工作表

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve