Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Writing Power Query (M language) functions that tailor table scope based on current row values

I have loaded a simple flat file I have pulled into a query. Fairly early on in the query's Applied Steps, the data looks like this:         . Note that the rightmost column is just a han...
  • BekahLoSurdo's avatar
    7 years ago

    I re-read this and have another idea that may be easier. I created a helper column to determine which scores should be evaluated:

     

    let
        Source = Excel.CurrentWorkbook(){[Name="Scores"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Specific Identifier", type text}, {"Score", Int64.Type}, {"Qualifier", type text}}),
        #"Added Qualified Scores" = Table.AddColumn(#"Changed Type", "Qualified Scores", each if [Qualifier] <> "Ignore" then [Score] else null),
        #"Added Outcome" = Table.AddColumn(#"Added Qualified Scores", "Outcome", each if (let group = [Specific Identifier] in
            List.Min(Table.SelectRows(#"Added Qualified Scores", each [Specific Identifier] = group) [Qualified Scores])) = [Score] then "Lowest" else "")
    in
        #"Added Outcome"

  • BekahLoSurdo's avatar
    BekahLoSurdo
    7 years ago

    Happy to help!

     

    As an aside, GroupKind.Local can be a powerful tool if you're grouping rows based on proximity to one another - especially if values are repeated later on but should be kept separate i.e.: 

     

    GroupValue
    A1
    A2
    A3
    B4
    B5
    B6
    A7
    A8
    C9
    C10

     

    In a table like this, if the two A groups ({1,2,3} and {7,8}) should be aggregated separately, GroupKind.Local would allow you to do that. 

     

    = Table.Group(#"Source Table", {"Group"}, {"Group Sum", each List.Sum([Value])}, GroupKind.Local)
    GroupGroup Sum
    A6
    B15
    A15
    C19

     

    When I re-read your post, I realized GroupKind.Local would have worked but wasn't necessary... but it's still a good tool to know about and one that someone just recently showed me so now I'm just trying to share the wealth :)