Forum Discussion

hnguyen76's avatar
hnguyen76
Resolver II
6 years ago
Solved

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:

 

HypothesisConditionConclusionResult
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:

IDVendorOrigOperatorIdSystem
1Sample2S3Man
2Sample1goGrR
3Sample3brgAP
4Sample4beeAP

 

The new conditional column should evaluate and return the expected result of:

 

IDVendorOrigOperatorIdSystemTest
1Sample2S3Man 
2Sample1goGrRSample
3Sample3brgAPResult1
4Sample4beeAPAPPO

 

Is this even possible? I think it should be. Any help would be greatly appreciated!
ImkeF 

  • ImkeF's avatar
    ImkeF
    6 years ago

    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

    • hnguyen76's avatar
      hnguyen76
      Resolver II

      Hi ImkeF ,
      Thank you for the support. There may be a one-off where it's Text.Contains([SampleField], "ABC", Comparer.OrdinalIgnoreCase)

    • hnguyen76's avatar
      hnguyen76
      Resolver 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.