Forum Discussion

KR300's avatar
KR300
Helper III
1 year ago
Solved

How to take Max String value based on Another Column which contains same values.

Hi Team,

Here based on the Isssue Id column we need to fetch max value of Fixversion and remove Other records.

Description: Issue ID - 2 is having multiple FixVersions ( SFP.R.11.7, SFP.R.11.8 & SFP.R.11.9 ), but i want to fetch only one record for each Issue ID with Max FixVersion like below

 

 

 

  • Hey!

    You can try an variation on this code. Som additional code may be needed to clean your data before.


    The code below code does the following:

    1. use a custom function to create a numeric combined value from main and sub version.
    2. Group the rows on issue to get the max
    3. Filter the rows so only the value that equals max will remain. 

     

    let
        Source = YOURDATA,
        filterEmpty = Table.SelectRows(Source, each [FixVersion] <> null and [FixVersion] <> ""),
    
        fn_VersionCombined = (vVersion as text) =>
        let
            split = Text.Split(vVersion, "."),
            main = split{2},
            sub = split{3},
            outcome  = Number.From(Text.Combine({main, if Number.From(sub) <= 9 then "0" & sub else sub}))
        in
            outcome,
        
        invoke_fnVersionCombined = Table.AddColumn(filterEmpty, "VersionCombined", each fn_VersionCombined([FixVersion]), Int64.Type),
        GroupRows = Table.Group(invoke_fnVersionCombined, {"Issue ID"}, {{"Max", each List.Max([VersionCombined]), type number}, {"Table", each _, type table [Issue ID=nullable number, FixVersion=nullable text, VersionCombined=number]}}),
        ExpandTable = Table.ExpandTableColumn(GroupRows, "Table", {"FixVersion", "VersionCombined"}, {"FixVersion", "VersionCombined"}),
        FilterRows = Table.SelectRows(ExpandTable, each ([VersionCombined] = [Max]))
        
    in
        FilterRows

     

    Hopefully this is usefull, if so consider accepting it as a solution to help other users!

    Good luck!

  • The code I supplied earlier seems to work with your new sample:

     

    Source

     

     

    Results

     

     

    By the way, if you want to change the name of the resultant column, merely edit the last line of code before the "in":

     

    Change:

     

    Table.ExpandRecordColumn(#"Grouped Rows", "Latest Version", {"FixVersion"})

     

    to something like:

     

    Table.ExpandRecordColumn(#"Grouped Rows", "Latest Version", {"FixVersion"},{"MaxVersion"})

     

  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi,

    Thanks for the solution KR300 , Chewdata , ThxAlot  and Ahmedx  offered, and i want to offer some more information for user to refer to.

    hello KR300 , you can refer to the follwing code in advanced editor in power query.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdBLCsAgDATQu2QtwbEf9QJdF7sU73+NfrDUMtk+EoaZWgXi5Nh2LQpokuaqhJHiQIWviu435YemkSJTYjIe4R+b+XNhWpkiU2LKTFcwGwwLhhlFYVTA1yH38j/q5QeDVxgWyN7JYcz0N8rtq7cT", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Issue ID" = _t, FixVersion = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Issue ID", Int64.Type}, {"FixVersion", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Select([FixVersion],{"0".."9"})),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", type number}}),
        #"Grouped Rows" = Table.Group(#"Changed Type1", {"Issue ID"}, {{"Max", each let a=List.Max([Custom])
    in List.Select([FixVersion],each Text.Contains(Text.Remove(_,"."),Text.From(a))){0}, type nullable text}})
    in
        #"Grouped Rows"

    Ouptut

     

    Best Regards!

    Yolo Zhu

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

  • let
        result = Table.Group(
            source_table, 
            "Issue ID", 
            {
                "FixVersion", (x) => List.Max(
                    x[FixVersion], 
                    null, 
                    (w) => ((nums) => Number.From(nums{0}) * 1000 + 
                        Number.From(nums{1})
                    )(List.LastN(Text.Split(w, "."), 2))
                )
            }
        )
    in
        result

