Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Muertepelá
Frequent Visitor

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

1 ACCEPTED SOLUTION

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 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

5 REPLIES 5

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

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

Hi

Rank column is that I would like to obtain. For first dates and product A, is 1, for next dates of product A, becomes in 2 , for next dates of product A becomes in  3 and so for each product. I have entries for each product when product changes and  undeterminate time.

Thanks

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 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.