Excel Power Query:如何在一步中将数字添加到所有列中

huangapple go评论76阅读模式
英文:

Excel Power Query: How to add a number to all the columns in one step

问题

我试图将一个或多个数字添加到多个列的所有行中。目前,我必须从“转换”选项卡的“标准添加”中按住每个列,并逐个输入数字。这增加了步骤数。

有时我有30个列,所以我应该在查询中添加30个步骤,数字有时可能相同,有时可能不同。正确的做法是什么?这样做花费了我很多时间,我无法弄清楚。我是否可以一次完成它?是否可能一次完成?

另一个问题是,当我按住所有列并尝试添加数字时,标准功能会变灰。大家是否认为我做错了?有人可以建议正确的方法吗?

我尝试了以下代码:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Full Name", type text}, {"1979", Int64.Type}, {"1990", Int64.Type}, {"1997", Int64.Type}, {"2007", Int64.Type}, {"2010", Int64.Type}}),
    #"Added to Column" = Table.TransformColumns(#"Changed Type", {{"1979", each _ + 5, type number}}),
    #"Added to Column1" = Table.TransformColumns(#"Added to Column", {{"1990", each _ + 10, type number}}),
    #"Added to Column2" = Table.TransformColumns(#"Added to Column1", {{"1997", each _ + 20, type number}}),
    #"Added to Column3" = Table.TransformColumns(#"Added to Column2", {{"2007", each _ + 15, type number}}),
    #"Added to Column4" = Table.TransformColumns(#"Added to Column3", {{"2010", each _ + 30, type number}})
in
    #"Added to Column4"

请注意,这是一段Power Query(M语言)代码,用于在Excel中进行数据转换。如果您需要进一步的帮助,请告诉我。

英文:

I am trying to add a number or numbers to all the rows in multiple columns. For now, i have to hold down each of the columns from transform tab standard add and entering the numbers one by one. Thus, increasing the number of steps.

Excel Power Query:如何在一步中将数字添加到所有列中

Sometimes I have 30 columns so should i add 30 steps to my query, and numbers may be same sometimes and may not be. What is the right way of doing it. It takes me lot of time while doing this and i am not able to figure out. How can i do it in one step is it possible to do?

Another issue is when I hold down all the columns and try to add the number the standard feature greys out. Do you all think i am doing wrong, can anyone suggest the right way of doing it

Excel Power Query:如何在一步中将数字添加到所有列中

I tried.

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Full Name", type text}, {"1979", Int64.Type}, {"1990", Int64.Type}, {"1997", Int64.Type}, {"2007", Int64.Type}, {"2010", Int64.Type}}),
    #"Added to Column" = Table.TransformColumns(#"Changed Type", {{"1979", each _ + 5, type number}}),
    #"Added to Column1" = Table.TransformColumns(#"Added to Column", {{"1990", each _ + 10, type number}}),
    #"Added to Column2" = Table.TransformColumns(#"Added to Column1", {{"1997", each _ + 20, type number}}),
    #"Added to Column3" = Table.TransformColumns(#"Added to Column2", {{"2007", each _ + 15, type number}}),
    #"Added to Column4" = Table.TransformColumns(#"Added to Column3", {{"2010", each _ + 30, type number}})
in
    #"Added to Column4"

答案1

得分: 2

以下是已翻译的内容:

这里有另一种方法,您需要知道:

  • 哪些列需要添加
    • 我假设除第一列之外的所有列都需要添加
  • 每列将添加多少

我创建的这三个列表都可以包含在#"Add to Columns"语句中,但我将它们拆分出来以增加清晰度。

英文:

Here's another approach where you need to know

  • which columns need the additions
    • I assumed it as all except the first
  • How much will be added to each column

The three lists I created could all be included in the #"Add to Columns" statement but I split them out for clarity.

let
    Source = Excel.CurrentWorkbook(){[Name="your_table_name"]}[Content],

//Creat List of all column names except the first
    colNames = List.RemoveFirstN(Table.ColumnNames(Source),1),

//Create List of the additions as a function
    Adds = List.Transform({5,10,15,20,25}, (n)=> each _ + n),

//Create LIst of the data types
    Types = List.Repeat({Int64.Type}, List.Count(colNames)),

//Create transfrom statement
    #"Add to Columns" = Table.TransformColumns(Source,List.Zip({colNames, Adds, Types}))

in
    #"Add to Columns"

答案2

得分: 1

让我来翻译代码部分:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Full Name", type text}, {"1979", Int64.Type}, {"1990", Int64.Type}, {"1997", Int64.Type}, {"2007", Int64.Type}, {"2010", Int64.Type}}),
    #"Added to Column" = Table.TransformColumns(#"Changed Type", {{"1979", each _ + 5, type number}})
in
    #"Added to Column"
英文:

To do this in one step, you'll have to type in the formula bar. Click the first column and add a number to it to get the code generated for you and you'll have this:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Full Name", type text}, {"1979", Int64.Type}, {"1990", Int64.Type}, {"1997", Int64.Type}, {"2007", Int64.Type}, {"2010", Int64.Type}}),
    #"Added to Column" = Table.TransformColumns(#"Changed Type", {{"1979", each _ + 5, type number}})
in
    #"Added to Column"

Your last step in the formula bar will look like this:

Excel Power Query:如何在一步中将数字添加到所有列中

The last parameter is a list of lists and you can just copy and paste in as many as you want. e.g.

Excel Power Query:如何在一步中将数字添加到所有列中

= Table.TransformColumns(#"Changed Type", {{"1979", each _ + 5, type number}, {"1990", each _ + 10, type number}, {"2007", each _ + 15, type number}})

huangapple
  • 本文由 发表于 2023年8月5日 07:57:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定