Forum Discussion

Mic1979's avatar
Mic1979
Post Partisan
1 year ago
Solved

Multiply two columns based on column header name

Dear all,   I need to multiply two columns based on the Header Name: Column1 contains %  Column2 contains € The code I used is this:   Result = Table.AddColumn ( RemoveDuplicate, "Result",...
  • dufoq3's avatar
    1 year ago

    Hi Mic1979, check this. This will also work if you have more than 1 % or € column (they will be multiplied all together)

     

    Output

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlVV0lFKBGJDAzBhYKAUqxMNZIDEk4DYCCRuChM3AosnA7ExTD1QIhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column1%" = _t, Column2 = _t, #"Column3€" = _t, Column4 = _t]),
        ChangedType = Table.TransformColumnTypes(Source,{{"Column1%", Percentage.Type}, {"Column3€", type number}, {"Column4", type number}}),
        Ad_Multiplied = Table.AddColumn(ChangedType, "Multiplied % and €", each
            [ a = Record.ToList(Record.SelectFields(_, List.Select(Record.FieldNames(_), (x)=> List.Contains({"%", "€"}, x, (y,z)=> Text.Contains(z, y))))),
              b = Expression.Evaluate(Text.Combine(List.Transform(a, (x)=> Number.ToText(x, "G", "en-US")), "*"))
            ][b], type number)
    in
        Ad_Multiplied