Forum Discussion

Dicken's avatar
Dicken
Post Prodigy
11 months ago
Solved

Creating a record from list values

Can someone explain why this works;     = let nlist = { {"One", "1"}, {"Two", "2"}} , a = nlist{0} {0}, b = nlist{0} {1} in [ a = b ] but this does not ?  = let nlist = { {"...
  • tayloramy's avatar
    11 months ago

    Hi Dicken,  

    Great question! The short version is: [] creates a record literal, and on the left side of = inside a record the thing must be a field name, not an expression.

     

    What’s happening

    • let ... in [ a = b ]
      Inside [], a is treated as the field name (literally the text "a"), and b is an expression whose value becomes that field’s value. So this returns a record like [a = "1"]. The earlier variable named a is not referenced as a value here; it’s just the field’s label.

    • let ... in [ nlist{0}{0} = nlist{0}{1} ]
      Here, nlist{0}{0} is an expression, not a valid field name token. Record literals don’t allow expressions on the left of =. That’s why it errors. It’s not about “list inside a record” vs “single value”; it’s about field-name syntax.

    Do this instead, depending on your intent

    If you wanted a boolean comparison of the two items:

     
    let
        nlist = { {"One", "1"}, {"Two", "2"} }
    in
        nlist{0}{0} = nlist{0}{1}

    or, if you want the boolean inside a record:

     
     
    let
        nlist = { {"One", "1"}, {"Two", "2"} }
    in
        [ result = nlist{0}{0} = nlist{0}{1} ]

    If you wanted a record with a dynamic field name taken from the list (e.g., field name "One", value "1"):

     
    let
        nlist = { {"One", "1"}, {"Two", "2"} },
        key   = nlist{0}{0},    // "One"
        val   = nlist{0}{1},    // "1"
        rec   = Record.AddField([], key, val)
    in
        rec

     

     

     

    If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution.

     

     

     

     

  • Jai-Rathinavel's avatar
    11 months ago

    Hi Dicken , 

    • In [ a = b ], the field name a is treated as a static identifier, so it works.
    • In [ nlist{0}{0} = nlist{0}{1} ], you’re asking Power Query to calculate the label first, which it doesn’t allow in that spot.
    • If you want a dynamic field name, you must use Record.AddField.

     

    Thanks,

    Jai

     

  • Omid_Motamedise's avatar
    11 months ago

    Hi Dicken 

    It is because of general definition of a record.

    A rocrd should be including the field name (what is provided befor the equal sign) and then the field value. in your second case, the field name is define as nlist{0} {0} wich is not valied for field name.