Forum Discussion

skopcak's avatar
skopcak
Helper I
9 years ago
Solved

In what order occurs catalog - Query Editor

In what order occurs catalog? Here I present what I needed to make   Date Katalog result 2017-02-05 10:45:00 10000 1 2017-02-05 23:16:00 10000 2 2017-02-06 23:06:00 50000 1 2...
  • MarcelBeug's avatar
    MarcelBeug
    9 years ago

    Oops....

    Below alternative code that looks more complicated, but in a test with 10,000 rows it completed in a few seconds while the original solution ran for a few minutes...

     

    Remark: contrary to the previous solution, these are all standard UI steps (except that I renamed every step), with only rather basic formulas entered in the 2 "AddColumn" steps. 

     

        OriginalSort = Table.AddIndexColumn(PreviousStep, "OriginalSort", 1, 1),
        SortedOnKatalog = Table.Sort(OriginalSort,{{"Katalog", Order.Ascending}, {"OriginalSort", Order.Ascending}}),
        Index0Added = Table.AddIndexColumn(SortedOnKatalog, "Index", 0, 1),
        Index1Added = Table.AddIndexColumn(Index0Added, "Index.1", 1, 1),
        Merged = Table.NestedJoin(Index1Added,{"Index"},Index1Added,{"Index.1"},"Prev",JoinKind.LeftOuter),
        PrevKatalog = Table.ExpandTableColumn(Merged, "Prev", {"Katalog"}, {"Prev.Katalog"}),
        IndicesRemoved = Table.RemoveColumns(PrevKatalog,{"Index", "Index.1"}),
        NewIndex0Added = Table.AddIndexColumn(IndicesRemoved, "Index", 0, 1),
        StartIndex = Table.AddColumn(NewIndex0Added, "StartIndex", each if [Katalog] <> [Prev.Katalog] or [Prev.Katalog] = null then [Index] else null),
        FilledDown = Table.FillDown(StartIndex,{"StartIndex"}),
        ResultAdded = Table.AddColumn(FilledDown, "Result", each 1 + [Index] - [StartIndex]),
        SortedOriginal = Table.Sort(ResultAdded,{{"OriginalSort", Order.Ascending}}),
        ColumnsRemoved = Table.RemoveColumns(SortedOriginal,{"OriginalSort", "Prev.Katalog", "Index", "StartIndex"})