Forum Discussion

Ketil_F's avatar
Ketil_F
New Member
1 year ago
Solved

How can I compare values fromdifferent records

I have a table with 3 columns that contains - name of a player - date for a practice session - a score for this practise session I want to add a forth column to indicate which dates a personal be...
  • dufoq3's avatar
    1 year ago

    Hi Ketil_F, check this:

     

    Output

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXJJTVYwNAIyFCwNlGJ1kASNQYIWpqiCpmBBCxRBIzOwdhNUQXOwSmOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Date = _t, Score = _t]),
        ChangedType = Table.TransformColumnTypes(Source,{{"Score", type number}}),
        GroupedRows = Table.Group(ChangedType, {"Name"}, {{"T", each 
            [ a = List.Buffer([Score]),
              b = List.Generate(
                        ()=> [ x = 0, y = a{x}, max = y, z = "X" ],
                        each [x] < List.Count(a),
                        each [ x = [x]+1, y = a{x}, max = List.Max({[max], y}), z = if y > [max] then "X" else null ],
                        each [z] ),
              c = Table.FromColumns(Table.ToColumns(_) & {b}, Value.Type(Table.FirstN(_, 0) & #table(type table[New best score=text], {})))
            ][c], type table}}),
        CombinedT = Table.Combine(GroupedRows[T])
    in
        CombinedT
  • p45cal's avatar
    1 year ago

    Here's one without an index if the date column contains proper dates:

     

     Table.AddColumn(ChangedType, "New Best Score", each if [Score]>List.Max(Table.SelectRows(ChangedType, (x)=>x[Date] < [Date] and x[Name]=[Name])[Score] &{0}) then "X" else "")

     

    where ChangedType is the previous step.

     

    For pasting into Advanced Editor, includes extended source data with an extra name, conversion of textual dates by adding year data and converting to proper dates:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXJJTVYwNAIyDi2wNFCK1UESNQaLWpiiippCRC1QRI3MICaYoIqaQ9Qag0WTsNqWhGybpRGqGIpdSVjtSsK0KxYA", BinaryEncoding.Base64), Compression.Deflate)),{"Name","Date","Score"}),
        AddedPrefix = Table.TransformColumns(Source, {{"Date", each "2024 " & _, type text}}),
        ChangedType = Table.TransformColumnTypes(AddedPrefix,{{"Date", type date}, {"Score", Int64.Type}}),
        AddedCustom = Table.AddColumn(ChangedType, "New Best Score", each if [Score]>List.Max(Table.SelectRows(ChangedType, (x)=>x[Date] < [Date] and x[Name]=[Name])[Score] &{0}) then "X" else "")
    in
        AddedCustom

     

     

    The order of the source data doesn't matter.

    It would probably better if there was a Table.Buffer in the query too.