power query - 如何在特定列中向下移动行

时间:2017-11-09 07:54:35

标签: excel powerquery excel-2016

我需要将特定列向下移动4行,这可能与电源查询有关吗?如果可能的话,我该怎样才能做到这一点?参考文献表示赞赏。

2 个答案:

答案 0 :(得分:1)

我不认为可以通过使用查询编辑器中的菜单选项来实现,但可以通过高级编辑器中的编码来完成。

如果要向下移动的列称为“ColToMove”,则可以使用以下代码。请注意,其他列将在结果的最后4行中获取空值。

let
    #"**** QUERY SUMMARY ****" =
        "This query moves column ""ColToMove"" 4 rows down, by:#(lf)" &
        "1. Saving the original column sequence and table type.#(lf)" &
        "2. Drilling down into ""ColToMove"".#(lf)" &
        "3. Adding 4 nulls at the start of the resulting list.#(lf)" &
        "4. Removing the ""ColToMove"" from the original table." &
        "5. Transforming the resulting table in a list of list.#(lf)" &
        "6. Adding the list from step 3.#(lf)" &
        "7. Transforming the result into a table, with the moved column as last column.#(lf)" &
        "8. Reordering the columns to the original sequence.#(lf)" &
        "9. Restoring the original table type.",

    Source = Table1,
    OriginalColumnSequence = Table.ColumnNames(Source),
    OriginalTableType = Value.Type(Source),

    ColToMove = Source[ColToMove],
    MovedCol = List.Repeat({null},4)&ColToMove,

    RemovedColToMov = Table.RemoveColumns(Source,{"ColToMove"}),
    TableToColumns = Table.ToColumns(RemovedColToMov),
    AddedMovedCol = TableToColumns&{MovedCol},

    ColumnNames = Table.ColumnNames(RemovedColToMov)&{"ColToMove"},
    TableFromColumns = Table.FromColumns(AddedMovedCol,ColumnNames),

    ReorderedColumns = Table.ReorderColumns(TableFromColumns,OriginalColumnSequence),
    RestoredOriginalTableType = Value.ReplaceType(ReorderedColumns,OriginalTableType)
in
    RestoredOriginalTableType

答案 1 :(得分:1)

道歉,第二个想法,它可以使用菜单选项实现。在此版本中,测试表的所有列名都是硬编码的。

let
    #"QUERY SUMMARY" =
        "In this query, column ""ColToMove"" is shifted down 4 positions.#(lf)" &
        "The code is mainly generated via the menu options in the Query Editor.#(lf)" &
        "Index columns are added, starting with 0 and with 4.#(lf)" &
        "Merging the table with itself on the different Index columns, cause the merged version to be shifted down 4 positions.#(lf)" &
        "The original sort is restored, columns are removed and reordered.",
    Source = Table1,
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
    #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 4, 1),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1",{"Index"},#"Added Index1",{"Index.1"},"Added Index1",JoinKind.FullOuter),
    #"Removed Columns" = Table.RemoveColumns(#"Merged Queries",{"ColToMove"}),
    #"Expanded Added Index1" = Table.ExpandTableColumn(#"Removed Columns", "Added Index1", {"ColToMove", "Index.1"}, {"ColToMove", "Index.1.1"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Added Index1",{{"Index.1.1", Order.Ascending}, {"Index.1", Order.Ascending}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"Index", "Index.1", "Index.1.1"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"ID", "ColToMove", "Col3", "Col4", "Col5", "Col6", "Col7", "Col8", "Col9", "Col10"})
in
    #"Reordered Columns"