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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Last value power query

Please, a need help for the following problem.

 

I have 3 columns, Date, Code and Stallment. I need add two columns woth the following values:

1) The last value of Stallments. That is ok. I got it!

2) The last  value of Stallment that the column Code is equal zero (Code=0). That is the problem.

 

The answer would be like that.

DateCodestallmentLast stallmentLast stalment with code = 0
29/03/20225003/15  
30/04/2022005/1503/1505/15
01/05/202221 05/1505/15
25/05/2022006/15  
30/05/20225005/1506/1506/15
28/06/20225006/1505/1506/15
26/07/2022008/1506/1508/15
29/07/20225007/1508/1508/15
12/08/202233 07/1508/15
25/08/20226209/15 08/15
1 ACCEPTED SOLUTION
edhans
Community Champion
Community Champion

Ok. Still not 100% sure, 😁 but is this what you want?

edhans_1-1673383191454.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZdDBCsMgDAbgd/HckRgb1x53Ley0o/Sww9hljDHooW+/qjXVDESCfn8ihmBoBHRASGQ6w7htlk/X+3cr4pq7YBwC9oWIWGu6n0RtgYslm7u0PjNiQFZNp+WtxvL/y9ba7kVqOAB6zUU1ucQ94FnNvyxPFYsnSY+VPpq/VK5wSzAU7Zx8QhMonyDQU768PT6SSHD+AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Code = _t, stallment = _t, #"Last stallment" = _t, #"Last stalment with code = 0" = _t]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"Date", type date}}, "en-BM"),
    #"Changed Type" = Table.TransformColumnTypes(#"Changed Type with Locale",{{"stallment", type date}, {"Last stallment", type date}, {"Last stalment with code = 0", type date}, {"Date", type date}}),
    LastCodeZero =
        Table.AddColumn(
            #"Changed Type",
            "Last Code 0",
            each 
                let varCurrentDate = [Date]
            in
                try
                    Table.Last(
                        Table.SelectRows(#"Changed Type", each [Code] = "0" and [Date] < varCurrentDate)
                    )[stallment]
                otherwise null
        )
in
    LastCodeZero

 

This all happens in the LastCodeZero step.

  1. It gets the "current" date from the Date column and stores it in varCurrentDate
  2. It filters the table from the previous step only where the code is 0 and the date is before varCurrentDate
  3. It then keeps the last record only (most recent)
  4. Then it gets the [stallment] field value.
  5. If there are no records in step #2 above, step #4 will return an error, so the entire thing is wrapped in a try/otherwise construct and returns null in that case.


Note: this will work fine for a few hundred rows in Power Query, maybe a couple of thousand. After that, this really should be done in DAX where it could do it quickly over millions of rows.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 

 



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

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Row 3, "date"=12/08/2022, "code"=33, "Stallment"=blank, "Last stallment" = 05/15 (Value of prevew row of column "Stallment"...

 

row 9, "date"=01/05/2022, "code"=21, "Stallment"=blank, "Last stallment" = 07/15 (Value of prevew row of column "Stallment", "Last stallment with code = 0"=08/15 (the last value of column "Stallment" that the column "Code" =0 is the row 7)

 

edhans
Community Champion
Community Champion

Ok. Still not 100% sure, 😁 but is this what you want?

edhans_1-1673383191454.png

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZdDBCsMgDAbgd/HckRgb1x53Ley0o/Sww9hljDHooW+/qjXVDESCfn8ihmBoBHRASGQ6w7htlk/X+3cr4pq7YBwC9oWIWGu6n0RtgYslm7u0PjNiQFZNp+WtxvL/y9ba7kVqOAB6zUU1ucQ94FnNvyxPFYsnSY+VPpq/VK5wSzAU7Zx8QhMonyDQU768PT6SSHD+AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Code = _t, stallment = _t, #"Last stallment" = _t, #"Last stalment with code = 0" = _t]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"Date", type date}}, "en-BM"),
    #"Changed Type" = Table.TransformColumnTypes(#"Changed Type with Locale",{{"stallment", type date}, {"Last stallment", type date}, {"Last stalment with code = 0", type date}, {"Date", type date}}),
    LastCodeZero =
        Table.AddColumn(
            #"Changed Type",
            "Last Code 0",
            each 
                let varCurrentDate = [Date]
            in
                try
                    Table.Last(
                        Table.SelectRows(#"Changed Type", each [Code] = "0" and [Date] < varCurrentDate)
                    )[stallment]
                otherwise null
        )
in
    LastCodeZero

 

This all happens in the LastCodeZero step.

  1. It gets the "current" date from the Date column and stores it in varCurrentDate
  2. It filters the table from the previous step only where the code is 0 and the date is before varCurrentDate
  3. It then keeps the last record only (most recent)
  4. Then it gets the [stallment] field value.
  5. If there are no records in step #2 above, step #4 will return an error, so the entire thing is wrapped in a try/otherwise construct and returns null in that case.


Note: this will work fine for a few hundred rows in Power Query, maybe a couple of thousand. After that, this really should be done in DAX where it could do it quickly over millions of rows.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 

 



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

Thank you very much!

edhans
Community Champion
Community Champion

Glad I was able to assist @Anonymous 



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
edhans
Community Champion
Community Champion

I don't understand your answer. Why is the last "stallment" for 5/15 code 50 6/15, but for 5/15 code 0 it is 3/15? Explain the logic a bit.



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

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors