在Excel Power Query中对类型日期的透视列名称进行排序

时间:2018-01-19 20:07:21

标签: excel powerquery

我使用数量作为值旋转了一年的专栏,但列显示无序。有没有办法让这些按正确的顺序重新排序,而不必每次都手动重新排序?我想要的订单是:2017年11月,2017年12月,2018年1月。

enter image description here

这是Power M代码:

let
    Source = Table.NestedJoin(#"Merge With New Data Source",{"Ship to Name", "Ship to Postal Code"},#"Ship to Address",{"Name", "Post_Code"},"Ship to Address",JoinKind.Inner),
    #"Expanded Ship to Address" = Table.ExpandTableColumn(Source, "Ship to Address", {"City", "County"}, {"Ship to Address.City", "Ship to Address.County"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Ship to Address",{{"Ship to Address.City", "Ship to City"}, {"Ship to Address.County", "Ship to State"}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Ship to Name", "Ship to City", "Ship to State", "Ship to Postal Code", "Bill to Name", "Item No", "Item Description", "2018-January", "2017-November", "2017-December"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Ship to Postal Code"})
in 
    #"Removed Columns"

1 个答案:

答案 0 :(得分:1)

您可以使用排序功能Table.Sort(...),{{"Attribute", Order.Ascending}})

你的最后一行是:

#"Removed Columns" = Table.Sort(Table.RemoveColumns(#"Reordered Columns",{"Ship to Postal Code"})),{{"Attribute", Order.Ascending}})

问题是,它会组织像[2017-Dec...], [2017-Nov...], [2018-Jan...]这样的字段,因为它们不是“日期类型”。

我添加了一些行来修复连接Year-MonthNum-MonthName:[2017-11-Nov], [2017-12-Dec], [2018-1-Jan]

let
    Source = Table.NestedJoin(#"Merge With New Data Source",{"Ship to Name", "Ship to Postal Code"},#"Ship to Address",{"Name", "Post_Code"},"Ship to Address",JoinKind.Inner),
    #"Expanded Ship to Address" = Table.ExpandTableColumn(Source, "Ship to Address", {"City", "County"}, {"Ship to Address.City", "Ship to Address.County"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Ship to Address",{{"Ship to Address.City", "Ship to City"}, {"Ship to Address.County", "Ship to State"}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Ship to Name", "Ship to City", "Ship to State", "Ship to Postal Code", "Bill to Name", "Item No", "Item Description", "2018-January", "2017-November", "2017-December"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Ship to Postal Code"}),

    //Transform each date field as registers
    UnpivotedDatesFields = Table.UnpivotOtherColumns(#"Removed Columns", {"Item Description", "Item No", "Bill to Name", "Ship to State", "Ship to City", "Ship to Name"}, "Attribute", "Value"),
    // Transform new field Attribute as Date Type
    FormatDate = Table.TransformColumnTypes(UnpivotedDatesFields,{{"Attribute", type date}}),
    //Extract Year From Attribute
    Year = Table.AddColumn(FormatDate, "Year", each Date.Year([Attribute]), Int64.Type),
    //Extract Month Number From Attribute
    Month = Table.AddColumn(Year, "Month", each Date.Month([Attribute]), Int64.Type),
    //Extract Month Name from Attribute and returns the first three characters
    MonthName = Table.AddColumn(Month, "Month Name", each Text.Start(Date.MonthName([Attribute]),3), type text),
    //Concatenate Year, Month number, and Month Name using "-" as delimiter and sort in order A-Z
    MergedColumns = Table.Sort(Table.CombineColumns(Table.TransformColumnTypes(MonthName, {{"Year", type text}, {"Month", type text}}, "en-US"),{"Year", "Month", "Month Name"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Dates"),{{"Dates", Order.Ascending}}),
    //Remove Attribute
    RemovedOldDate = Table.RemoveColumns(MergedColumns,{"Attribute"}),
    //Return Date registers as fields again
    PivotedColumn = Table.Pivot(RemovedOldDate, List.Distinct(RemovedOldDate[Dates]), "Dates", "Value", List.Sum)
in
    PivotedColumn

请告诉我这是否对您有用!