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 =  {  {"One", "1"}, {"Two", "2"}} 
 in  
 [   nlist{0} {0} =   nlist{0} {1} ]

 

I presume to do with a list inside  a record as oppsed to single value ? 

Richrd

]

  • 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.

     

     

     

     

  • 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

     

  • 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.

     

     

5 Replies

  • 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.

     

     

     

     

    • Dicken's avatar
      Dicken
      Post Prodigy

      Thanks, 
      I like the record add filed approach, this came about as I was trying to create reecords from lists
      without using Record.FromList, Just to to see. 

  • 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

     

  • 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.

     

     

  • v-sshirivolu's avatar
    v-sshirivolu
    Community Support

    Hi Dicken ,
    Thanks for reaching out to the Microsoft Fabric Community Forum.

    Working Example:
    let
      nlist = { {"One", "1"}, {"Two", "2"} }
    in
      [ nlist{0}{0} = nlist{0}{1} ]

    This returns a record where the field name is a static identifier, and the field value is the corresponding value (here, "1").

    Non-Working Example:

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

    This example does not work because, in a record literal, the field name on the left side of the equals sign must be a static identifier, not an expression.

    Explanation:

    When constructing a record literal using [fieldName = expression], the fieldName must be a static identifier such as a, Name, or Value. Expressions are not allowed as field names.

    In the second example, nlist{0}{0} (which evaluates to "One") is used as a field name. However, because this is an expression, Power Query / M does not allow it in a record literal.

    The first example is valid because the field is defined with a static identifier (a), and only the value on the right side of the equals sign is generated from an expression.