Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Transformation to Multiply and Divide in the Same Step

 

Guys, what is wrong with this script?

= Table.TransformColumns(#"Expanded Table Column1", {{"Avg Talk", each _ *[Interactions]/ 86400, type number}})

 

What I aim to is to multiply column: Avg Talk and column: Interactions and then divide the output by 86400, by transforming not by adding new column.

  • xzmiche's avatar
    xzmiche
    5 years ago

    Table.TransformColumns can access only each column in isolation. We have to use below walk around method (this solution is from power-query-transform-a-column-based-on-another-column 😞 

     

     

    = Table.FromRecords(Table.TransformRows(#"Changed Type", (r)=> Record.TransformFields(r,{{"Avg Talk", each _ * r[Interactions]/86400}})))

     

     

     

12 Replies

  • = Table.TransformColumns(#"Expanded Table Column1", each [Avg Talk], each [Avg Talk] *[Interactions]/ 86400, type number)
    • Anonymous's avatar
      Anonymous
      Not applicable

    • xzmiche's avatar
      xzmiche
      Icon for Resolver I rankResolver I
      = Table.ReplaceValue(#"Changed Type",each [Avg Talk],each [Avg Talk]*[Interactions],Replacer.ReplaceValue,{"Avg Talk"})
      • Anonymous's avatar
        Anonymous
        Not applicable

         

        Problem is with the replacing method that you could apply the step in 1 column only, I was hoping to apply in many columns thru 1 step.

  • Anonymous's avatar
    Anonymous
    Not applicable

    This is an order of operations issue. Try:

     

    = Table.TransformColumns(#"Expanded Table Column1", {{"Avg Talk", each (_ *[Interactions])/ 86400, type number}})

    • Anonymous's avatar
      Anonymous
      Not applicable

      I got this error

       

  • Anonymous's avatar
    Anonymous
    Not applicable

    Anonymous 
    You may just use Table.Replacevalue():

     

    = Table.ReplaceValue(#"Changed Type", each [Value], each [Value] * [Interaction]/86400,Replacer.ReplaceValue, {"Value"})

     

     

    Full code of the example:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjAzMTBQ0lEyNFCK1YkGUiAA4ivFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t, Interaction = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Interaction", Int64.Type}}),
        Custom1 = Table.ReplaceValue(#"Changed Type", each [Value], each [Value] * [Interaction]/86400,Replacer.ReplaceValue, {"Value"})
    in
        Custom1

     

     

    Paul Zheng _ Community Support Team
    If this post helps, please Accept it as the solution to help the other members find it more quickly.

  • @Magdoulin 
    You may just use Table.Replacevalue():

    Vpazhenmsft_6-1630648078174.png

     

    = Table.ReplaceValue(#"Changed Type", each [Value], each [Value] * [Interaction]/86400,Replacer.ReplaceValue, {"Value"})

     

    Vpazhenmsft_5-1630648063723.png

     

    Full code of the example:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjAzMTBQ0lEyNFCK1YkGUiAA4ivFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t, Interaction = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Interaction", Int64.Type}}),
        Custom1 = Table.ReplaceValue(#"Changed Type", each [Value], each [Value] * [Interaction]/86400,Replacer.ReplaceValue, {"Value"})
    in
        Custom1

     

     

    Paul Zheng _ Community Support Team
    If this post helps, please Accept it as the solution to help the other members find it more quickly.

  • @Magdoulin 
    You may just use Table.Replacevalue():

    Vpazhenmsft_6-1630648078174.png

     

    = Table.ReplaceValue(#"Changed Type", each [Value], each [Value] * [Interaction]/86400,Replacer.ReplaceValue, {"Value"})

     

    Vpazhenmsft_5-1630648063723.png

     

    Full code of the example:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WsjAzMTBQ0lEyNFCK1YkGUiAA4ivFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t, Interaction = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Interaction", Int64.Type}}),
        Custom1 = Table.ReplaceValue(#"Changed Type", each [Value], each [Value] * [Interaction]/86400,Replacer.ReplaceValue, {"Value"})
    in
        Custom1

     

     

    Paul Zheng _ Community Support Team
    If this post helps, please Accept it as the solution to help the other members find it more quickly.