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
rayza
New Member

Creating a Rank Column within nested groups with Power Query

Hi, I am having trouble creating a ranked colum inside a nested group. I am not sure my approach to is this problem is correct. 

 

Description: I have a Query that runs daily and appends the results to a file. For each CollectionDate I wish to group each computerDnsName, then sort by lastSeen Descending, then add a Rank column.  So if I were to Filter the Rank to "1" we would have the most recent lastSeen entry for each computer per CollectionDate. 

 

Example of expected output:

 

computerDnsName lastSeen CollectionDate GUID exposureLevel Rank
Workstation11913/07/2023 2:589-Aug-23287237c4cf4f4c0None1
Workstation119129/06/2023 3:569-Aug-23fdfe79636a1afd7None2
Workstation119127/06/2023 0:399-Aug-2326e05b46e31397bNone3
Workstation119126/06/2023 7:589-Aug-231598a46d5564412None4
Workstation11359/08/2023 0:409-Aug-232b7c781977cd4a7Medium1
Workstation113522/06/2023 2:379-Aug-23b0c89945cfa2f4bNone2
Workstation11352/03/2023 1:099-Aug-2346b83af427e0783None3
Workstation11913/07/2023 2:588-Aug-23287237c4cf4f4c0None1
Workstation119129/06/2023 3:568-Aug-23fdfe79636a1afd7None2
Workstation119127/06/2023 0:398-Aug-2326e05b46e31397bNone3
Workstation119126/06/2023 7:588-Aug-231598a46d5564412None4
Workstation11357/08/2023 23:558-Aug-232b7c781977cd4a7Medium1
Workstation113522/06/2023 2:378-Aug-23b0c89945cfa2f4bNone2
Workstation11352/03/2023 1:098-Aug-2346b83af427e0783None3
Workstation11913/07/2023 2:587-Aug-23287237c4cf4f4c0None1
Workstation119129/06/2023 3:567-Aug-23fdfe79636a1afd7None2
Workstation119127/06/2023 0:397-Aug-2326e05b46e31397bNone3
Workstation119126/06/2023 7:587-Aug-231598a46d5564412None4
Workstation11356/08/2023 23:367-Aug-232b7c781977cd4a7None1
Workstation113522/06/2023 2:377-Aug-23b0c89945cfa2f4bNone2
Workstation11352/03/2023 1:097-Aug-2346b83af427e0783None3
Workstation11913/07/2023 2:584-Aug-23287237c4cf4f4c0None1
Workstation119129/06/2023 3:564-Aug-23fdfe79636a1afd7None2
Workstation119127/06/2023 0:394-Aug-2326e05b46e31397bNone3
Workstation119126/06/2023 7:584-Aug-231598a46d5564412None4
Workstation113526/06/2023 1:214-Aug-232b7c781977cd4a7None1
Workstation113523/06/2023 0:164-Aug-2316b14e2f99a9d50None2
Workstation113522/06/2023 4:494-Aug-23e6faadd2aab8409None3
Workstation113522/06/2023 2:374-Aug-23b0c89945cfa2f4bNone4
Workstation113521/06/2023 6:054-Aug-238313b9cda1b4b3aNone5
Workstation11352/03/2023 1:094-Aug-2346b83af427e0783None6

 

I have followed the awesome guide on https://data-witches.com/2020/10/21/adding-a-row-rank-based-on-a-different-column-with-power-query/ to add a Rank for each computerDnsName based lastSeen date but I am struggling to understand how to iterate this for each CollectionDate. 

 

Sample of the Power Query :

 

 

 

 

 

 

    #"Grouped Rows" = Table.Group(#"Changed Type1", {"computerDnsName"}, {{"AllHostGroup", 
    each Table.AddIndexColumn(
        Table.Sort(_,{{"lastSeen", 
        Order.Descending}}), "Row Rank",1,1),
     type table [GUID=nullable text,
     computerDnsName=nullable text,
     lastSeen=nullable datetime, 
     exposureLevel=nullable text,  
     CollectionDate=nullable date,
     Row Rank=nullable number
     ]}
     }),
    #"Expanded AllHostGroup" = Table.ExpandTableColumn(#"Grouped Rows", "AllHostGroup", {"GUID", "computerDnsName", "lastSeen", "exposureLevel", "CollectionDate", "Row Rank"}, {"GUID", "computerDnsName.1", "lastSeen", "exposureLevel", "CollectionDate", "Row Rank"})
in
    #"Expanded AllHostGroup"

 

 

 

 

 

 

 Any advice appericated,

1 ACCEPTED SOLUTION
v-rongtiep-msft
Community Support
Community Support

Hi @rayza ,

Please refer to my pbix file to see if it helps you.

Create a custom column.

Table.RowCount(Table.SelectRows(Table.SelectRows(Table.Group(#"Changed Type with Locale", {"computerDnsName", "CollectionDate"}, {{"Data", each _}}),(x)=>x[computerDnsName]=[computerDnsName] and x[CollectionDate]=[CollectionDate]){0}[Data],(y)=>y[lastSeen]>=[lastSeen]))

vrongtiepmsft_0-1691728637883.png

 

How to Get Your Question Answered Quickly 

 

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

 

Best Regards
Community Support Team _ Rongtie

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

View solution in original post

1 REPLY 1
v-rongtiep-msft
Community Support
Community Support

Hi @rayza ,

Please refer to my pbix file to see if it helps you.

Create a custom column.

Table.RowCount(Table.SelectRows(Table.SelectRows(Table.Group(#"Changed Type with Locale", {"computerDnsName", "CollectionDate"}, {{"Data", each _}}),(x)=>x[computerDnsName]=[computerDnsName] and x[CollectionDate]=[CollectionDate]){0}[Data],(y)=>y[lastSeen]>=[lastSeen]))

vrongtiepmsft_0-1691728637883.png

 

How to Get Your Question Answered Quickly 

 

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

 

Best Regards
Community Support Team _ Rongtie

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.