【问题标题】:Merging subsequent cell values only for same ids in excel仅在 excel 中合并相同 ID 的后续单元格值
【发布时间】:2023-01-03 21:24:06
【问题描述】:

我在 excel 中有一个要求,我必须在单个单元格中为 C 和 D 列中的分布式数据填充带有国家和城市名称的“设施”列,但仅限于相同的 ID。 我附上图片以供参考

感谢您提前抽出时间

我尝试了 CONCAT 函数和 TEXTJOIN 函数,但这没有帮助

【问题讨论】:

标签: excel excel-formula concatenation


【解决方案1】:

这可以使用 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)中提供的 Power Query 来完成

使用 Power Query

  • 选择数据表中的一些单元格
  • Data => Get&Transform => from Table/Range
  • 当 PQ 编辑器打开时:Home => Advanced Editor
  • 记下表格姓名在 2 号线
  • 粘贴下面的 M 代码代替您看到的内容
  • 将第 2 行中的表名称更改回最初生成的名称。
  • 阅读cmets并探索Applied Steps以了解算法

M代码

let

//Change next line to reflect actual data source
    Source = Excel.CurrentWorkbook(){[Name="Table31"]}[Content],

//set data types for each column
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"Country", type text}, {"City", type text}}),

//Group by ID
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Id"}, {

    //for each subgroup, group by Country
        {"Facility", (t)=> let 
            grp=Table.Group(t,{"Country"},{

    //Then combine all the cities in one text string
              "Cities", (tc)=> "(" & Text.Combine(tc[City],",") & ")"}),

    //Add index column to "number" the different country/cities combinations
            #"Add Index" = Table.AddIndexColumn(grp, "Index",1),
            #"Index to Text" = Table.TransformColumns(#"Add Index",{"Index", each Number.ToText(_,"0. ")}),

    //combine the separate subtable columns into one string
            #"Combine Columns" = Table.CombineColumns(
                #"Index to Text",{"Index","Country","Cities"},Combiner.CombineTextByDelimiter(""),"Facility"),

    //combine the separate rows into a single row
            #"Combine to One Row" = Text.Combine(#"Combine Columns"[Facility]," ")

        in    
            #"Combine to One Row", type text},
        
        {"All", each _, type table [Id=nullable number, Country=nullable text, City=nullable text]}}),

//Expand the Country and City columns
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Country", "City"})
in
    #"Expanded All"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    相关资源
    最近更新 更多