0

I have the following table in power query:

enter image description here

I'm trying to return the latest files for BOTH words "RAM" and "SoCal". In this case, the return files would be:

RR RAM 2024-07-08.xlsx

RR SoCal 2024-08-05.xlsx

How would I write a function to do this?

I tried:

= Table.SelectRows(GetDateFromFileName, let latest = List.Max(GetDateFromFileName[Text Between Delimiters]) in each [Text Between Delimiters] = latest and (Text.Contains([Name], "RAM") or Text.Contains([Name], "SoCal")))

But it did not work.

Thank you

3
  • Can you paste the image as copiable text. Is this Excel or PowerBI? Commented Aug 5, 2024 at 12:42
  • @davidebacci it's PowerQuery in Excel Binary RR RAM 2024-07-05.xlsx 7/5/2024 Binary RR RAM 2024-07-08.xlsx 7/8/2024 Binary RR SoCal 2024-07-01.xlsx 7/1/2024 Binary RR SoCal 2024-07-08.xlsx 7/8/2024 Binary RR SoCal 2024-08-05.xlsx 8/5/2024 Commented Aug 5, 2024 at 12:50
  • Update your question with the sample data in text format Commented Aug 5, 2024 at 13:32

1 Answer 1

0

Try:

  1. Add a custom conditional column depending if Name contains the string you want.
  2. Sort on the date column Text Between Delimiters DESCENDING
  3. Buffer the table to maintain the sort order.
  4. Remove duplicates on the conditional column

Similar to:

let
    ...
    #"Added Conditional Column" = Table.AddColumn(GetDateFromFileName, "Custom", each if Text.Contains([Name], "RAM") then "RAM" else if Text.Contains([Name], "SoCal") then "SoCal" else "ignore"),
    #"Sorted Rows" = Table.Sort(#"Added Conditional Column",{{"Text Between Delimiters", Order.Descending}}),
    TableBuffer = Table.Buffer(#"Sorted Rows"),
    #"Removed Duplicates" = Table.Distinct(TableBuffer, {"Custom"})
in
    #"Removed Duplicates"


Follow-up to the ask in the comments about doing similar but keeping the top 3 of each category:

  1. GroupBy on the category column while keeping all Rows.
  2. Transform the nested table by sorting it then taking the Top 3 rows.
  3. Expand the nested table

Example:

let
  ...,
  #"Added Conditional Column" = Table.AddColumn(GetDateFromFileName, "Custom", each if Text.Contains([Name], "RAM") then "RAM" else if Text.Contains([Name], "SoCal") then "SoCal" else "ignore"),
  #"Grouped rows" = Table.Group(#"Added Conditional Column", {"Custom"}, {{"Rows", each _, type nullable table[Content = nullable text, Name = nullable text, #"Text Between Delimiters" = nullable Int64.Type, Custom = any]}}),
  #"Sort and Take" = Table.TransformColumns(#"Grouped rows", {"Rows", each Table.FirstN(Table.Sort(_, {{"Text Between Delimiters", Order.Descending}}), 3) }),
  #"Expanded Rows" = Table.ExpandTableColumn(#"Sort and Take", "Rows", {"Content", "Name", "Text Between Delimiters"}, {"Content", "Name", "Text Between Delimiters"})
in
  #"Expanded Rows"
Sign up to request clarification or add additional context in comments.

6 Comments

Brilliant. Thanks so much. So basically you sort by date then remove duplicates by the custom column. Is this correct? What exactly does Table.Buffer do? I ready that its necessary to use that to make Table.Distinct act predictably, but is that the only reason? learn.microsoft.com/en-us/powerquery-m/table-buffer
Yes that's correct, remove duplicates keeps the first and removes the others hence order descending. See Remove Duplicates and Keep the Last Record with Power Query for why Table.Buffer is needed.
Thanks--One more question. What if you wanted to keep the top 3 files of each RAM or SoCal, but there could be different combination of dates of each category?
Have a look at the Follow-up bottom half of the updated answer above - see if that works for you.
Sam, am I able to use the UI to do the grouping? I'm having trouble following which options you're using for the grouping and what exactly is going on. Would you be able to provide more details? Thank you again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.