简体   繁体   English

将逗号分隔的单元格拆分为多行,保持原始行不变?

[英]Split comma delimited cell into multiple rows, keeping Original Rows as it is?

I have tried Excel Power Query option to Split Comma Seprated Values into Rows.我试过 Excel Power Query 选项将逗号分隔值拆分为行。 It working Well.它运作良好。 But I want to Keep original row as it is .我想保持原来的行

Because Power Query Deletes Original Row and Generates New.因为 Power Query 删除原始行并生成新行。 I want to keep original row & Generate new rows below that.我想保留原始行并在其下方生成新行。 by comma delimiter.通过逗号分隔符。 How can we do this?我们应该怎么做?

Expectation:期待:

I have Data Rows like
1 - name - A,B
2 - name - A,B

It should Convert Like This.
1 - name - A,B
1 - name - A
1 - name - B
2 - name - A,B
2 - name - A
2 - name - B

Ok got the Simple solution.好的,得到了简单的解决方案。

  1. Just Split All Rows by Comma Delimiter 'using power query'.只需通过逗号分隔符“使用电源查询”拆分所有行。 we get " After Split Rows "我们得到“ After Split Rows
  2. Then Paste All Original Rows Above " After Split Rows "然后将所有原始行粘贴到“ After Split Rows ”上方
  3. Add the filter to numbers/id row & use " Sort smallest to largest " and Done.将过滤器添加到 numbers/id 行并使用“ Sort smallest to largest ”并完成。

We get the Expected Result with the Original Rows & Split Rows Below them, as shown in above question.如上问题所示,我们得到了原始行和拆分行下方的预期结果。

Use 2 imports.使用 2 个进口。

What Power Query passes across to Excel is the model, and this can contain more than one table or process. Power Query 传递给 Excel 的是 model,它可以包含多个表或进程。

Import your data to be split.导入要拆分的数据。 Carry out your Split by Delimiter into Rows .将您Split by Delimiter into Rows This will give you a table which has your separated values - Table After Split .这将为您提供一个表格,其中包含您的分隔值 - Table After Split

Import your data again as Table Original .再次将您的数据导入为Table Original

Append Table After Split to Table After Split to produce Table Combined . Append Table After SplitTable After Split后生成Table Combined

Drop Table Original and Table After Split as they are no longer needed and are wasting memory.删除Table Original Table After Split的表,因为它们不再需要并且正在浪费 memory。

Now what you have remaining in your model is your combined table.现在您在 model 中剩下的就是您的组合表。

Just add a custom column which contains a List consisting of Column 3 plus the Split of column 3 with the comma delimiter.只需添加一个自定义列,其中包含一个列表,该列表由第 3 列加上第 3 列的拆分和逗号分隔符组成。 Then expand that column into rows然后将该列展开为行

  • Custom Column:自定义列:
    {[Column3]} & Text.Split([Column3],",")

在此处输入图像描述

original data原始数据
在此处输入图像描述

M Code M代码

let

//change Table name in next line to whatever your real source is named
    Source = Excel.CurrentWorkbook(){[Name="Table12"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
                            {"Column1", Int64.Type}, {"Column2", type text}, {"Column3", type text}}),

//create the list for the column    
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each {[Column3]} & Text.Split([Column3],",")),
    
//remove old column 3 and expand the list
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column3"}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns", "Custom")
in
    #"Expanded Custom"

Results结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM