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
Ricardofaster
Frequent Visitor

Simulate ROW Number as SQL Server to have latest date and corresponding value to that date

Hi, I need to have the the latest date and the corresponding Fee of a Customer table having many different customers and many rows for the same Customer. This can be done easily in SQL Server with ROW_NUMBER and then ordering It by the Latest Date. 

Input:

CustomerDateFee
A13/01/202010
A25/01/202015
B26/01/202012
B16/01/20207
B17/01/202013
C05/01/202020
C02/01/202025
C20/02/202015

Temporal

CustomerDateFeeRow Number
A13/01/2020102
A25/01/2020151
B26/01/2020121
B16/01/202073
B17/01/2020132
C05/01/2020202
C02/01/2020253
C20/02/2020151

Output:

CustomerDateFee
A25/01/202015
B26/01/202012
C20/02/202015

Thanks in advance

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @Ricardofaster ,

 

The code @CNENFRNL provided works well. 

If you don't want to change the code manually, just try this:

maxdate.gif

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
Icey
Community Support
Community Support

Hi @Ricardofaster ,

 

The code @CNENFRNL provided works well. 

If you don't want to change the code manually, just try this:

maxdate.gif

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

You don't need Row Number to do this... First, just use GROUP BY to get the latest dates for each customer and then JOIN the output to the original table on customer and the date. It'll pick up the rows you want.
CNENFRNL
Community Champion
Community Champion

Hi, @Ricardofaster , it's as easy as in SQL to process in PQ given there's in PQ an equivalent of Grougp By clause in SQL.

GroupBy.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Tc7BDcAgDAPAXfJGwjFKebcdA7H/GgUqgX/xJbLSmt2WzEuGZ4KYAdbT7wz1WP5Mv9S53dXr4arnZfk7Rmg9cZzqsZ3IYyXv9A8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, Date = _t, Fee = _t]),
    #"Changed Type" = Table.TransformColumns(Source,{{"Date", each Date.FromText(_, "fr")}, {"Fee", Number.From}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer"}, {{"temporal", each Table.Sort(_, {"Date", Order.Descending}){0}}}),
    #"Expanded temporal" = Table.ExpandRecordColumn(#"Grouped Rows", "temporal", {"Date", "Fee"}, {"Latest Date", "Fee"})
in
    #"Expanded temporal"

 

 


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!

Hi,

 

@CNENFRNL , @Icey Thank you very much, both solutions work.

 

I appreciate your help.

 

All the best.

Hi @CNENFRNL , 

 

Thanks for your response but with your solution I will only get the sum of the Fees by Employee. What I need is the unic value of the Fee corresponding with the latest date  by Customer.

Hi, @Ricardofaster , I've amended the code in line with your requirement. Pls try it again.


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!

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
Top Kudoed Authors