简体   繁体   English

Excel Power Query 在其他列之间插入列

[英]Excel Power Query inserting column between other columns

I'm importing a bunch of columns to do some analysis on in Excel power query.我正在导入一堆列以在 Excel 电源查询中进行一些分析。 Some of the analysis columns need to be inserted after a certain column, but every option for adding a column only lets me append the column to the very end.某些分析列需要在某个列之后插入,但是添加列的每个选项只能让我将 append 列到最后。 I want to insert the new columns after the one named "Total" for readability.为了便于阅读,我想在名为“Total”的列之后插入新列。

Bellow a function than outputs the list of re-arranged column names.波纹管 function 比输出重新排列的列名列表。

ReorderList:重新排序列表:

(tableName as table, newColumnName as text, optional columnName as text) as list=>
//tableName - the name of the table we want to reorder.
//newColumnName - the name of the column you want to change the position.
//columnName - the name of the column you want the newColumnName to be positioned after. If omited newColumnName will be placed as the first column.

let
    columnNames = Table.ColumnNames(tableName),
    positionOf = if columnName is null or columnName = "" then 0 else List.PositionOf(columnNames,columnName) + 1,
    intermediaryList = List.Combine({List.FirstN(columnNames,positionOf),{newColumnName}}),
    intermediaryList2 = List.RemoveItems(columnNames,intermediaryList),
    reorderList = List.Combine({intermediaryList,intermediaryList2})
in
    reorderList

Usage like this:像这样的用法:

let 
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom1", each 4),
#"Reordered Columns" = Table.ReorderColumns(#"Added Custom", ReorderList(#"Added Custom","Custom1","Total"))
in 
#"Reordered Columns"

Sample below.下面的示例。

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

// get baseline column names. Use this before inserting new analysis columns
Names = Table.ColumnNames(Source),
TotalSpot = List.PositionOf(Names,"Total"),

// add any code or steps here ; this is random sample. don't use 
#"Added Custom" = Table.AddColumn(Source, "Custom1", each 4),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom2", each 5),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom3", each 6),

// insert this after all your new columns are added
// it moves all new columns to the right of the Total column
//  replace #"Added Custom2" in step below with previous step name

#"Reordered Columns" = Table.ReorderColumns(#"Added Custom2",List.Combine ({List.FirstN(Names,TotalSpot+1),List.RemoveItems(Table.ColumnNames(#"Added Custom2"),Names),List.RemoveFirstN (Names,TotalSpot+1)}))
in #"Reordered Columns"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM