添加自定义列和动态列 [英] Add Custom and Dynamic columns
本文介绍了添加自定义列和动态列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表,并且正在尝试了解如何创建自定义代码来添加具有基于另一个表中的行值的自定义名称的动态列。然后,我需要使用表2中的行值,不仅要创建列名,还要用表2中另一列的值填充新的动态列。 表1的行数取决于用户输入的内容。 表2的行数取决于用户输入的值的数量。
前的表1
第1列 | 第2列 | 第3列 |
---|---|---|
材料1 | 材料2 | 材料3 |
材料4 | 材料5 | 材料6 |
。 | 。 | 。 |
。 | 。 | 。 |
表2
名称 | 值 |
---|---|
名称1 | 100 |
名称2 | 500 |
。 | 。 |
名称X | Y |
后的表1
第1列 | 第2列 | 第3列 | ";列";&;名称1 | ";列";&;名称2 | . | &Quot;列&Quot;&;NameX |
---|---|---|---|---|---|---|
材料1 | 材料2 | 材料3 | 100 | 500 | . | Y |
材料4 | 材料5 | 材料6 | 100 | 500 | . | Y |
。 | 。 | 。 | 100 | 500 | . | Y |
。 | 。 | 。 | 100 | 500 | . | Y |
";列&q;&;Name1表示我要将该列与表2中"名称"列中的值连接起来。
推荐答案
您可以通过不引用绝对列名,而是使用Table.ColumnNames
函数返回这些名称来使其动态化。
我确实假设表2中的列名是固定的。如果不是,则可以更改代码。
阅读代码注释并检查"应用的步骤"窗口,以更好地理解使用的方法。这里有设置数据类型以及在不引用硬编码列名的情况下重命名列的示例。M代码
let
//read in the two tables and set the data types
Source1 = Excel.CurrentWorkbook(){[Name="Table_2"]}[Content],
Table2 =Table.TransformColumnTypes(Source1,
{{"Name", type text},{"Values", type any}}),
Source = Excel.CurrentWorkbook(){[Name="Table_1_Before"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,
List.Transform(Table.ColumnNames(Source), each {_, type text})),
//create the extra columns by
//Transpose Table2
// Use first row as headers
xpose = Table.Transpose(Table2),
#"Promoted Headers" = Table.PromoteHeaders(xpose, [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",
List.Transform(Table.ColumnNames(#"Promoted Headers"), each {_, type any})),
//rename the columns
renameNameCols = Table.RenameColumns(#"Changed Type1",
List.Zip(
{Table.ColumnNames(#"Changed Type1"),
List.Transform(Table.ColumnNames(#"Changed Type1"), each "Column " & _)})),
//Combine the tables
combine = Table.Combine({#"Changed Type",renameNameCols}),
//fill up the original table 2 columns and remove the blank Table 1 rows
#"Filled Up" = Table.FillUp(combine,Table.ColumnNames(renameNameCols)),
#"Filtered Rows" = Table.SelectRows(#"Filled Up", each ([Col1] <> null))
in
#"Filtered Rows"
请注意,我没有添加逻辑以避免在...
前面加上单词列,如您所需的输出中所示,但如果确实需要,可以很容易地添加
这篇关于添加自定义列和动态列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文