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

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
Solution Specialist
Solution Specialist

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


[Tip] Keep CALM and DAX on.
[Solved?] Hit “Accept as Solution” and leave a Kudos.
[About] Chiel | SuperUser (2023–2) |

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.