SetFields function

Applies To
Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web

Use .SetFields() to add fields to a value or change its existing fields. For example, move an employee to a new team with =A2.SetFields("Team","Research"), or record where a number came from.

.SetFields() is a dot function: you call it on a value by typing a period after the value. It returns a new value and leaves the original unchanged. For an overview, see Data types and dot functions in Excel.

Note

These features are rolling out to Microsoft 365 Insiders in Beta Channel on Windows Version 2612 (Build 20715.20000) or later and Mac Version 16.117 (Build 26111815) or later, and in Excel for the web.

Syntax

.SetFields() returns a copy of value with the fields you name added or updated.

value.SetFields(field_name, field_value, [field_name, field_value], ...)

Argument Description
value (required) The value before the dot: an entity (including a Linked Data Type such as Stocks), a number, text, or a Boolean. Can be a cell reference, a formula, or an array of values.
field_name (required) The name of the field to add or update, as text, or an array or range of names.
field_value (required) The value of the field. If field_name is an array or range, use an array or range of values with the same shape.
field_name, field_value (optional) More pairs of names and values.

Notes

  • If a field already exists, .SetFields() replaces its value. Otherwise, it adds the field. Fields you don't name stay as they are.

  • Field names aren't case-sensitive. If value already has the field with different capitalization, .SetFields() replaces it, and the field takes the capitalization you typed.

  • What you get back depends on value:

    value .SetFields() returns
    An entity An entity with the new fields.
    A Linked Data Type, such as Stocks The same linked value with your fields added. Its own fields stay. After a refresh, the formula recalculates, so the result has the refreshed data and your fields.
    A number, text, or Boolean A value with metadata: the same value with fields attached. It behaves like its basic value.
    An array of values One result for each item.
  • Arithmetic uses the basic value. If A2 is a number with fields, =A2+1 returns a number with no fields. To keep the fields, copy them from A2: =(A2+1).SetFields(A2.GetFieldNames(),A2.GetFieldValues()).

  • .SetFields() changes only the fields you name. Other fields don't recalculate: if a Total field was calculated from Price, setting Price leaves Total as it was. To update Total, build the value again from new inputs.

  • Fields you add stay in the workbook. They aren't sent to the data provider.

  • A number typed before the dot needs parentheses, as in =(30).SetFields("Unit","g"), because Excel reads 30. as part of the number.

  • To change the display text, use .SetEntityText(). To remove a field, use .RemoveFields().

Examples

Copy the example data and paste it in cell A1 of a new Excel worksheet. If you need to, you can adjust the column widths to see all the data.

Example 1

Change one field and keep the original.

Field Value
Name Ada
Team Design
Location London
Formula Result
=ENTITY(B2,A2:A4,B2:B4) Ada
=A6.SetFields("Team","Research") Ada
=A7.Team Research
=A6.Team Design

A6 uses the ENTITY function to build an employee entity: a value that shows Ada in its cell and holds Name, Team, and Location fields.

A7 has the new team. A6, the original, still has Design. Name and Location are the same in both.

Two employee cards side by side: the original with Team Design, the copy with Team Research

Example 2

Add several fields from ranges. Each name in A4:A5 pairs with the value beside it.

Field Value
Name Ada
Team Design
Manager Grace
Start year 2024
Formula Result
=ENTITY(B2,A2:A3,B2:B3) Ada, with Name and Team
=A7.SetFields(A4:A5,B4:B5) Ada, with Manager, Name, Start year, and Team
=A8.Manager Grace

Example 3

Attach fields to a number. The result is still a number.

Label Value
Training hours 12.5
Formula Result
=B2.SetFields("Source","HR system", "Year",2026) 12.5
=A4.Source HR system
=ISNUMBER(A4) TRUE
=A4+1 13.5, with no fields
=(A4+1).Source #FIELD!, because the sum has no fields

In the grid, A4 looks like any other number. Its card shows its fields. To list its fields in cells, use =A4.GetFields().

Example 4

Add your own field to a Stock value. This example doesn't use pasted data.

  1. Type MSFT in A2, and then select Data > Stocks.
  2. Type 10 in B2.
  3. Enter these formulas in A4 and A5:
Formula Result
=A2.SetFields("Shares",B2) The MSFT stock, showing the same text as A2
=A4.Price*A4.Shares The value of 10 shares at the current price

The result is still a Stock value: it keeps every Stock field, such as Price, and adds Shares.

Errors

If a field name has no value after it, Excel doesn't accept the formula.

Error Cause
#VALUE! value can't hold fields, such as a Python object.
#VALUE! field_name and field_value are ranges or arrays of different shapes.
#VALUE! A field name is empty or longer than 255 characters.

See also