Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Conditional Merge

ello i have a query wich i am working on and I need to merge it depending on the value of [ID RS] if its certain values then merge it with a table called CCunicos and the other [ID RS] with another table called CONTRATOS using also another identifier named [ID_LOC]

my code looks like this:

Let 

....

(all the steps in the query)

.....

#"Custom"= if [ID RS]=39 or [ID RS]=41 or [ID RS]=46 or [ID RS]=48 or [ID RS]=55 or [ID RS]=56 or [ID RS]=58 or [ID RS]=70 or [ID RS]=71 or [ID RS]=74 or [ID RS]=75 or [ID RS]=83 or [ID RS]=88 or [ID RS]=91 then ( Table.NestedJoin(#"Columnas reordenadas16","ID RS",CCunicos,"ID RS","Prueba",JoinKind.LeftOuter))


else ( Table.NestedJoin(#"Columnas reordenadas16",{"ID RS", "ID_LOC"},CONTRATOS,{"company.externalCode", "location.externalCode"},"Prueba",JoinKind.LeftOuter))

in

 

#"Custom"

 

OBS: #"Columnas reordenadas16" is the last step before my attempt to solve it 

 

then i get this Error Expression.Error: There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?

Can you help me sort it out?

 

 

 

 

  • edhans's avatar
    edhans
    6 years ago

    That is a bit different than what I originally understood. I think a conditional merge is overcomplicating it.

     

    Just do two merges, then some if/then/else logic on nulls.

     

    See the attached file. This is in Excel because it was easier to get your tables loaded from a spreadsheet vs PBIs "Enter Data" screen.\

     

    The lower green table is what you want the result to be. See the queries in Power Query.

     

    I had to change the last record in your first table since you gave me all 71's in the ID RS field, which was a 100% match to the first table. I changed it to 93 and 300030001 for the ID_LOC field.

8 Replies

  • artemus's avatar
    artemus
    Microsoft Employee

    Your if statements needs to be in the context of a row. Each of your statements in [] refers to the column of the current row. Since you aren't adding a column or doing a filter you have no row context.

     

    More technically, The error means that the variable "_" is not defined. "_" is automatically defined by using the "each" keyword which is shorthand for (_) =>. In other words, "each" is a lamda function which takes 1 input.

  • edhans's avatar
    edhans
    Community Champion

    The following code will conditionally merge with table a or table b.

     

    = if varSeason = "X" then Table.NestedJoin(#"Changed Type", {"Item Number"}, PartAItems, {"Part A Item"}, "ItemWeights", JoinKind.LeftOuter) else Table.NestedJoin(#"Changed Type", {"Item Number"}, PartBItems, {"Part B Item"}, "ItemWeights", JoinKind.LeftOuter)

     

    My conditional tables are PartAItems and PartBItems. Notice I forced the merge column to be ItemWeights vs the normal table name so the expansion always works. Without working through each and every bit of syntax in your statement, it looks like you are incorrectly referring to fields as "field" vs {"field"} in a few places. so Table.NestedJoin isn't getting what it needs as a list in the right places in your first bit of code. The 2nd part looks more correct. So:

    Table.NestedJoin(#"Columnas reordenadas16","ID RS",CCunicos,"ID RS", "Prueba", JoinKind.LeftOuter))

    is the one that looks wrong.

    You should probably just manually do the merges separately, then copy and paste the generated Table.NestedJoin statement. Typing all of the brackets, squigley brackets, quotes, commas, etc. manually will drive one to insanity.😀

    Or copy and paste my code, then replace with your table and field names.

    • Anonymous's avatar
      Anonymous
      Not applicable

      i tried that solution but it didnt work i still get the same error

      • edhans's avatar
        edhans
        Community Champion

        Then please post sample data if possible, because it works here.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Sample Data, this is the query i am working on ( not showing every column bc i cant)

    Clave de PlazaEstatusID PUESTOID RSJIID_CTCentro de TrabajoLOCALIDADID_LOC
    83466821VACANTE15000477718345690128CERCOTEC 7 NORTE TIJUANA20040001
    MP86473401OCUPADA801004727186417411703PACIFICO ADMINISTRATIVOTIJUANA20040001
    MP86470551VACANTE801004727186417861703PACIFICO ADMINISTRATIVOTIJUANA20040001
    MP86473221VACANTE801004727186706891703PACIFICO ADMINISTRATIVOTIJUANA20040001
    MP86475151OCUPADA801004727186406471703PACIFICO ADMINISTRATIVOTIJUANA20040001
    MP86471341OCUPADA801004727186466441703PACIFICO ADMINISTRATIVOTIJUANA20040001

     

    So depending on the Value of [ID RS] (39,41,46,48,55,56,58,70,71,74,75,83,88,91) i need it to merge with this table  using ID RS as key

     

    ID  RS               CC 

    39CVN39010102
    41CVN41909001
    46CVN46010104
    48CVN48010101
    55BTL55909001
    56BTL56909001
    58CVN58010103
    70TVI70010101
    71CMS71909001
    74TVI74909007
    75CMS75010101
    83TTC83909001
    88TLC88909001
    91CCM91909001

     

    and if its not any of those values i need it to merge with this table using [ID RS] and [ID_LOC] as keys

     

    ID RSID LOCCC 
    93300030001CCM93010101
    93290050001CCM93010102
    5040030001CCM93010103
    93300440001CCM93010104
    43300470001CCM93010105
    30300710001CCM93010106
    31210850001CCM93010107

     

     

    • edhans's avatar
      edhans
      Community Champion

      That is a bit different than what I originally understood. I think a conditional merge is overcomplicating it.

       

      Just do two merges, then some if/then/else logic on nulls.

       

      See the attached file. This is in Excel because it was easier to get your tables loaded from a spreadsheet vs PBIs "Enter Data" screen.\

       

      The lower green table is what you want the result to be. See the queries in Power Query.

       

      I had to change the last record in your first table since you gave me all 71's in the ID RS field, which was a 100% match to the first table. I changed it to 93 and 300030001 for the ID_LOC field.

      • Anonymous's avatar
        Anonymous
        Not applicable

        thanks a lot! i didnt think of that way of doing it, if i change for example ID RS then the CC column will update right?