如何select power query中的某列
How to select certain column in power query
我想在 power query 中选择某些列,但不使用它们的名称。前任。我可以在 R 中通过命令执行此操作:select。我想知道如何在电源查询中做到这一点。我找到了一些信息 ,但不是我需要的所有信息。
如果我想引用多个专栏,有什么想法吗?
如果我这样写代码是行不通的:
#"Filtered Part Desc" = Table.SelectRows (
#"Removed Columns3",
each List.Contains(
{ "ENG", "TRANS" },
Record.Field(_, Table.ColumnNames(#"Removed Columns3") { 5, 6, 7 })
)
)
假设我有这个 table 并且想对它做一些事情。
首先,我想更改第二列和最后一列的列类型。我们可以使用 Table.ColumnNames
使用简单的索引(从零开始)来做到这一点,如下所示:
Table.TransformColumnTypes(
Source,
{
{Table.ColumnNames(Source){1}, Int64.Type},
{Table.ColumnNames(Source){3}, Int64.Type}
}
)
可行,但需要分别指定每个索引。如果我们想像这样取消透视这些列
Table.Unpivot(#"Changed Type", {"Col2", "Col4"}, "Attribute", "Value")
但是我们可以使用与上面相同的方法来代替索引值
Table.Unpivot(
#"Changed Type",
{
Table.ColumnNames(Source){1},
Table.ColumnNames(Source){3}
}, "Attribute", "Value"
)
但是有没有办法做到这一点,我们可以使用一个位置索引值列表并只使用一次 Table.ColumnNames
?我在 this blog 上找到了一个相对简单但不直观的方法。对于这种情况,它的工作方式如下:
Table.Unpivot(
#"Changed Type",
List.Transform({1,3}, each Table.ColumnNames(Source){_}),
"Attribute", "Value"
)
此方法从位置索引值列表开始,然后通过查找与这些位置对应的列的名称将它们转换为列名称。
这是我正在使用的查询的完整 M 代码:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTIE4nIgtlSK1YlWSgKyjIC4AogtwCLJQJYxEFcCsTlYJAXIMgHiKiA2U4qNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t, Col4 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{Table.ColumnNames(Source){1}, Int64.Type},{Table.ColumnNames(Source){3}, Int64.Type}}),
#"Unpivoted Columns" = Table.Unpivot(#"Changed Type", List.Transform({1,3}, each Table.ColumnNames(Source){_}), "Attribute", "Value")
in
#"Unpivoted Columns"
我想在 power query 中选择某些列,但不使用它们的名称。前任。我可以在 R 中通过命令执行此操作:select。我想知道如何在电源查询中做到这一点。我找到了一些信息
如果我想引用多个专栏,有什么想法吗?
如果我这样写代码是行不通的:
#"Filtered Part Desc" = Table.SelectRows (
#"Removed Columns3",
each List.Contains(
{ "ENG", "TRANS" },
Record.Field(_, Table.ColumnNames(#"Removed Columns3") { 5, 6, 7 })
)
)
假设我有这个 table 并且想对它做一些事情。
首先,我想更改第二列和最后一列的列类型。我们可以使用 Table.ColumnNames
使用简单的索引(从零开始)来做到这一点,如下所示:
Table.TransformColumnTypes(
Source,
{
{Table.ColumnNames(Source){1}, Int64.Type},
{Table.ColumnNames(Source){3}, Int64.Type}
}
)
可行,但需要分别指定每个索引。如果我们想像这样取消透视这些列
Table.Unpivot(#"Changed Type", {"Col2", "Col4"}, "Attribute", "Value")
但是我们可以使用与上面相同的方法来代替索引值
Table.Unpivot(
#"Changed Type",
{
Table.ColumnNames(Source){1},
Table.ColumnNames(Source){3}
}, "Attribute", "Value"
)
但是有没有办法做到这一点,我们可以使用一个位置索引值列表并只使用一次 Table.ColumnNames
?我在 this blog 上找到了一个相对简单但不直观的方法。对于这种情况,它的工作方式如下:
Table.Unpivot(
#"Changed Type",
List.Transform({1,3}, each Table.ColumnNames(Source){_}),
"Attribute", "Value"
)
此方法从位置索引值列表开始,然后通过查找与这些位置对应的列的名称将它们转换为列名称。
这是我正在使用的查询的完整 M 代码:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTIE4nIgtlSK1YlWSgKyjIC4AogtwCLJQJYxEFcCsTlYJAXIMgHiKiA2U4qNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t, Col4 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{Table.ColumnNames(Source){1}, Int64.Type},{Table.ColumnNames(Source){3}, Int64.Type}}),
#"Unpivoted Columns" = Table.Unpivot(#"Changed Type", List.Transform({1,3}, each Table.ColumnNames(Source){_}), "Attribute", "Value")
in
#"Unpivoted Columns"