Power Query: how to create a udf-function for equation criteria

anvg

Active Member
Joined
Feb 14, 2012
Messages
485
Hi
How to create a udf-function for equaton criteria?
It is my code for that, but one is not work at all. I received a message a udf-function is not allowed in this context
Code:
let
    source = {"part","part1","settings"},
    myEqual = (base, substr) =>
    let
       result = if Text.Contains(base,substr) then true else false
    in
       result,
    return = List.RemoveMatchingItems(source, {"part"},myEqual)
in
    return
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
If you want to use the function myEqual that takes in 2 Parameters (base and substr), these parameters must be passed to the function-call: myEqual([ParameterBase], [ParameterSubstr])

Also you should use a function for the equation criteria (Comparer.FromCulture):

= List.RemoveMatchingItems(source, {"part"}, Comparer.FromCulture("en",myEqual([Parameter1], [Parameter2])))

Not sure where the parameters shall come from at the moment, as you're starting from the list that is subject to the removal process.
It would be better if you start it from a table that contains the 2 parameters in 2 separate columns and then add a column in which you perform the List.RemoveMatchingItems-operation.
 
Upvote 0
Thank you for your response.
I am sorry, but maybe I do not understand anything. Comparer.FromCulture function returns a function with two parameters which are compared and that one passes to its output a logial value of that comparison.
And also Compare.FromCulture has a second parameter as a logical type not a function type.

Indeed I can solve that task in other manner.
Code:
let
    source = {"part","part1","settings"},
    poses = List.Positions(source),
    withNull = List.Transform(poses, each if Text.Contains(source{_},"part") then null else source{_}),
    return = List.RemoveNulls(withNull)
in
    return
But I want understand where is my mistake in myEqual definition? Or it is really not permited.
 
Upvote 0
Your function myEqual will return text for logical values (true or false). This is what the Comparer.FromCulture needs. But in order to return sth, the function needs to be fed with the parameters (base, substr).

The function List.RemoveMatchingItems requires the logical value as type any - as this means basically "I don't care" - it doesn't seem to mind being fed by a function instead. The documentation lead me to the Comparer.FromCulture: https://msdn.microsoft.com/en-us/library/mt253634.aspx
I couldn't find any other logical expression that worked here.

But neither true nor false delivered the desired result from your new example above:

A List.DoesNotContain can also be written like this: List.Select(source, each Text.Contains(_, "part")=false)
 
Last edited:
Upvote 0
Oh, I am silly. If I cannot remove unnecessarys then I can select essentials.
Thank you very mach!
 
Upvote 0

Forum statistics

Threads
1,224,154
Messages
6,176,731
Members
452,740
Latest member
MrCY

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