Forum Discussion
Power Query Configurable / Dynamic If Statement
Hi All.
I'm attempting to create a configuration list that I can convert into a single statement and then have a custom column ingest that logic and evaluate if possible. The objective behind this is to allow conditions to be set outside of power bi as the rules and logics will change over time without requiring to touch the solution and make it more dynamic. I have a sample table below that holds some key information:
| Hypothesis | Condition | Conclusion | Result |
| if | [Vendor] = "Sample1" | then | "Sample" |
| if | [OrigOperatorId] = "brg" and [System] = "AP" | then | "Result1" |
| if | [System] = "AP" or [System] = "PO" | then | "APPO" |
I want to convert that into something like this:
if [Vendor] = "Sample1" then "Sample" else if [OrigOperatorId] = "brg" and [System] = "AP" then "Result1" else if [System] = "AP" or [System] = "PO" then "APPO" else ""
Finally passing that into a custom column that evaluates the entire string. So let's say if I have a sample table such as this:
| ID | Vendor | OrigOperatorId | System |
| 1 | Sample2 | S3 | Man |
| 2 | Sample1 | go | GrR |
| 3 | Sample3 | brg | AP |
| 4 | Sample4 | bee | AP |
The new conditional column should evaluate and return the expected result of:
| ID | Vendor | OrigOperatorId | System | Test |
| 1 | Sample2 | S3 | Man | |
| 2 | Sample1 | go | GrR | Sample |
| 3 | Sample3 | brg | AP | Result1 |
| 4 | Sample4 | bee | AP | APPO |
Is this even possible? I think it should be. Any help would be greatly appreciated!
ImkeF
Thanks artemus ,
that circumvents the combination of the forumula dependencies with the row context nicely.
Is this a general limitation when using Expression.Evaluate or do you think that the error is due to some specific settings within this sample?
hnguyen76 ,I've rewritten it slightly to make it easier to swap the sample code against your actual data.
Also, I've included a "List.Select"-statement to grab the correct value from evaluated conditions:let ConditionsFromExcelTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WykxT0lGKDkvNS8kvilWwVYhRCk7MLchJNYxRAkqUZKTmASmIkFKsDky9f1Fmun9BalFiSX6RZwpEX1JReoySQmJeikJ0cGVxSWouRNgxANmkoNTi0pwSQySj0NUq5BehGhDgj2yAY0CAP0J3SGpFiZ5zfl5JYmZesQbMGzoKcG8YxShpYvojFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Hypothesis = _t, Condition = _t, Conclusion = _t, Result = _t]), AddFunctionColumToConditions = let Source = ConditionsFromExcelTable, #"Changed Type" = Table.TransformColumnTypes(Source,{{"Hypothesis", type text}, {"Condition", type text}, {"Conclusion", type text}, {"Result", type text}}), GenerateFunctions = Table.AddColumn(#"Changed Type", "Function", each Expression.Evaluate("each if " & [Condition] & " then """ & [Result] & """ else null", [Text.Contains = Text.Contains, Comparer.OrdinalIgnoreCase = Comparer.OrdinalIgnoreCase])) in GenerateFunctions, SourceData = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQpOzC3ISTUCsYyBhG9inlKsTrSSEVwKpCg9H0i4FwWBpYzhUiBWUlE6kHQMAEuZwKVArKTUVKhULAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Vendor = _t, OrigOperatorId = _t, System = _t]), #"Changed Type" = Table.TransformColumnTypes(SourceData,{{"Vendor", type text}, {"OrigOperatorId", type text}, {"System", type text}}), Evaluate = Table.AddColumn( #"Changed Type", "Custom", each List.First(List.Select(List.Transform(AddFunctionColumToConditions[Function], (fn) => fn(_)), each _ <> null)) ) in Evaluate
20 Replies
- ImkeFCommunity Champion
- v-xuding-msftCommunity Support
Hi hnguyen76 ,
You could create a conditional column. When you want to change If Statement, you just need to click that step rather than change your code manually in Advanced Editor.
For more informations, you could reference the blog to have a try.
https://radacad.com/conditional-column-in-power-bi-using-power-query-you-can-do-anything
- hnguyen76Resolver II
Hi v-xuding-msft ,
The idea is to develop a solution and make it "touchless". The condition changes overtime and we would like to allow a product owner to create his/her own hypotheses/conclusions to be generated without altering anything within Power BI. You can assume that the users will not have the ability to may any modifications either large or small to a production model.
- v-xuding-msftCommunity Support
Hi hnguyen76 ,
Unfortunately, as I know, it is not supported. If you really need this feature implemented, you could submit an idea on https://ideas.powerbi.com/ideas/.