Forum Discussion

Julian-K's avatar
Julian-K
Regular Visitor
4 years ago
Solved

Search Email Subject For Keywords, But Foldable

Hi! I want to search a mailbox (Microsoft Exchange) for mails with certain keywords in the subject.   Mailbox = Exchange.Contents("[email protected]"), Mails = Mailbox{[Name="Mail"]}[Data], ...
  • AlexisOlson's avatar
    4 years ago

    I'm not sure if it's possible to write the M for this so that it folds into a native Exchange query. You might need to at least download all of the Subject lines.

     

    I'm not sure if something like this will work but it might be worth a try.

    Mails = Exchange.Contents("[email protected]"){[Name="Mail"]}[Data],
    Keywords = List.Buffer(Keywords[Column1]),
    Subjects = List.Buffer(List.Distinct(Mails[Subjects])),
    FilteredSubjects =
        List.Buffer(
            List.Select(
                Subjects,
                (S) => List.AnyTrue(
                           List.Transform(Keywords, each Text.Contains(S, _))
                       )
            )
        ),
    Filtered = Table.SelectRows(Mails, each List.Contains(FilteredSubjects, [Subject]))

    Note that FilteredSujects might need to be a separate query and you just use the first and last lines of the above to attempt to create a foldable query.