Forum Discussion
Power Query Configurable / Dynamic If Statement
- 5 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
ImkeF ,
Question: I've noticed that the function calls / evaluates my conditions query again after every file within source folder. I've attempted to add a List.Buffer or Table.Buffer but have been unsuccessful. So, is it possible to load the conditions once and reference that per row?
Hi hnguyen76 ,
not sure I understand.
Your conditions have to be evaluated on a row-by-row-basis.
Where do you see potential for skipping any evaluation?
- hnguyen765 years agoResolver II
Hi ImkeF ,
Sorry my previous post may have sounded confusing, hehehe. I didn't want to skip any evaluation but rather I wanted to find a way to only load the condition query just once in memory.
So to outline it a bit, I have about 100 files within a folder that I'm ingesting and the way it's currently evaulating / loading is as such:
1. ConditionsQuery.xlsx (3kb)
2. FileA (1kb)
3. ConditionsQuery.xlsx (1.2mb)
4. FileB (1kb)
5. ConditionsQuery.xlsx (2.89 mb)
6. FileC (1kb)
7. ConditionsQuery.xlsx (4.3 mb)
...
etc.
8. ConditionsQuery.xlsx (189 mb)As you can see, as each new file is being loaded, my conditions query gets called over and over again starting from 3kb and just keeps climbing up and up. So, the way I want it to ideally load would be:
1. ConditionsQuery.xlsx (3 kb)
2. FileA
3. FileB
4. FileC
5. FileD...
etc.
In any case, after tinkering with it a bit further, I kind of knew that I wanted to use some kind of buffer. After attempting to use List.Buffer() it didn't work out the way I had planned and Table.Buffer() at the EVALUATE step didn't work either. So I think by adding Table.Buffer() at the step before the EVALUATE applied step is working (i think) hehehe. I no longer see that it's continually loading the conditions query more than once.