2016-11-27 57 views
1

我有一个表,它看起来像在Excel中添加电源唯一索引查询

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.WeekOfYear”的基础上一年赋予的数字,但我想分配从1到唯一的数字....的日期一样

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 

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

回答

2

假设:

  1. 你的日期进行排序。

  2. 重复后的行将从重复获得Custom_weeknumber + 1

然后你就可以按日期(用新的列名,例如“DateGroups”和Oparation“所有行”)组,添加索引列,展开“DateGroups”字段并删除“DateGroups”字段。在电源查询在Excel中创建

代码示例:

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

我会做这种方式(这在我看来有点simplier,我不喜欢嵌套的表格,除非绝对必要):

  1. 集团有效期限栏
  2. 可选:排序
  3. 添加索引
  4. 加入源表

代码:

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