16 Replies

    • Split the version into the letters and digits portion
    • Split the digits portion by the dot, then combine them to create a whole number
    • Group by Issue ID
    • Sort each group by letter and digits
    • Return the last value of each subgroup

     

     

    let
        Source = Table.FromColumns({
            {1,2,2,2,3,3,3,3},
            {"SFP.R.11.8","SFP.R.11.7","SFP.R.11.8","SFP.R.11.9","SFP.R.11.7","SFP.R.11.8","SFP.R.11.9","SFP.R.11.10"}},
                    type table[Issue ID=Int64.Type, FixVersion=text]),
    
    //Split digit and letter parts
        #"Split" = Table.AddColumn(Source, "SplitParts", each
            let 
                split = Text.Split([FixVersion],"."),
                letter = Text.Combine(List.Range(split,0,2),"."),
                digits = Number.From(split{2})*1000 + Number.From(split{3})
            in 
                [letters=letter, digits=digits],
                type [letters=text, digits=Int64.Type]),
       
        #"Expanded SplitParts" = Table.ExpandRecordColumn(Split, "SplitParts", {"letters", "digits"}, {"letters", "digits"}),
        #"Grouped Rows" = Table.Group(#"Expanded SplitParts", {"Issue ID"}, {
            
            {"Latest Version", each Table.Last(Table.Sort(_, {{"letters", Order.Ascending}, {"digits", Order.Ascending}}))
            
            
            , type [Issue ID=number, FixVersion=text, letters=text, digits=number]}}),
        #"Expanded Latest Version" = Table.ExpandRecordColumn(#"Grouped Rows", "Latest Version", {"FixVersion"})
    in
        #"Expanded Latest Version"

     

     

     

     

  • Chewdata's avatar
    Chewdata
    Responsive Resident

    Hey!

    You can try an variation on this code. Som additional code may be needed to clean your data before.


    The code below code does the following:

    1. use a custom function to create a numeric combined value from main and sub version.
    2. Group the rows on issue to get the max
    3. Filter the rows so only the value that equals max will remain. 

     

    let
        Source = YOURDATA,
        filterEmpty = Table.SelectRows(Source, each [FixVersion] <> null and [FixVersion] <> ""),
    
        fn_VersionCombined = (vVersion as text) =>
        let
            split = Text.Split(vVersion, "."),
            main = split{2},
            sub = split{3},
            outcome  = Number.From(Text.Combine({main, if Number.From(sub) <= 9 then "0" & sub else sub}))
        in
            outcome,
        
        invoke_fnVersionCombined = Table.AddColumn(filterEmpty, "VersionCombined", each fn_VersionCombined([FixVersion]), Int64.Type),
        GroupRows = Table.Group(invoke_fnVersionCombined, {"Issue ID"}, {{"Max", each List.Max([VersionCombined]), type number}, {"Table", each _, type table [Issue ID=nullable number, FixVersion=nullable text, VersionCombined=number]}}),
        ExpandTable = Table.ExpandTableColumn(GroupRows, "Table", {"FixVersion", "VersionCombined"}, {"FixVersion", "VersionCombined"}),
        FilterRows = Table.SelectRows(ExpandTable, each ([VersionCombined] = [Max]))
        
    in
        FilterRows

     

    Hopefully this is usefull, if so consider accepting it as a solution to help other users!

    Good luck!

  • pls try this code

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQp2C9AL0jM01LNQitWJVjJCFjLHFMKiyhIsZIyp0RhTozEBjYYGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Issue ID" = _t, FixVersion = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Issue ID", Int64.Type}, {"FixVersion", type text}}),
        #"Removed Duplicates" = Table.Distinct(#"Changed Type", {"FixVersion"}),
        #"Grouped Rows" = Table.Group(#"Removed Duplicates", {"Issue ID"}, {{"Count", each List.Max([FixVersion]), type nullable text}})
    in
        #"Grouped Rows"

     

    • KR300's avatar
      KR300
      Helper III

      Hi it helps. But If we have some more data like in the screenshot. Pls have a look.

      1. I need more help on this & how it can be done.
      2. If Issue ID is same, I want only one Issue ID with Max FixVersion Record
      3. Note: FixVersion from Least to Highest ( 8.1,8.2,....,8.10,8.11, 8.12..,   11.1, 11.2,....,11.10, 11.11, 11.12.., )
      • Chewdata's avatar
        Chewdata
        Responsive Resident

        Hey,
        Have you tried my solution yet? It should do just that. Same as the other solutions. Otherwise the problem you are facing is not yet clear to us.

    • ronrsnfld's avatar
      ronrsnfld
      Super User

      With your algorithm, if I add  SFP.R.10.22 to ID3, it gets returned as the latest version vs SFP.R.11.10

  • KR300's avatar
    KR300
    Helper III

    I have entere some more data. How to do in this case

    • ronrsnfld's avatar
      ronrsnfld
      Super User

      The code I supplied earlier seems to work with your new sample:

       

      Source

       

       

      Results

       

       

      By the way, if you want to change the name of the resultant column, merely edit the last line of code before the "in":

       

      Change:

       

      Table.ExpandRecordColumn(#"Grouped Rows", "Latest Version", {"FixVersion"})

       

      to something like:

       

      Table.ExpandRecordColumn(#"Grouped Rows", "Latest Version", {"FixVersion"},{"MaxVersion"})

       

      • KR300's avatar
        KR300
        Helper III

        But Power BI Consider SFP.R.8.9 as max string Compared to SFP.R.11.8 ( but this is the newest version right and Max Stiring). See Key -> SMAR-2363

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi,

    Thanks for the solution KR300 , Chewdata , ThxAlot  and Ahmedx  offered, and i want to offer some more information for user to refer to.

    hello KR300 , you can refer to the follwing code in advanced editor in power query.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bdBLCsAgDATQu2QtwbEf9QJdF7sU73+NfrDUMtk+EoaZWgXi5Nh2LQpokuaqhJHiQIWviu435YemkSJTYjIe4R+b+XNhWpkiU2LKTFcwGwwLhhlFYVTA1yH38j/q5QeDVxgWyN7JYcz0N8rtq7cT", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Issue ID" = _t, FixVersion = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Issue ID", Int64.Type}, {"FixVersion", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Text.Select([FixVersion],{"0".."9"})),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Custom", type number}}),
        #"Grouped Rows" = Table.Group(#"Changed Type1", {"Issue ID"}, {{"Max", each let a=List.Max([Custom])
    in List.Select([FixVersion],each Text.Contains(Text.Remove(_,"."),Text.From(a))){0}, type nullable text}})
    in
        #"Grouped Rows"

    Ouptut

     

    Best Regards!

    Yolo Zhu

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

  • let
        result = Table.Group(
            source_table, 
            "Issue ID", 
            {
                "FixVersion", (x) => List.Max(
                    x[FixVersion], 
                    null, 
                    (w) => ((nums) => Number.From(nums{0}) * 1000 + 
                        Number.From(nums{1})
                    )(List.LastN(Text.Split(w, "."), 2))
                )
            }
        )
    in
        result
  • KR300's avatar
    KR300
    Helper III

    Hi All, A lot of thanks for everyone. I will try what is the best way of workaround from all above options & Reply to all

     

    Thanks