Search code examples
azure-data-factoryetlcommon-expression-language

Azure Data factory expression


i saw this expression in one of the Pipeine , in the filter activity condition. can anyone help me understad this expresson used( you can refrase inorder to make it understandable). looks difficult to understand.

@if(equals(pipeline().parameters.FileName,'default'),endswith(toUpper(item().name),'.PDF'),
and(startswith(item().name,replace(string(pipeline().parameters.Filemane),'*.txt','')),
endswith(toUpper(item().name),'.PGP')))

Thanks

I dont have blocker , but i cannot understand the Expression. Just wanted to get some clarity what is the purpose of that code , what are they trying to achieve in that particualar filter condition in the ADF


Solution

  • If you use the above expression in the filter activity. It will filter the values of the array based on the FileName parameter.

    This is my sample array of file names:

    ['rakesh.pdf','correct1.pgp','wrong1.pgp','laddu.pdf','correct2.pgp','wrong2.pgp','virat.pdf','wrong3.pgp','correct3.txt']
    

    For you the array will be an array of objects. that's why in the above expression it's there as item().name. For me it's only item().

    Filter activity filters based on the condition. If the particular array item satisfies the condition(true/false) then it filters it.

     @if(equals(pipeline().parameters.FileName,'default'),endswith(toUpper(item().name),'.PDF'), and(startswith(item().name,replace(string(pipeline().parameters.Filemane),'*.txt','')), endswith(toUpper(item().name),'.PGP')))
    

    In the above expression, If the FileName parameter value is 'default', it filters the items which are ending with .PDF. if(condition,True case,else case) so in true case we are checking endswith() .PDF or not. If it is true, then filter condition is true and that particular array value will be filtered.

    else case

    and(startswith(item().name,replace(string(pipeline().parameters.Filemane),'*.txt','')), endswith(toUpper(item().name),'.PGP'))
    

    If the parameter value is correct*.txt(other than default). In this case it replaces the '*.txt' with empty strings('') and returns 'correct'. Then it checks(first condition) the whether the array value starts with 'correct' and endswith .PGP(second condition). If both conditions are true, then filter condition is true and array value is filtered.

    This is a sample demo:

    Array:

    enter image description here

    Filter with same condition:

    for me it's only item() not item().name.

    enter image description here

    If I take the parameter value as 'default'.

    enter image description here

    Filter output array (all .PDF files):

    enter image description here

    If I take the parameter value as 'correct*.txt'.

    enter image description here

    Filter output array(.PGP files starts with 'correct'):

    enter image description here

    NOTE: If your parameter value is only like 'correct.txt' then use '.txt' in the expression.