Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Create a new column by group

I have a dataframe like this group country type value a japan  AB 1000 a japan - AC AB 500 a Mexico - AC CD 630 a USA AB 1400 b japan  CD 1000 b japan - AC C...
  • AlienSx's avatar
    2 years ago
        Table.AddColumn(
            Source, "Custom", 
            each Text.Trim(
                if Text.EndsWith([country], "- AC") 
                then Text.Range([country], 0, Text.Length([country]) - 4) 
                else [country]
            )
        )
  • ThxAlot's avatar
    2 years ago
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUcpKLEjMUwAyHJ2AhKGBgYFSrA6ylIKugqMzTN4USdo3tSIzOR8m7ewCJMyMEdKhwY5wU02g2pKQLQTrgFuIkEI2ECEHNA7FJSgy2LVAHAh3BLLbkUyDONwAVRLZJhOwXCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [group = _t, country = _t, #"type" = _t, value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"group", type text}, {"country", type text}, {"type", type text}, {"value", Int64.Type}}),
        #"Extracted country" = Table.TransformColumns(#"Changed Type", {"country", each Text.Trim(Text.BeforeDelimiter(_,"-"))})
    in
        #"Extracted country"