Forum Discussion
Creating a record from list values
- 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 recIf you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution.
- 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
- 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.
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.
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.