Forum Discussion

Jays_D's avatar
Jays_D
Advocate II
2 years ago
Solved

Return value based on highest number in index column

Hi All,   New to PBI and could really use some help working this one out.   I have a table which has multiple rows, which can be the same account repeated a few times. A column which has a Status...
  • Vijay_A_Verma's avatar
    2 years ago

    Below is the sample code for this problem

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyNjQyVtJRCk/MLMnMSweyDJVidZAknPNzc1PzklNTgGwjsJSLq5uJqRkWPe4enuYWlhA9BTmpJalApjEuLaaoEkGleXkQCaAlsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t, Status = _t, Index = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Account", type text}, {"Status", type text}, {"Index", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Account"}, {{"All", each Table.AddColumn(_, "Last Status", (x)=> _[Status]{List.PositionOf(_[Index],List.Max(_[Index]))})}})[[All]],
        #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Account", "Status", "Index", "Last Status"}, {"Account", "Status", "Index", "Last Status"})
    in
        #"Expanded All"
  • dufoq3's avatar
    2 years ago

    Hi Jays_D,

     

    2 similar versions of code:

     

    Result

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyNjQyVtJRCk/MLMnMSweyDJVidZAknPNzc1PzklNTgGwjsJSLq5uJqRkWPe4enuYWlhA9BTmpJalwGSxa0MwKKs3Lg0gYK8XGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t, Status = _t, Index = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}}),
        v1_GroupedRows = Table.Group(#"Changed Type", {"Account"}, {{"All", each _, type table}, {"Last Status", each Table.Max(_, "Index")[Status], type table}}),
        v1_StepBack = #"Changed Type",
        v1_Ad_LastStatus = Table.AddColumn(v1_StepBack, "Last Status", each v1_GroupedRows{[Account = [Account]]}[Last Status], type text),
        v1_Ad_PrevStatus = Table.AddColumn(v1_Ad_LastStatus, "Previous Status", each try v1_GroupedRows{[Account = [Account]]}[All]{[Index = [Index]-1]}[Status] otherwise null,type text),
        v2_SingleStep = Table.Combine(Table.Group(#"Changed Type", {"Account"},
        {{"Added Statuses", each Table.AddColumn(Table.AddColumn(_, "Last Status", (x)=> Table.Max(_, "Index")[Status], type text), "Previous Status", (x)=> try _{[Index = x[Index]-1]}[Status] otherwise null, type text), type table}})[Added Statuses])
    in
        v2_SingleStep