Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Create a new column by group

I have a dataframe like this

groupcountrytypevalue
ajapan AB1000
ajapan - ACAB500
aMexico - ACCD630
aUSAAB1400
bjapan CD1000
bjapan - ACCD0
bUSA - ACAB0
bUSA- ACCD0
bMexicoAB1500
aUSA - ACCD6000
aUSA- ACAB4000

 

And I would to create a new column removing AC in column country, because after I want sum value of Japan and Japan - AC, for example. The result would be like this

 

groupcountrytypevaluecountry  2
ajapan AB1000japan
ajapan - ACAB500japan
aMexico - ACCD630Mexico
aUSAAB1400USA
bjapan CD1000japan
bjapan - ACCD0japan
bUSA - ACAB0USA
bUSA- ACCD0USA
bMexicoAB1500Mexico
aUSA - ACCD6000USA
aUSA- ACAB4000USA
  •     Table.AddColumn(
            Source, "Custom", 
            each Text.Trim(
                if Text.EndsWith([country], "- AC") 
                then Text.Range([country], 0, Text.Length([country]) - 4) 
                else [country]
            )
        )
  • 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"

2 Replies

  •     Table.AddColumn(
            Source, "Custom", 
            each Text.Trim(
                if Text.EndsWith([country], "- AC") 
                then Text.Range([country], 0, Text.Length([country]) - 4) 
                else [country]
            )
        )
  • 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"