Forum Discussion

Vishnu812's avatar
Vishnu812
Frequent Visitor
3 years ago
Solved

Power Bi dax query

Hi, I have table contains multiple values with same name in Id column and has corresponding rank value in Rank column. Need to remove the old rank values and keep the latest one. For example: Sam...
  • BA_Pete's avatar
    BA_Pete
    3 years ago

     

    No problem. Just include the new [Branch] column within the Group By step, so you group on both [Branch] and [ID].

     

    New example code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUUpMSgaSBkqxOsh8Qzi/orIKRb6gsAiFD5E3RNNvBOYnIcvHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Branch = _t, Id = _t, Rank = _t]),
        chgTypes = Table.TransformColumnTypes(Source,{{"Rank", Int64.Type}}),
    
    // Relevant steps from here ----->
        addIndex = Table.AddIndexColumn(chgTypes, "Index", 1, 1, Int64.Type),
        groupBranchId = Table.Group(addIndex, {"Branch", "Id"}, {{"data", each _, type table [Branch=nullable text, Id=nullable text, Rank=nullable number, Index=number]}}),
        addLatestRecord = Table.AddColumn(groupBranchId, "latestRecord", each Table.Max([data], "Index")),
        expandLatestRecord = Table.ExpandRecordColumn(addLatestRecord, "latestRecord", {"Rank"}, {"Rank"}),
        removeDataColumn = Table.RemoveColumns(expandLatestRecord,{"data"})
    in
        removeDataColumn

     

    New output:

     

    Pete