Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Sequential Assigned Number

I'm trying to assign "audit numbers" to order information per store.  I have the Order, Store Information in one table, and a list of employees and store assignments on another table.   I want to ...
  • lbendlin's avatar
    3 years ago

    Does the numbering have to start at 1?

     

    Table Stores:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyN7E0U9JRcgRiQ6VYHSQhJyA2QhVyBmJjVCEXIDaBCBmbmJuZArmuCLNgQm4Is2BC7gizYEIemGZ5ArGpUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Store = _t, Employee = _t, #"Employee #" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Store", Int64.Type}, {"Employee", type text}, {"Employee #", Int64.Type}})
    in
        #"Changed Type"

     

    Table Orders:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc+7EcQwCEXRXhQ7ED+BavG4/zbs8WywXKdn4D04z1GS4xii6XuN63hAq4PL6mBzdogvKAEtYWgJ5R2xO2TwjnCAo9Zz8TmsbEFLCX5xfTPsyYrfhHZID4BiIswBCys70KI5AQKoOXkHMswLwIzN51I5UTi97H/lugE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"order #" = _t, Store = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"order #", Int64.Type}, {"Store", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Counter", (k)=> Table.SelectRows(Stores,each [Store]=k[Store])),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each 1+Number.Mod([Index],Table.RowCount([Counter]))),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"order #", "Store", "Custom"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Custom", Int64.Type}})
    in
        #"Changed Type1"
  • lbendlin's avatar
    lbendlin
    3 years ago

    These are Power Query queries. How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

     

    If the seqence has to start at 1 then the Orders table would be slightly different:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Zc+7EcQwCEXRXhQ7ED+BavG4/zbs8WywXKdn4D04z1GS4xii6XuN63hAq4PL6mBzdogvKAEtYWgJ5R2xO2TwjnCAo9Zz8TmsbEFLCX5xfTPsyYrfhHZID4BiIswBCys70KI5AQKoOXkHMswLwIzN51I5UTi97H/lugE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"order #" = _t, Store = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"order #", Int64.Type}, {"Store", Int64.Type}}),
        AddCount = (t as table)=> let
        #"Added Index" = Table.AddIndexColumn(t, "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Counter", (k)=> Table.SelectRows(Stores,each [Store]=k[Store])),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom", each 1+Number.Mod([Index],Table.RowCount([Counter])))
         in
        #"Added Custom1",
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Store"}, {{"Count", each _, type table [#"order #"=nullable number, Store=nullable number]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each AddCount([Count])    ),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"order #", "Store", "Index", "Counter", "Custom"}, {"order #", "Store", "Index", "Counter", "Custom.1"}),
        #"Removed Other Columns1" = Table.SelectColumns(#"Expanded Custom",{"order #", "Store", "Custom.1"})
    in
        #"Removed Other Columns1"