Powerquery - 将具有列名和值的单列信息转换为 table
Powerquery - Converting single column information having column names and values to table
我在单列中有一个数据如下
CName1
CName2
CName3
.
.
.
行 1 列 1 值 1
行 1 列 2 值 2
行 1 列 2 值 3
.
.
.
行 2 列 1 值 4
行 2 列 2 值 5
行 2 列 3 值 6
.
.
.
行 3 列 1 值 7
行 3 列 2 值 8
行 3 列 3 值 9
.
.
.
.
我想要的是
CName1
C名称2
C名称3
.....
.....
.....
值 1
值2
价值3
.....
.....
.....
值4
值5
价值6
.....
.....
.....
值7
价值8
值9
.....
.....
.....
.....
.....
.....
.....
.....
如何实现?
感谢您的帮助。
这是一种方法:
假设
- 每个数据行中的位置条目实际上并不存在
- 如果是,我们会修改算法
- 所有列 headers 都在第一组中
- 每组至少由一个空行分隔
- 每个组的列都从第一列开始(但它们不必都具有相同的列数)
请阅读代码注释,探索应用步骤window,了解算法
M码
编辑从最后一行删除hard-coded列名称
let
Source = Excel.CurrentWorkbook(){[Name="Table12"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
//add shifted column
shifted = Table.FromColumns(
Table.ToColumns(#"Changed Type") &
{{null} & List.RemoveLastN(#"Changed Type"[Column1])},
type table[Column1=text, shiftedCol1 =text]),
//create a column which adds an index number at the first row of each group
//then fill down to create a column on which to group
#"Added Index" = Table.AddIndexColumn(shifted, "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "grouper", each if [Column1] <> null and [shiftedCol1] = null then [Index] else null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
#"Filled Down" = Table.FillDown(#"Removed Columns",{"grouper"}),
#"Removed Columns1" = Table.RemoveColumns(#"Filled Down",{"shiftedCol1"}),
//remove the empty rows
#"Filtered Rows" = Table.SelectRows(#"Removed Columns1", each ([Column1] <> null)),
//group by the "grouper" column
//then transpose each sub table
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"grouper"}, {
{"transpose", each Table.Transpose(Table.SelectColumns(_,"Column1"))}
}),
#"Removed Columns2" = Table.RemoveColumns(#"Grouped Rows",{"grouper"}),
//combine the grouped subtables, and promote the headers
comb = Table.Combine(#"Removed Columns2"[transpose]),
#"Promoted Headers" = Table.PromoteHeaders(comb, [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",
List.Transform(Table.ColumnNames(#"Promoted Headers"), each {_, type text}))
in
#"Changed Type1"
编辑
如果正如您的评论所暗示的那样,数据集之间确实没有空白行,但您知道最终结果中的列数,
- 对列数进行硬编码(或将其设置为参数)
- 更改构建“石斑鱼”列的方式,方法是在索引列上执行 IntegerDivide,而不是我在上面使用的公式。
M码
let
Source = Excel.CurrentWorkbook(){[Name="dataTable"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", type text}}),
//hard coded number of columns
numCols = 3,
//add grouper
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Inserted Integer-Division" = Table.AddColumn(#"Added Index", "grouper", each Number.IntegerDivide([Index], numCols), Int64.Type),
#"Removed Columns" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index"}),
//group by the "grouper" column
//then transpose each sub table
#"Grouped Rows" = Table.Group(#"Removed Columns", {"grouper"}, {
{"transpose", each Table.Transpose(Table.SelectColumns(_,"Data"))}
}),
#"Removed Columns2" = Table.RemoveColumns(#"Grouped Rows",{"grouper"}),
//combine the grouped subtables, and promote the headers
comb = Table.Combine(#"Removed Columns2"[transpose]),
#"Promoted Headers" = Table.PromoteHeaders(comb, [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",
List.Transform(Table.ColumnNames(#"Promoted Headers"), each {_, type text}))
in
#"Changed Type1"
我在单列中有一个数据如下
CName1
CName2
CName3
.
.
.
行 1 列 1 值 1
行 1 列 2 值 2
行 1 列 2 值 3
.
.
.
行 2 列 1 值 4
行 2 列 2 值 5
行 2 列 3 值 6
.
.
.
行 3 列 1 值 7
行 3 列 2 值 8
行 3 列 3 值 9
.
.
.
.
我想要的是
CName1
C名称2
C名称3
.....
.....
.....
值 1
值2
价值3
.....
.....
.....
值4
值5
价值6
.....
.....
.....
值7
价值8
值9
.....
.....
.....
.....
.....
.....
.....
.....
如何实现?
感谢您的帮助。
这是一种方法:
假设
- 每个数据行中的位置条目实际上并不存在
- 如果是,我们会修改算法
- 所有列 headers 都在第一组中
- 每组至少由一个空行分隔
- 每个组的列都从第一列开始(但它们不必都具有相同的列数)
请阅读代码注释,探索应用步骤window,了解算法
M码
编辑从最后一行删除hard-coded列名称
let
Source = Excel.CurrentWorkbook(){[Name="Table12"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
//add shifted column
shifted = Table.FromColumns(
Table.ToColumns(#"Changed Type") &
{{null} & List.RemoveLastN(#"Changed Type"[Column1])},
type table[Column1=text, shiftedCol1 =text]),
//create a column which adds an index number at the first row of each group
//then fill down to create a column on which to group
#"Added Index" = Table.AddIndexColumn(shifted, "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "grouper", each if [Column1] <> null and [shiftedCol1] = null then [Index] else null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
#"Filled Down" = Table.FillDown(#"Removed Columns",{"grouper"}),
#"Removed Columns1" = Table.RemoveColumns(#"Filled Down",{"shiftedCol1"}),
//remove the empty rows
#"Filtered Rows" = Table.SelectRows(#"Removed Columns1", each ([Column1] <> null)),
//group by the "grouper" column
//then transpose each sub table
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"grouper"}, {
{"transpose", each Table.Transpose(Table.SelectColumns(_,"Column1"))}
}),
#"Removed Columns2" = Table.RemoveColumns(#"Grouped Rows",{"grouper"}),
//combine the grouped subtables, and promote the headers
comb = Table.Combine(#"Removed Columns2"[transpose]),
#"Promoted Headers" = Table.PromoteHeaders(comb, [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",
List.Transform(Table.ColumnNames(#"Promoted Headers"), each {_, type text}))
in
#"Changed Type1"
编辑
如果正如您的评论所暗示的那样,数据集之间确实没有空白行,但您知道最终结果中的列数,
- 对列数进行硬编码(或将其设置为参数)
- 更改构建“石斑鱼”列的方式,方法是在索引列上执行 IntegerDivide,而不是我在上面使用的公式。
M码
let
Source = Excel.CurrentWorkbook(){[Name="dataTable"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", type text}}),
//hard coded number of columns
numCols = 3,
//add grouper
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Inserted Integer-Division" = Table.AddColumn(#"Added Index", "grouper", each Number.IntegerDivide([Index], numCols), Int64.Type),
#"Removed Columns" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index"}),
//group by the "grouper" column
//then transpose each sub table
#"Grouped Rows" = Table.Group(#"Removed Columns", {"grouper"}, {
{"transpose", each Table.Transpose(Table.SelectColumns(_,"Data"))}
}),
#"Removed Columns2" = Table.RemoveColumns(#"Grouped Rows",{"grouper"}),
//combine the grouped subtables, and promote the headers
comb = Table.Combine(#"Removed Columns2"[transpose]),
#"Promoted Headers" = Table.PromoteHeaders(comb, [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",
List.Transform(Table.ColumnNames(#"Promoted Headers"), each {_, type text}))
in
#"Changed Type1"