Forum Discussion

thangdev's avatar
thangdev
Regular Visitor
2 years ago
Solved

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
  • Hi thangdev,

     

    Output

    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

6 Replies

  • dufoq3's avatar
    dufoq3
    Icon for Community Champion rankCommunity Champion

    Hi thangdev,

     

    Output

    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
    • thangdev's avatar
      thangdev
      Regular Visitor

      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 ? 

      • dufoq3's avatar
        dufoq3
        Icon for Community Champion rankCommunity Champion

        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.

  • Hi thangdev 

    Another solution

    = 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 

    • thangdev's avatar
      thangdev
      Regular Visitor

      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