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
Anonymous
Not applicable

Merge duplicate rows that are next to each other (based on sort)

Hi everyone!

 

I'm trying to find a way to merge duplicate rows based on Customer & Status, and keep only the earliest date. I managed to do that using Table.Buffer, however I also want to merge ONLY the Status rows that are directly next to each other. For example:

 

CustomerDateStatus

1

01.01.2020A
105.01.2020A
106.01.2020B
108.01.2020A
110.01.2020B

 

The result I want would be:

CustomerDateStatus
101.01.2020A
106.01.2020B
108.01.2020A
110.01.2020B

 

The result I get instead:

CustomerDateStatus
101.01.2020A
106.01.2020B

 

Any ideas on how to solve it? 🙂

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Hi, @Anonymous , the 3rd parameter of Table.Group func, i.e. GroupKind.Local, does the trick to your issue. Pls refer to the M code below,

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1AMiIwMjAyDHUSlWBypsil3YDEnYCSFsgVW1oQGq6lgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Date = _t, Status = _t]),
    #"Changed Type" = Table.TransformColumns(Source,{{"Date", each Date.From(_, "fr")}}),

    // The most juicy part of the solution
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Status"}, {{"Custom", each Table.Sort(_, {"Date", Order.Ascending}){0}}}, GroupKind.Local),
    // Yes, you're done! Or 99.99% done.

    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Status"}),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Removed Columns", "Custom", {"Customer", "Date", "Status"}, {"Customer", "Date", "Status"})
in
    #"Expanded Custom"

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

9 REPLIES 9
CNENFRNL
Community Champion
Community Champion

Hi, @Anonymous , the 3rd parameter of Table.Group func, i.e. GroupKind.Local, does the trick to your issue. Pls refer to the M code below,

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1AMiIwMjAyDHUSlWBypsil3YDEnYCSFsgVW1oQGq6lgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Date = _t, Status = _t]),
    #"Changed Type" = Table.TransformColumns(Source,{{"Date", each Date.From(_, "fr")}}),

    // The most juicy part of the solution
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Status"}, {{"Custom", each Table.Sort(_, {"Date", Order.Ascending}){0}}}, GroupKind.Local),
    // Yes, you're done! Or 99.99% done.

    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Status"}),
    #"Expanded Custom" = Table.ExpandRecordColumn(#"Removed Columns", "Custom", {"Customer", "Date", "Status"}, {"Customer", "Date", "Status"})
in
    #"Expanded Custom"

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Anonymous
Not applicable

Totally solved my problem! Thank you!!!

@CNENFRNL - where did you get info on what GroupKind.Local is doing? The M documentation is useless here

 

It seems to only group if the row above/below is the same. 

So 

edhans_0-1599577301536.png

would become below, where it only grouped the pair of 4's next to each other on rows 4 and 5. But I'd like to understand more about it before I rely on it. 

edhans_1-1599577340565.png

And while I'm here, I'm going to ask about the the 7th parameter keyEqualityComparers  in Table.NestedJoin. I've never found an answer as to what that does, and wondering if you have any clue? 😁

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
CNENFRNL
Community Champion
Community Champion

Hi, @edhans , I stumbled onto this blog in this regard,

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

 

Indeed, M is sooo... poorly documented by MS; official docs are next to useless! In most cases, we have to google those scattered caviar of M functions. Quite some of them are very powerful; but we can't make the best of them as MS didn't provide detailed, definitive guide on them.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Here is the original article that @ImkeF  referenced.

 

https://web.archive.org/web/20180430162358/https://pqfans.com/836.html

 

Seems to be very similar to what Partition Over does in TSQL.

 

Hi @edhans ,

I find this a very good article explaining it: https://blog.crossjoin.co.uk/2014/01/03/aggregating-by-local-groups-in-power-query/

 

Also, there is a 5th element in the function that can come in very handy sometimes: https://www.thebiccountant.com/2018/01/21/table-group-exploring-the-5th-element-in-power-bi-and-powe...

 

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

Anonymous
Not applicable

The first article is great!

lbendlin
Super User
Super User

Create a Conditional column that checks if the status in the current row is the same as the status in the previous row. Then filter based on that column.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIw1AMiIwMjAyDHUSlWBypsil3YDEnYCSFsgVW1oQGq6lgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Date = _t, Status = _t]),
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Check", each if [Index]=0 then false else [Status]=#"Added Index"[Status]{[Index]-1}),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Check] = false))
in
    #"Filtered Rows"
Greg_Deckler
Super User
Super User

@Anonymous I have an idea, call in @ImkeF and @edhans 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors