Indirect Formula in Power Query

MikeDBMan

Well-known Member
Joined
Nov 10, 2010
Messages
609
In Power Query I have a text column I will call "Formula" that contains the text "[Col1] + (([Col2] + [Col3]) *.05)" and I want to evaluate that text cell and make the caldulation that would be [Col1] + (([Col2] + [Col3]) *.05) and return that value in another column in Power Query. Can this be done? Theoretically there would be a different formula in each row of the "Formula" field so each calculation would be different. Thanks for any help!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
There is a function Expression.Evaluate() that should do what you want. You first must have the calculation in a column as text and then pass that text to this function.
 
Upvote 0
There is a function Expression.Evaluate() that should do what you want. You first must have the calculation in a column as text and then pass that text to this function.
I must be missing something (see attachment). I created a very simple model and the Eval column which contains the Expression.Evaluate just returns the text of the formula, not the results of the formula. What am I doing wrong? Thanks.
 

Attachments

  • Evaluate.jpg
    Evaluate.jpg
    44.7 KB · Views: 31
Upvote 0
No quotes in the Rules.Formula column and the row context doesn't work inside Expression.Evaluate so you need to replace the column references like below

1667489021817.png
 
Upvote 0
Unfortunately, none of my formulas are this simplistic. How would I nest things to evaluate multiple fields like this:

[TotalAmount]+[MfgOH]+[MOH]+[Fringe]+[EngLaborOH]) * [GAPRO]
 
Upvote 0
Unfortunately, none of my formulas are this simplistic. How would I nest things to evaluate multiple fields like this:

[TotalAmount]+[MfgOH]+[MOH]+[Fringe]+[EngLaborOH]) * [GAPRO]
And some of the formulas don't have the same number of fields. The above is one formula. In the next formula, it is

If [Category]="Outside Services" then 0 else [TotalAmount])+[MfgOH]+[MOH]+[Fringe]+[EngLaborOH]+[IntG&A]) * [FeeRate]
 
Upvote 0
This approach might work if your data set is not too big and your rule formulas are just if statements and simple math operators

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    myTextReplace = (headers as list, row as record, txt as text)=>
        List.Accumulate(headers, txt, (s,c)=> Text.Replace(s, "[" & c & "]", Text.From(Record.Field(row,c)))),
    Result = Table.AddColumn(Source, "Eval", each 
        try Expression.Evaluate(myTextReplace(Table.ColumnNames(Source), _, [Rules.Formula]))
        otherwise "Not supported")
in
    Result

Book2
ABCDEFGHIJK
1Col1Col2Col3Rules.FormulaCol1Col2Col3Rules.FormulaEval
2171323[Col1]+[Col2]*[Col3]171323[Col1]+[Col2]*[Col3]316
372115Number.Power([Col2],2)72115Number.Power([Col2],2)Not supported
472115[Col2]*[Col2]72115[Col2]*[Col2]441
512214[Col1]-[Col3]12214[Col1]-[Col3]-13
611186[Col1]*[Col2]*[Col3]11186[Col1]*[Col2]*[Col3]1188
712223if [Col1] = 2 then 3 else [Col2]12223if [Col1] = 2 then 3 else [Col2]222
8321if [Col2]-[Col1]>0 then 50 else 22.3321if [Col2]-[Col1]>0 then 50 else 22.322.3
9456List.Sum({1,2,3})456List.Sum({1,2,3})Not supported
10Will this work?[Col1] & [Col2] & [Col3]Will this work?[Col1] & [Col2] & [Col3]Not supported
11
Sheet2
 
Upvote 0
added #shared as the Expression.Evaluate environment and added quotes to to text values and the previously unsupported formulas work

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    myTextReplace = (headers as list, row as record, txt as text)=> 
        List.Accumulate(headers, txt, (s,c)=> 
            let x = Record.Field(row,c), y = if Value.Is(x, type text) then """" & x & """" else x in  
            Text.Replace(s, "[" & c & "]", Text.From(y))),
    Result = Table.AddColumn(Source, "Eval", each 
        Expression.Evaluate(myTextReplace(Table.ColumnNames(Source), _, [Rules.Formula]), #shared))
in
    Result

Book2
ABCDEFGHIJK
1Col1Col2Col3Rules.FormulaCol1Col2Col3Rules.FormulaEval
2171323[Col1]+[Col2]*[Col3]171323[Col1]+[Col2]*[Col3]316
372115Number.Power([Col2],2)72115Number.Power([Col2],2)441
472115[Col2]*[Col2]72115[Col2]*[Col2]441
512214[Col1]-[Col3]12214[Col1]-[Col3]-13
611186[Col1]*[Col2]*[Col3]11186[Col1]*[Col2]*[Col3]1188
712223if [Col1] = 2 then 3 else [Col2]12223if [Col1] = 2 then 3 else [Col2]222
8321if [Col2]-[Col1]>0 then 50 else 22.3321if [Col2]-[Col1]>0 then 50 else 22.322.3
9456List.Sum({1,2,3})456List.Sum({1,2,3})6
10Will this work?[Col1] & [Col2] & [Col3]Will this work?[Col1] & [Col2] & [Col3]Will this work?
11Will this work?Text.Combine({[Col1],[Col2],[Col3]}, "")Will this work?Text.Combine({[Col1],[Col2],[Col3]}, "")Will this work?
12
Sheet2
 
Upvote 0

Forum statistics

Threads
1,223,516
Messages
6,172,778
Members
452,477
Latest member
DigDug2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top