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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
11097486
Helper I
Helper I

Power Query - Value from previous transaction (Other row)

Hello, I would like to be able to show the Date and Sales value from the previous transaction in Power Query M (Not DAX). The previous transaction is on another row. Thank you

 

11097486_1-1607695129097.png

 

 

4 REPLIES 4
mahoneypat
Employee
Employee

Here is one way to do it in the query editor.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kstV4jML8pW0lEy1DfUNzIwMgAxDZRidaKVfDMTczPBMsZwGVOwjI8jWNgEJmyErsEULmOMrMEcbo4hWBjFeguYpJlSbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [City = _t, Date = _t, Sales = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Date", type date}, {"Sales", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Descending}}),
    #"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each let thisdate = [Date], thiscity = [City] in Table.FirstN(Table.SelectRows(#"Changed Type", each [Date] < thisdate and [City] = thiscity),1)),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Date", "Sales"}, {"Date.1", "Sales.1"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Custom",{{"Date.1", "Prev Date"}, {"Sales.1", "Prev Sales"}})
in
    #"Renamed Columns"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hi Pat, close but not exactly. Your solution gives me the minimum date and not previous date. Any ideas? Thank you

 

11097486_0-1608051451012.png

 

11097486_1-1608051551021.png

 

 

 

 

 

11097486
Helper I
Helper I

Hello, not really what I'm looking for. I need something that will be able to find the previous date of the same category (in this case city). Thank you

v-yangliu-msft
Community Support
Community Support

Hi  @11097486 ,

 

Here are the relevant contents for you to check and hope to help you:

https://community.powerbi.com/t5/Desktop/Power-Query-Use-Previous-Rows-Value-in-Current-Calculation/...

https://community.powerbi.com/t5/Desktop/Power-Query-M-to-Display-Correct-Start-Date-Script-Attached...

https://docs.microsoft.com/en-us/powerquery-m/quick-tour-of-the-power-query-m-formula-language

 

Best Regards,

Liu Yang

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

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.