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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
thangdev
Regular Visitor

Get last status of item in Power Query (mcode)

Dear everyone, 

I have a query as below table 

 

IDItem
1A
2A
3A
4B
5B
6C
7C
8C
9D
10D
11D

 

I would like to add additional column to select last row of each item , something like below table, could you please assist for this ? 

 

IDItemselect
1Ano
2Ano
3Ayes
4Bno
5Byes
6Cno 
7Cno
8Cyes
9Dno
10Dno
11Dyes
1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @thangdev,

 

Output

dufoq3_0-1724244409515.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWJVjKCs4zhLBMgywnMMoWzzIAsZzDLHM6ygLMsgSwXMMvQAME0hDBjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Item = _t]),
    GroupedRows = Table.Group(Source, {"Item"}, {{"All", each 
        [ a = Table.AddIndexColumn(_, "IndexHelper",0,1),
          b = Table.AddColumn(a, "Select", (x)=> if x[IndexHelper] = List.Max(a[IndexHelper]) then "yes" else "no", type text),
          c = Table.RemoveColumns(b, {"IndexHelper"})
        ][c], type table}}),
    Combined = Table.Combine(GroupedRows[All])
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

6 REPLIES 6
slorin
Super User
Super User

Hi @thangdev 

Another solution

Capture d'écran 2024-08-21 184345.png

= Table.FromColumns(
Table.ToColumns(Your_Source) &
{List.Transform(
List.Zip({Your_Source[Item], List.Skip(Your_Source[Item])}),
List.IsDistinct)},
type table [ID = Int64.Type, Item = text, Select = logical]
)

Stéphane 

Hello @slorin 

Thank you for your assist, however I tried with your code, the result is also incorrect if the column "ID" is inorder , please see below picture for more details

 

thangdev_0-1724292011662.png

 

dufoq3
Super User
Super User

Hi @thangdev,

 

Output

dufoq3_0-1724244409515.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWJVjKCs4zhLBMgywnMMoWzzIAsZzDLHM6ygLMsgSwXMMvQAME0hDBjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Item = _t]),
    GroupedRows = Table.Group(Source, {"Item"}, {{"All", each 
        [ a = Table.AddIndexColumn(_, "IndexHelper",0,1),
          b = Table.AddColumn(a, "Select", (x)=> if x[IndexHelper] = List.Max(a[IndexHelper]) then "yes" else "no", type text),
          c = Table.RemoveColumns(b, {"IndexHelper"})
        ][c], type table}}),
    Combined = Table.Combine(GroupedRows[All])
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

hello @dufoq3 ,

I read the code, it seems that you added the index column then go row by row to check, that is correct with this instance, however If the ID column isn't order, does this code work correctly ? 

Hi, yes this code groups by [Item] and add internal index column. Then it check whether current internal row [IndexHelper] equals max of current group [IndexHelper] and if yes then "yes" else "no".

It will check row by row with respecting of last step row order.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi @dufoq3 ,

Thank you for your reply.  

 

I understood your solution, but it is only correct if the column ID was order. in the different situation, It's wrong, let consider below example, I have tested and It is not my expectation 

 

IDItem
1A
100A
3A
4B
5B
38C
7C
8C
9D
15D
11D

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Kudoed Authors