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
lukmtb08
Helper III
Helper III

Power Query - Column with latest status per ID

Hi, 

I have the first three columns of the following table available: 

Order IDStatus OrderFlag (needs to be created)
1001 
1002 
10031
2001 
20021


The column Flag needs to be added with Power Query. Only the max Status Order should be flagged. Others could be blank or 0. 

Thanks four your support.

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Create Blank Query and replace whole code with this one.

dufoq3_0-1705509136751.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwUNJRMlSK1YGxjZDYxmC2EZIaI5iaWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Order ID" = _t, #"Status Order" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order ID", Int64.Type}, {"Status Order", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Order ID"}, {{"Max Status Order", each List.Max([Status Order]), type nullable number}, {"All", each _, type table [Order ID=nullable number, Status Order=nullable number]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Status Order"}, {"Status Order"}),
    Ad_OrderFlag = Table.AddColumn(#"Expanded All", "Order Flag", each if [Status Order] = [Max Status Order] then 1 else null, Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(Ad_OrderFlag,{"Max Status Order"})
in
    #"Removed Columns"

 


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

2 REPLIES 2
dufoq3
Super User
Super User

Create Blank Query and replace whole code with this one.

dufoq3_0-1705509136751.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwUNJRMlSK1YGxjZDYxmC2EZIaI5iaWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Order ID" = _t, #"Status Order" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order ID", Int64.Type}, {"Status Order", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Order ID"}, {{"Max Status Order", each List.Max([Status Order]), type nullable number}, {"All", each _, type table [Order ID=nullable number, Status Order=nullable number]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Status Order"}, {"Status Order"}),
    Ad_OrderFlag = Table.AddColumn(#"Expanded All", "Order Flag", each if [Status Order] = [Max Status Order] then 1 else null, Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(Ad_OrderFlag,{"Max Status Order"})
in
    #"Removed Columns"

 


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

ChielFaber
Responsive Resident
Responsive Resident

Just used a DAX solution found here https://community.fabric.microsoft.com/t5/Desktop/Dynamic-Row-Numbers/m-p/52671 for something similar. This worked great for me. 

If you need a PowerQuery solution you could look into group by and add a max column. Use advanced option with group by.  Group by ID.  First aggregation is All rows and select new column with MAX IDStatus. 

In the next step expand the allrows column and you will have the max IDStatus attached to all the rows. The simply add a column with an if statement. If ID_status = Columnname for MAXIDSTATUS then 1 else 0

 

Hope this helps

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