Forum Discussion

ks1's avatar
ks1
Frequent Visitor
5 years ago
Solved

Grouping by three conditions

Hello,

 

I'd reatly appreciate some help with Power Query Grouping with data like that below, I'm trying to consildate/group to one row per ID, retaining the non-blank values in the Status and Result columns based on the latest dates and times, using the following heirachy of rules:

 

First: Non blank

Then: Latest / oldest 'Date' 

Then: Latest /oldest  date & time in the 'Record last updated' if there is more than one non-blank value on the same date

 

 

IDDateStatusResultRecord last updated
3390494422 Jul 20 Something else12 10 2020 18:29:31
3390494420 Jul 20This  12 10 2020 18:29:30
98741359704 Aug 20 Something else12 10 2020 18:30:18
98741359724 Jul 20 Something 12 10 2020 18:30:11
98741359722 Jul 20That 12 10 2020 18:30:00
98741359722 Jul 20This Something else12 10 2020 18:30:01
98741359719 Feb 20  12 10 2020 18:18:10
98741359719 Feb 20  12 10 2020 18:19:19
98741359706 Feb 20This Something ordinary12 10 2020 18:31:21
98741359706 Feb 20  12 10 2020 18:18:09
258321669424 Feb 20ThatSomething 12 10 2020 18:19:26
258321669419 Feb 20This Something else12 10 2020 18:19:25

 

 

I'm expecting to for the query to filter the above to this:

 

IDStatsResult
33904944This Something else
987413597This Something else
2583216694ThatSomething 

 

I'm new to this forum and tried posting this before but can't find it anywhere, so hopefully not cross-posted.

  • No. As I said earlier, null, blank/empty, and space are different. Here is the same code but I've replaced your <space> with ""

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZG7DoJAEEV/ZbK1xczs8lg6Gwtb7QgFRqIkCAmPwr93ARMgA0qyJAS4h3Nn4lhpbdFYY9RBMcO5K4DR3YO7LtUra595+YCsaDL3gBgI3XtGoDBiG2lSyWHJwIlxfeYNfFkyikPUhoEh7dnAfYMGjt1j9/81RhQKCJutEqsAkgCeN0jb1QIuirLAMjqW/18CpQNZOGW3qYQU6I8U2JGz7sjJ+1NOilf1PS/T+i3lKWIpP4dtyuMowV6omXzfmnF1M4th8j+255qwLyHzEezcQU/yVJJ8AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Date = _t, Status = _t, Result = _t, #"Record last updated" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Record last updated", type datetime}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type"," ","",Replacer.ReplaceValue,{"Status", "Result"}),
        #"Grouped Rows" = Table.Group(#"Replaced Value", {"ID"}, {{"All Rows", each _, type table [ID=nullable text, Date=nullable date, Status=nullable text, Result=nullable text, Record last updated=nullable datetime]}}),
        #"Added Status" = 
            Table.AddColumn(
                #"Grouped Rows", 
                "Status", 
                each
                Table.Max( 
                    Table.FromRecords(
                        {
                            Table.Max(
                            Table.SelectRows([All Rows], each [Status] <> ""),
                            each [Date]
                            )
                        }
                    ),
                    each [Record last updated]
                )[Status]
            ),
        #"Added Result" = 
            Table.AddColumn(
                #"Added Status", 
                "Result", 
                each
                Table.Max( 
                    Table.FromRecords(
                        {
                            Table.Max(
                            Table.SelectRows([All Rows], each [Result] <> ""),
                            each [Date]
                            )
                        }
                    ),
                    each [Record last updated]
                )[Result]
            ),
        #"Removed Other Columns" = Table.SelectColumns(#"Added Result",{"ID", "Status", "Result"})
    in
        #"Removed Other Columns"

     

17 Replies