在Excel Power Query中添加唯一索引

我有一张桌子,看起来像

Date 9/4/2016 9/11/2016 9/18/2016 9/25/2016 10/2/2016 10/9/2016 10/16/2016 10/23/2016 10/30/2016 11/6/2016 11/13/2016 11/20/2016 11/20/2016 

我试图给'date列'分配唯一的索引值,但不能使用电源查询中的'添加自定义索引值',它不检查重复。 此外,我尝试了“Date.WeekOfYear”,它给出了基于年的数字,但我想分配从1到….唯一的数字为date

  Date Custom_weeknumber 9/4/2016 1 9/11/2016 2 9/18/2016 3 9/25/2016 4 10/2/2016 5 10/9/2016 6 10/16/2016 7 10/23/2016 8 10/30/2016 9 11/6/2016 10 11/13/2016 11 11/20/2016 12 11/20/2016 12 

任何帮助将是有益的,谢谢!

假设:

  1. 你的date是sorting的。

  2. 重复之后的行将从重复项+ 1中获得Custom_weeknumber。

然后你可以按date分组(新的列名如“DateGroups”和Oparation“All Rows”),添加索引列,展开“DateGroups”字段并删除“DateGroups”字段。

Excel中Power Query中创build的代码示例:

 let Source = Excel.CurrentWorkbook(){[Name="Dates"]}[Content], Typed = Table.TransformColumnTypes(Source,{{"Date", type date}}), Grouped = Table.Group(Typed, {"Date"}, {{"DateGroups", each _, type table}}), Numbered = Table.AddIndexColumn(Grouped, "Custom_weeknumber", 1, 1), Expanded = Table.ExpandTableColumn(Numbered, "DateGroups", {"Date"}, {"DateGroups"}), Removed = Table.RemoveColumns(Expanded,{"DateGroups"}) in Removed 

我会这样做(这似乎是我简​​单一些,我不喜欢嵌套表,除非绝对需要):

  1. 按date分组
  2. 可选:sorting
  3. 添加索引
  4. join源表

码:

  let //Source = Excel.CurrentWorkbook(){[Name="YourTableName"]}[Content], Source = #table(type table [Date = date], {{#date(2016, 10, 12)}, {#date(2016, 10, 13)}, {#date(2016,10,14)}, {#date(2016, 10, 14)}}), GroupBy = Table.RemoveColumns(Table.Group(Source, "Date", {"tmp", each null, type any}), {"tmp"}), //Optional: sort to ensure values are ordered Sort = Table.Sort(GroupBy,{{"Date", Order.Ascending}}), Index = Table.AddIndexColumn(Sort, "Custom_weeknumber", 1, 1), JoinTables = Table.Join(Source, {"Date"}, Index, {"Date"}, JoinKind.Inner) in JoinTables