Power Query - (1) 获取列名 (2) 拆分 Table
Power Query - (1) Get Column Name (2) Split Table
如何获取以下场景的查询。
场景一:
我有一个 MAX 列,其中包含定义列中的最大值。
Q1:如何获取该值的列名?
(我以前有一个解决方案是基于动态列名,但没有用。)
场景二:
我有一个 table,我想按列拆分。
Q2: Table 1: [X],[Z],[A],[Y],[B] Table 2: [Z]、[A]、[Y]
来源:
Key X A Z Y B
Cat 15 5 10 5 10
Cat 25 10 15 20
Cat 5 15 5 20 25
Dog 5 25 10 5 5
Dog 5 25 15
Bird 25 15 5 5 5
这是我的问题的可视化。
非常感谢,
艾库特
如果您想查找硬编码列的最大值 A/Z/Y 然后添加索引并使用代码
#"Add Max"=Table.AddColumn(#"Added Index" ,"Max",each List.Max( Record.ToList( Table.SelectColumns(#"Added Index" ,{"A","Z","Y"}){[Index]}) )),
如果您已经有一个名为 Max 的最大列,并且您想在硬编码列名称 X/A/Z/Y/B 中查找它来自哪个列,那么使用代码
#"Add Label"=Table.AddColumn(#"PriorStepName","MaxLabel",each {"X","A","Z","Y","B"}{List.PositionOf(Record.ToList( Table.SelectColumns(#"PriorStepName",{"X","A","Z","Y","B"}){[Index]}),[Max])})
两者兼顾:
let Source = Excel.CurrentWorkbook(){[Name="Table14"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", type text}, {"X", Int64.Type}, {"A", Int64.Type}, {"Z", Int64.Type}, {"Y", Int64.Type}, {"B", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Add Max"=Table.AddColumn(#"Added Index" ,"Max",each List.Max( Record.ToList( Table.SelectColumns(#"Added Index" ,{"A","Z","Y"}){[Index]}) )),
#"Add Label"=Table.AddColumn(#"Add Max","MaxLabel",each {"X","A","Z","Y","B"}{List.PositionOf(Record.ToList( Table.SelectColumns(#"Add Max",{"X","A","Z","Y","B"}){[Index]}),[Max])})
in #"Add Label"
如果要select列col1,col2,col3 , 将这些名称硬编码,然后只需使用
#"Removed Other Columns" = Table.SelectColumns(#"priorstepname",{"col1", "col2", "col2"})
所以具体
Table1=Table.SelectColumns(#"priorstepname",{"X", "A", "Z","Y","B"}),
Table2=Table.SelectColumns(#"priorstepname",{"A", "Z","Y"}),
如何获取以下场景的查询。
场景一: 我有一个 MAX 列,其中包含定义列中的最大值。
Q1:如何获取该值的列名?
(我以前有一个解决方案是基于动态列名,但没有用。)
场景二: 我有一个 table,我想按列拆分。
Q2: Table 1: [X],[Z],[A],[Y],[B] Table 2: [Z]、[A]、[Y]
来源:
Key X A Z Y B
Cat 15 5 10 5 10
Cat 25 10 15 20
Cat 5 15 5 20 25
Dog 5 25 10 5 5
Dog 5 25 15
Bird 25 15 5 5 5
这是我的问题的可视化。
非常感谢, 艾库特
如果您想查找硬编码列的最大值 A/Z/Y 然后添加索引并使用代码
#"Add Max"=Table.AddColumn(#"Added Index" ,"Max",each List.Max( Record.ToList( Table.SelectColumns(#"Added Index" ,{"A","Z","Y"}){[Index]}) )),
如果您已经有一个名为 Max 的最大列,并且您想在硬编码列名称 X/A/Z/Y/B 中查找它来自哪个列,那么使用代码
#"Add Label"=Table.AddColumn(#"PriorStepName","MaxLabel",each {"X","A","Z","Y","B"}{List.PositionOf(Record.ToList( Table.SelectColumns(#"PriorStepName",{"X","A","Z","Y","B"}){[Index]}),[Max])})
两者兼顾:
let Source = Excel.CurrentWorkbook(){[Name="Table14"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Key", type text}, {"X", Int64.Type}, {"A", Int64.Type}, {"Z", Int64.Type}, {"Y", Int64.Type}, {"B", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Add Max"=Table.AddColumn(#"Added Index" ,"Max",each List.Max( Record.ToList( Table.SelectColumns(#"Added Index" ,{"A","Z","Y"}){[Index]}) )),
#"Add Label"=Table.AddColumn(#"Add Max","MaxLabel",each {"X","A","Z","Y","B"}{List.PositionOf(Record.ToList( Table.SelectColumns(#"Add Max",{"X","A","Z","Y","B"}){[Index]}),[Max])})
in #"Add Label"
如果要select列col1,col2,col3 , 将这些名称硬编码,然后只需使用
#"Removed Other Columns" = Table.SelectColumns(#"priorstepname",{"col1", "col2", "col2"})
所以具体
Table1=Table.SelectColumns(#"priorstepname",{"X", "A", "Z","Y","B"}), Table2=Table.SelectColumns(#"priorstepname",{"A", "Z","Y"}),