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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
Super User
Super User

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Fabric Monthly Update - May 2024

Check out the May 2024 Fabric update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors