在excel中仅合并相同ID的后续单元格值

4dbbbstv  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(183)

我在excel中有一个要求,即我必须在单个单元格中使用国家和城市名称填充“Facility”列,以获得C和D列中的分布式数据,但仅限于相同的id。

我已附上图像以供参考
提前感谢您的时间
我尝试了CONCAT函数和TEXTJOIN函数,但没有帮助

eh57zj3b

eh57zj3b1#

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

  • 选择数据表中的某个单元格
  • 第一个月
  • PQ编辑器打开时:Home => Advanced Editor
  • 记录第2行中的表名称
  • 将下面的M代码粘贴到您看到的位置
  • 将第2行中的Table名称改回最初生成的名称。
  • 阅读评论并探索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"

相关问题