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
pakovic
Helper I
Helper I

2 last records in power query

Good morning, Thank you in advance for reading my case. I have a list of names and what these people have spent in several years.

is it possible to transform a table in powerquery to display the last 2 records for each name?

Logically, people who only have 1 record, show that record. Attached pbix example

attached pbix

 

https://www.dropbox.com/s/l8vhureetptsn0g/test%20%282%29.pbix?dl=0

 

Name    Mount   Year

andres12502020
andres21202021
andres45842022
andres54662023
felipe13652020
felipe12112021
jesus15202020
juan20002020
juan4522021
pepe10002019
pepe15002021
pepe65542022

transform to:

Name    Mount   Year

andres45842022
andres54662023
felipe13652020
felipe12112021
jesus15202020
juan20002020
juan4522021
pepe15002021
pepe65542022

 

pakovic_0-1655294694837.png

 

pakovic_1-1655294704058.png

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

let
    Origen = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY7RCoMwDEX/Jc8+JLG3zG8pfRCWwWSIKP6/rq2uhT2VnpN7kxBoscWoI2Hm81GWgWIXaNrHOf0LVk74msaFJeFxfq62fYWimX/Z550TvUctJtv2FIA2gbtJRZsV5SAHremvX0VqUQ71gMtY23qHx38B530WPcV4AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Mount = _t, Year = _t]),
    #"Tipo cambiado" = Table.TransformColumnTypes(Origen,{{"Mount", Int64.Type}}),
    Personalizado1 = Table.Buffer(Table.Sort(#"Tipo cambiado",{{"Name", Order.Descending}})),
    #"Grouped Rows" = Table.Group(Personalizado1, {"Name"}, {{"Record", each _, type table [Name=nullable text, Mount=nullable number, Year=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Last2", each Table.LastN([Record], 2)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Last2"}),
    #"Expanded Last2" = Table.ExpandTableColumn(#"Removed Other Columns", "Last2", {"Name", "Mount", "Year"}, {"Name", "Mount", "Year"})
in
    #"Expanded Last2"

View solution in original post

4 REPLIES 4
pakovic
Helper I
Helper I

¡wow! Thankyou very much... GSun!!  Your are incredible.

Anonymous
Not applicable

@HotChilli 's solution is better than mine

Anonymous
Not applicable

let
    Origen = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY7RCoMwDEX/Jc8+JLG3zG8pfRCWwWSIKP6/rq2uhT2VnpN7kxBoscWoI2Hm81GWgWIXaNrHOf0LVk74msaFJeFxfq62fYWimX/Z550TvUctJtv2FIA2gbtJRZsV5SAHremvX0VqUQ71gMtY23qHx38B530WPcV4AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Mount = _t, Year = _t]),
    #"Tipo cambiado" = Table.TransformColumnTypes(Origen,{{"Mount", Int64.Type}}),
    Personalizado1 = Table.Buffer(Table.Sort(#"Tipo cambiado",{{"Name", Order.Descending}})),
    #"Grouped Rows" = Table.Group(Personalizado1, {"Name"}, {{"Record", each _, type table [Name=nullable text, Mount=nullable number, Year=nullable text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Last2", each Table.LastN([Record], 2)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Last2"}),
    #"Expanded Last2" = Table.ExpandTableColumn(#"Removed Other Columns", "Last2", {"Name", "Mount", "Year"}, {"Name", "Mount", "Year"})
in
    #"Expanded Last2"
HotChilli
Super User
Super User

If you sort by Name and then Year, you can add an index column for each Name group. Try and find this out yourself (on this forum or there are plenty of examples out there on the web.  I will help if you get stuck).

Then filter the new Index column to keep only Index = 1 or 2

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.