Power query - query first of prioritized group

michiel_soede

New Member
Joined
Oct 7, 2009
Messages
15
All,


I have attached simplified data set.

AssetStatusPriorityTimestamp
ATerminate113-Oct-18
ANew413-Oct-18
BTerminate113-Oct-18
BChange313-Oct-18
CTerminate113-Oct-18
CChange313-Oct-18

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>



Is it possible in power query to select / query the first asset after I have sorted on priority? I can't simply select the status where I don't have the same transaction types for all assets (so for one asset I have new and terminated and for others ceased and new for example). Therefore I need to pick the first prioritized transaction per group of transaction for each asset.


Thanks for your help


Michiel
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Are you looking for something like this?


Code:
let
    // load your table
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpJLcrNzEssSQWyDUHYWNc/uUTX0EIpVgeiwi+1HEiaYMg5EdQNUuGckZiXDpI2xpB2JmiAMx4DYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Asset = _t, Status = _t, Priority = _t, Timestamp = _t]),
    
    
    ChangedType = Table.TransformColumnTypes(Source,{{"Asset", type text}, {"Status", type text}, {"Priority", Int64.Type}, {"Timestamp", type date}}),
    
    GrpTable = Table.Group( 
                    ChangedType, 
                    {"Asset"}, 
                    { {
                        "EntireTable", 
                        each Table.Sort( Table.RemoveColumns( _, {"Asset"} ), {"Priority", Order.Ascending}){0},
                        type record} 
                    },
                    GroupKind.Global
    ),
    ExpandedEntireTable = Table.ExpandRecordColumn(GrpTable, "EntireTable", {"Status", "Priority", "Timestamp"}, {"Status", "Priority", "Timestamp"})
in
    ExpandedEntireTable
 
Upvote 0
Thanks for your reaction. My understanding of M is still in a beginner's mode but if you say load your table:

Code:
[COLOR=#333333]Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpJLcrNzEssSQWyDUHYWNc/uUTX0EIpVgeiwi+1HEiaYMg5EdQNUuGckZiXDpI2xpB2JmiAMx4DYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Asset = _t, Status = _t, Priority = _t, Timestamp = _t]),
[/COLOR][COLOR=#333333][/COLOR]

What does that line exactly do and with which part do you then pull data from your actual source (in my case being source = TBL_SOURCE_LL_B2B_FVODA -> coming from another query)?

grateful for your response,

Michiel
 
Upvote 0
Great, I have it working (in the sense of spitting out data) but not 100% sure yet if it behaves exactly as I want!
I need to study it a bit and let you know but wanted to thank you already for your response!

Regards,

Michiel
 
Upvote 0
Hopefully you allow me to ask one more question.
After having studied it a bit I am still not sure about the exact working of the bold part:

- EntireTable is just a way to get the entire table in PowerQuery?
- Table.RemoveColumns makes sure the fields are removed for those rows that will not be included? But how does that now interacts with the fact that it is included in the first sort part?


Code:
let
    // load your table
    Source = TBL_SOURCE_LL_B2B_FVODA,


    
   // ChangedType = Table.TransformColumnTypes(Source,{{"Asset", type text}, {"Status", type text}, {"Priority", Int64.Type}, {"Timestamp", type date}}),
    
    GrpTable = Table.Group( 
                    Source, 
                    {"AssetId_New"}, 
                    { {
[B]                        "EntireTable", [/B]
[B]                        each Table.Sort( Table.RemoveColumns( _, {"AssetId_New"} ), {"NewColumn.Order", Order.Ascending}){0},[/B]
[B]                        type record} [/B]
                    },
                    GroupKind.Global
    ),
    ExpandedEntireTable = Table.ExpandRecordColumn(GrpTable, "EntireTable", {"Index", "Status", "NewColumn.Order", "Description", "ContractBeginDate", "ContractBeginDate_Proper", "Code", "ContractPeriod", "LeaseAmount"}, {"Index", "Status", "NewColumn.Order", "Description", "ContractBeginDate", "ContractBeginDate_Proper", "Code", "ContractPeriod", "LeaseAmount"})
in
    ExpandedEntireTable

thanks, where this will be very useful!
 
Last edited:
Upvote 0
Table.Group performs a grouping of Source by AssetID.
EntireTable is just a name I have given to the new column that is created to host the content of Source grouped by Asset.
So the step GrpTable will contain one column with the unique Assets and another column whose each individual cell contains a table only with rows of the corresponding asset. I am sorting this tables in the second column by Priority and taking only first row afterwards, essentially taking only the lowest priority per Asset ID
 
Upvote 0
Thanks. Would it also be possible to first sort on {"ContractBeginDate_Proper", Order.Descending} and then on {"NewColumn.Order", Order.Ascending} where simply adding that in between the query does not work.
 
Upvote 0
Sorry i forgot to add a comma. For everybody interested my final code now would now be:

Code:
let    // load your table
    Source = TBL_SOURCE_LL_B2B_FVODA,


    
   // ChangedType = Table.TransformColumnTypes(Source,{{"Asset", type text}, {"Status", type text}, {"Priority", Int64.Type}, {"Timestamp", type date}}),
    
    GrpTable = Table.Group( 
                    Source, 
                    {"AssetId_New"}, 
                    { {
                        "EntireTable", 
                        each Table.Sort( Table.RemoveColumns( _, {"AssetId_New"} ), {{"NewColumn.Order", Order.Ascending}, {"ContractBeginDate_Proper", Order.Descending}}){0},
                        type record} 
                    },
                    GroupKind.Global
    ),
    ExpandedEntireTable = Table.ExpandRecordColumn(GrpTable, "EntireTable", {"Index", "Status", "NewColumn.Order", "Description", "ContractBeginDate", "ContractBeginDate_Proper", "Code", "ContractPeriod", "LeaseAmount"}, {"Index", "Status", "NewColumn.Order", "Description", "ContractBeginDate", "ContractBeginDate_Proper", "Code", "ContractPeriod", "LeaseAmount"})
in
    ExpandedEntireTable
 
Upvote 0

Forum statistics

Threads
1,214,409
Messages
6,119,339
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top