Forum Discussion

Muertepelá's avatar
Muertepelá
Frequent Visitor
6 years ago
Solved

Rank by time range and product

Dear all, 

 

I have a simple table  with date colum and product, the products are manufactured by batches so I have several entries for product for diferents times range.  I would like to know how many times a product is manufactured  in a period of time, I put a example:

ProductDateRank expected
A16/01/2020 22:561
A17/01/2020 22:411
A18/01/2020 6:361
C18/01/2020 12:251
C18/01/2020 20:201
C19/01/2020 20:051
C20/01/2020 3:591
C20/01/2020 11:541
D20/01/2020 12:571
D23/01/2020 12:091
D23/01/2020 20:031
D24/01/2020 3:581
D24/01/2020 11:521
D24/01/2020 19:471
D25/01/2020 3:411
A25/01/2020 5:342
A25/01/2020 13:292
A25/01/2020 21:242
A26/01/2020 5:182
B26/01/2020 10:221
B26/01/2020 18:171
B27/01/2020 2:121
A27/01/2020 3:553
D31/01/2020 20:182
D01/02/2020 4:132
C01/02/2020 6:043
C01/02/2020 13:593
C01/02/2020 21:543
C02/02/2020 5:493
C02/02/2020 13:443
C02/02/2020 21:383
C03/02/2020 5:333
A03/02/2020 12:552
A03/02/2020 20:512
A04/02/2020 4:462
A04/02/2020 12:412
A04/02/2020 20:372
A05/02/2020 4:312

 

Any idea or suggestion it will be apreciated, 

Thanks in advanced

  • ImkeF's avatar
    ImkeF
    6 years ago

    Hi Muertepelá  

    that's a job for GroupKind.Local:

    Please paste this code into the advanced editor and follow the steps:

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdNLrsMwCAXQrVQZVypcwI6Zvc8uqu5/G3XaKuCKaY64BEju9+1nu27cbsQ3EOgCuLXtcf1Az6AcsJ9AzeVd8bcCw2EVgBwUMDJQVICih7iNCpjd9AX/XzDn6AGSgUYFR3MJ0KX5XsHRHCUM19TcclRaYgZz0QpYHKMCsCNVtBzF79f9XYHn2lHC7twD4uYEZ0SPvqzEzgGF8xI5djWfEj4V6iznBTM0J62Al5snwHnzFyCizHVUMKO0rJhRsgdIjhI5J09wfFdWwZzc4rSkeXJtFfDyRyWYUdIDLEfJrHg8AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Product = _t, Date = _t]),
        #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
        #"Grouped Rows" = Table.Group(#"Added Index", {"Product"}, {{"Partition", each _, type table [Product=text, Date=text]}}, GroupKind.Local),
        #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"Product"}, {{"Partition", each Table.AddIndexColumn(_, "Rank", 1,1)}}),
        #"Expanded Partition" = Table.ExpandTableColumn(#"Grouped Rows1", "Partition", {"Partition", "Rank"}, {"Partition.1", "Rank"}),
        #"Expanded Partition.1" = Table.ExpandTableColumn(#"Expanded Partition", "Partition.1", {"Date", "Index"}, {"Date", "Index"}),
        #"Sorted Rows" = Table.Sort(#"Expanded Partition.1",{{"Index", Order.Ascending}})
    in
        #"Sorted Rows"

     

     

    Used techniques:

    in step "Grouped Rows": https://blog.crossjoin.co.uk/2014/01/03/aggregating-by-local-groups-in-power-query/ 

    in step "Grouped Rows1": https://www.youtube.com/watch?v=-3KFZaYImEY 

     

5 Replies

    • Muertepelá's avatar
      Muertepelá
      Frequent Visitor

      HI

       

      Sorry there was a mistake on the previus table, apologies¡¡¡¡ Here the right one¡¡¡

       

      ProductDateRank expected
      A16/01/2020 22:561
      A17/01/2020 22:411
      A18/01/2020 6:361
      C18/01/2020 12:251
      C18/01/2020 20:201
      C19/01/2020 20:051
      C20/01/2020 3:591
      C20/01/2020 11:541
      D20/01/2020 12:571
      D23/01/2020 12:091
      D23/01/2020 20:031
      D24/01/2020 3:581
      D24/01/2020 11:521
      D24/01/2020 19:471
      D25/01/2020 3:411
      A25/01/2020 5:342
      A25/01/2020 13:292
      A25/01/2020 21:242
      A26/01/2020 5:182
      B26/01/2020 10:221
      B26/01/2020 18:171
      B27/01/2020 2:121
      A27/01/2020 3:553
      D31/01/2020 20:182
      D01/02/2020 4:132
      C01/02/2020 6:042
      C01/02/2020 13:592
      C01/02/2020 21:542
      C02/02/2020 5:492
      C02/02/2020 13:442
      C02/02/2020 21:382
      C03/02/2020 5:332
      A03/02/2020 12:554
      A03/02/2020 20:514
      A04/02/2020 4:464
      A04/02/2020 12:414
      A04/02/2020 20:374
      A05/02/2020 4:314

       

      I have a data set of thounsands,  The table is just for near a month, and in that month product A was manufactured 4 times, producto B was manufactured only once, product C was manufactured 2 times and product D was 2.  I would like to know how many times each product has been manufactured over time. 

       

      Thanks in advance

      • amitchandak's avatar
        amitchandak
        Icon for Super User rankSuper User

        Why for product A  16, 17,18 is 1 are we checking the sequence, when it becomes 2 when it becomes 3