Power Query 在每一列上应用一个函数
Power Query Applying a Function Across Every Column
我正在尝试编写一个查询,它采用 table 并将 table 中的每个数字乘以 100。我已经接近了,但是我无法正确地将它应用于每个柱子。下面是我到目前为止的代码。以 ReplaceTable 开头的行是我为一列工作的行,下面的行是我试图让它为其他列工作的行。我目前正在处理一个小子集,但真实数据可能有 ~100 列,所以我不想手动执行此操作。如果有更好的方法来完成这项任务,请告诉我。我是 Power Query 的新手,所以如果可以请解释我的 error/the 解决方案,以便我学习。谢谢!
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
//Organization will always be of type text. The others will be should be numbers, unless user error
#"Changed Type" = Table.TransformColumnTypes(Source, {{"Organization", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}}),
//function to replace all values in all columns with multiplied values
MultiplyReplace = (DataTable as table, DataTableColumns as list) =>
let
Counter = Table.ColumnCount(DataTable),
ReplaceCol = (DataTableTemp, i) =>
let
colName = {DataTableColumns{i}},
col = Table.Column(DataTableTemp, colName),
//LINE THAT WORKS- want this functionality for ALL columns
ReplaceTable = Table.ReplaceValue(DataTableTemp, each[A], each if [A] is number then [A]*100 else [A], Replacer.ReplaceValue, colName)
//ReplaceTable = Table.ReplaceValue(DataTableTemp, each col, each if col is number then col*100 else col, Replace.ReplaceValue, colName)
in
if i = Counter-1 then ReplaceTable else @ReplaceCol(ReplaceTable, i+1)
in
ReplaceCol(DataTable, 0),
allColumns = Table.ColumnNames(#"Changed Type"),
#"Multiplied Numerics" = MultiplyReplace(#"Changed Type", allColumns)
//#"Restored Type" = Value.ReplaceTypes(#"Multiplied Numerics", #"Changed Type")
in
#"Multiplied Numerics"
这个问题涉及函数和变量的范围。
对于硬编码的列名(例如 [A]),代码将 shorthand 理解为实际意思 _[A]
。在 Table.ReplaceValue
函数中,_
引用当前记录或行。但是,col
变量引用了 entire table 列。在替换函数中使用时,会导致错误。 (不幸的是(?),替换函数中的错误被忽略,没有错误消息,因此问题很难追踪。)
在更正后的代码中,我删除了 col
变量,因为它是在错误的范围级别确定的。我将 colName
更改为文本而不是列表,然后将 Record.Field
函数与 _
(Table.ReplaceValue
函数中的当前记录)和文本值 colName
使用 Table.ReplaceValue
函数本身提取计算所需的记录。
更正代码
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
//Organization will always be of type text. The others will be should be numbers, unless user error
#"Changed Type" = Table.TransformColumnTypes(Source, {{"Organization", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}}),
//function to replace all values in all columns with multiplied values
MultiplyReplace = (DataTable as table, DataTableColumns as list) =>
let
Counter = Table.ColumnCount(DataTable),
ReplaceCol = (DataTableTemp, i) =>
let
colName = DataTableColumns{i},
//LINE THAT WORKS- want this functionality for ALL columns
ReplaceTable = Table.ReplaceValue(DataTableTemp,each Record.Field(_, colName), each if Record.Field(_, colName) is number then Record.Field(_, colName)*100 else Record.Field(_, colName),Replacer.ReplaceValue,{colName})
//ReplaceTable = Table.ReplaceValue(DataTableTemp, each col, each if col is number then col*100 else col, Replace.ReplaceValue, colName)
in
if i = Counter-1 then ReplaceTable else @ReplaceCol(ReplaceTable, i+1)
in
ReplaceCol(DataTable, 0),
allColumns = Table.ColumnNames(#"Changed Type"),
#"Multiplied Numerics" = MultiplyReplace(#"Changed Type", allColumns)
//#"Restored Type" = Value.ReplaceTypes(#"Multiplied Numerics", #"Changed Type")
in
#"Multiplied Numerics"
我正在尝试编写一个查询,它采用 table 并将 table 中的每个数字乘以 100。我已经接近了,但是我无法正确地将它应用于每个柱子。下面是我到目前为止的代码。以 ReplaceTable 开头的行是我为一列工作的行,下面的行是我试图让它为其他列工作的行。我目前正在处理一个小子集,但真实数据可能有 ~100 列,所以我不想手动执行此操作。如果有更好的方法来完成这项任务,请告诉我。我是 Power Query 的新手,所以如果可以请解释我的 error/the 解决方案,以便我学习。谢谢!
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
//Organization will always be of type text. The others will be should be numbers, unless user error
#"Changed Type" = Table.TransformColumnTypes(Source, {{"Organization", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}}),
//function to replace all values in all columns with multiplied values
MultiplyReplace = (DataTable as table, DataTableColumns as list) =>
let
Counter = Table.ColumnCount(DataTable),
ReplaceCol = (DataTableTemp, i) =>
let
colName = {DataTableColumns{i}},
col = Table.Column(DataTableTemp, colName),
//LINE THAT WORKS- want this functionality for ALL columns
ReplaceTable = Table.ReplaceValue(DataTableTemp, each[A], each if [A] is number then [A]*100 else [A], Replacer.ReplaceValue, colName)
//ReplaceTable = Table.ReplaceValue(DataTableTemp, each col, each if col is number then col*100 else col, Replace.ReplaceValue, colName)
in
if i = Counter-1 then ReplaceTable else @ReplaceCol(ReplaceTable, i+1)
in
ReplaceCol(DataTable, 0),
allColumns = Table.ColumnNames(#"Changed Type"),
#"Multiplied Numerics" = MultiplyReplace(#"Changed Type", allColumns)
//#"Restored Type" = Value.ReplaceTypes(#"Multiplied Numerics", #"Changed Type")
in
#"Multiplied Numerics"
这个问题涉及函数和变量的范围。
对于硬编码的列名(例如 [A]),代码将 shorthand 理解为实际意思 _[A]
。在 Table.ReplaceValue
函数中,_
引用当前记录或行。但是,col
变量引用了 entire table 列。在替换函数中使用时,会导致错误。 (不幸的是(?),替换函数中的错误被忽略,没有错误消息,因此问题很难追踪。)
在更正后的代码中,我删除了 col
变量,因为它是在错误的范围级别确定的。我将 colName
更改为文本而不是列表,然后将 Record.Field
函数与 _
(Table.ReplaceValue
函数中的当前记录)和文本值 colName
使用 Table.ReplaceValue
函数本身提取计算所需的记录。
更正代码
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
//Organization will always be of type text. The others will be should be numbers, unless user error
#"Changed Type" = Table.TransformColumnTypes(Source, {{"Organization", type text}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}}),
//function to replace all values in all columns with multiplied values
MultiplyReplace = (DataTable as table, DataTableColumns as list) =>
let
Counter = Table.ColumnCount(DataTable),
ReplaceCol = (DataTableTemp, i) =>
let
colName = DataTableColumns{i},
//LINE THAT WORKS- want this functionality for ALL columns
ReplaceTable = Table.ReplaceValue(DataTableTemp,each Record.Field(_, colName), each if Record.Field(_, colName) is number then Record.Field(_, colName)*100 else Record.Field(_, colName),Replacer.ReplaceValue,{colName})
//ReplaceTable = Table.ReplaceValue(DataTableTemp, each col, each if col is number then col*100 else col, Replace.ReplaceValue, colName)
in
if i = Counter-1 then ReplaceTable else @ReplaceCol(ReplaceTable, i+1)
in
ReplaceCol(DataTable, 0),
allColumns = Table.ColumnNames(#"Changed Type"),
#"Multiplied Numerics" = MultiplyReplace(#"Changed Type", allColumns)
//#"Restored Type" = Value.ReplaceTypes(#"Multiplied Numerics", #"Changed Type")
in
#"Multiplied Numerics"