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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
ExcelAthlete
Regular Visitor

Power Query: Filter for lowest value for each entry

Hi,

 

I have a list of documents with Actions associated with it (in Powerquery coming out of a .csv) and days that these actions have been assigned for. It might look something like this:

 

Document IDAction byDays
DOC 001Andy3
DOC 001Mike7
DOC 001Jon10
DOC 001Andy16
DOC 002Helen5
DOC 002Andy5
DOC 002Sophie10
DOC 003Kevin7
DOC 003Scott8
DOC 003Jon14
DOC 003Mark17

 

The relevant columns that I would have to sort by are "Document ID" & "Days" and there's more columns similar to the "Action by" one where I just need the data that is in this specific line that I want to pick out. What I would like to end up with is a list that shows me each row filtered for the most recently assigned actions for each document, together with the name (and other columns that I omitted) that they have been assigned to. Some of these will lead to multiple entries for the same document e.g. DOC 002:

 

Document IDAction byDays
DOC 001Andy3
DOC 002Helen5
DOC 002Andy5
DOC 003Kevin7

 

I was able to achieve sort-of what I was looking for with the "group by" function in Power Query by grouping per Document ID with a MIN column for both the "Days" & "Action by" column. Unfortunately this would only show one of the two entries for DOC 002.

 

Hope I could convey what I'm looking for and appreciate any input!

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

5 REPLIES 5
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1672024053632.png

 

I am trying to use the formual you provided in power query (which I am new too) to filter each row in the table by the value, "Consignment_Event__c" to locate the lowest number for the column "SH#" that is formated as a whole number.

 

Here is what the table looks like with all the assocated SH# to the Consignment Event in one table.  I need to locate the lowest SH# for each Consignment Event.  

 

IdConsignment_Event__cOrder_Recordtype__cSH DuplicatesSH#
a8H0a00000002xrEAAa7m0a000002mECuAAM0120a0000019UbiSH-0005123800051238
a8H0a00000002xsEAAa7m0a000002mEBwAAM0120a0000019UbjSH-0005123900051239
a8H0a00000002xtEAAa7m0a000002mDroAAE0120a0000019UbjSH-0005124000051240
a8H0a00000002xuEAAa7m0a000002mBlyAAE0120a0000019UbkSH-0005124100051241
a8H0a00000002xvEAAa7m0a000002mBlyAAE0120a0000019UbkSH-0005124200051242
a8H0a00000002xwEAAa7m0a000002mDxhAAE0120a0000019UbjSH-0005124300051243
a8H0a00000002xxEAAa7m0a000002mDxhAAE0120a0000019UbjSH-0005124400051244
a8H0a00000002xyEAAa7m0a000002mE03AAE0120a0000019UbjSH-0005124500051245

 

I am getting an error, "Expression.SyntaxError: Invalid Identifier." with the below formula.  Since I am new to Power Query Editor I am not sure where the error is.

 

First SH.png

Thanks!

What I ended up doing in the meantime is create a separate column in Excel, filling it with an "X" using the minifs() function. I will try to implement your solution since it is more elegant doing it in the Query and report back in case I can't get it to work.

Anonymous
Not applicable

You could just group by Document ID, and use an All Rows aggregation, name the column "Details", and then add a custom column to get each Table.Min based on the Days column, and then keep just the new column and expand it. So after grouping Document ID:

 

= Table.AddColumn(PriorStepOrTableName, "NewTable", each Table.Min([Details], "Days"))


Now keep just your NewTable column and expand it.


--Nate

 

jgeddes
Super User
Super User

You can try something like this...

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcvF3VjAwMFTSUXLMS6kEUsZKsTrIwr6Z2alAyhxN2Cs/D0gaGqAJQw0xNEMSNwIKeKTmpII0mKKJQ9WjCwfnF2RkpqJbYAwU8E4ty8xDcw9IPDg5v6QESFugiUPdaYIm7JtYlA0SBxoTCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Document ID" = _t, #"Action by" = _t, Days = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Document ID", type text}, {"Action by", type text}, {"Days", Int64.Type}}),
#"Filtered Table" = Table.AddColumn(#"Changed Type", "MinValue", ((x) => List.Min(Table.SelectRows(#"Changed Type", each [Document ID] = x[Document ID])[Days]))),
#"Added Custom" = Table.AddColumn(#"Filtered Table", "Matches", each if [MinValue] = [Days] then "Matches" else "noMatch"),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Matches] = "Matches")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"MinValue", "Matches"})
in
#"Removed Columns"

It isn't the most elegent, but it gets the job done... 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Helpful resources

Announcements
Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Top Solution Authors
Top Kudoed Authors