Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Record.FieldCount: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Record.FieldCount returns the number of fields in a record. Use it when you need to validate that a record has the expected number of fields, filter out malformed records, or build dynamic logic that depends on record width. The function is simple: Record.FieldCount(record as record) as number. You can call it in the Power Query Editor from the formula bar, in a custom column, or inside a query step. No add-ins or special permissions are needed beyond Power Query itself, which is built into Excel, Power BI Desktop, and Power BI Service dataflows.

What You'll Need

  • Excel (Microsoft 365, Excel 2021, or Excel 2019) or Power BI Desktop with the Power Query Editor available.
  • A query that produces or references a record value, or a table with a column of type Record.
  • Basic familiarity with the Power Query Editor: the formula bar, the Applied Steps pane, and the Advanced Editor.

Step-by-Step

  1. Open the Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor (or edit an existing query). In Power BI Desktop, use Home > Transform data. Menu labels can differ slightly by version, device, or region.
  2. Get a record to test with. The simplest way is to create a blank query: Home > New Source > Blank Query, then in the formula bar type and press Enter. This produces a record with three fields. Name the query something like SampleRecord .
  3. Call Record.FieldCount. Create another blank query and enter = Record.FieldCount(SampleRecord) in the formula bar. The result is the number 3. If you prefer to work inside the first query, add a step: .
  4. Use it on a table column of records. If your table has a column named RecordColumn where each cell is a record, select the table, then go to Add Column > Custom Column. Enter Record.FieldCount([RecordColumn]) and click OK. Each row now shows how many fields its record contains.
  5. Filter or validate based on the count. Use the filter arrow on the new custom column to keep only rows where the count equals the expected number, or add a step like = Table.SelectRows(PreviousStep, each Record.FieldCount([RecordColumn]) = 3).
  6. Handle errors gracefully. If a cell is null or not a record, Record.FieldCount returns an error. Wrap the call with try ... otherwise: try Record.FieldCount([RecordColumn]) otherwise null. This keeps the query from failing on bad rows.
  7. Check the result and close. Verify the new column shows numbers, then use Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to load the query.

Common Mistakes

  • Passing a table instead of a record. Record.FieldCount expects a single record. If you pass a table, you get an error. Use Table.RowCount for tables, or convert a row to a record first.
  • Forgetting that null is not a record. A null value causes an error. Always wrap with try ... otherwise when the source column may contain nulls.
  • Confusing field count with column count. A record's fields are its named values; a table's columns are separate. Record.FieldCount counts fields inside one record, not columns in a table.
  • Using it on a list. Lists have items, not fields. Use List.Count for lists.
  • Expecting it to count nested fields. Record.FieldCount counts only the top-level fields. Nested records count as one field each.

Tips

  • Use Record.FieldCount to quickly inspect unknown JSON or API responses: expand a record, count its fields, then decide which to keep.
  • Combine with Record.FieldNames to list field names and Record.FieldCount to get the total, giving you a full picture of a record's shape.
  • For performance, avoid calling Record.FieldCount repeatedly inside large custom columns if the record shape is fixed; compute it once in a step instead.
  • Remember that field order does not affect the count; only the number of fields matters.

Troubleshooting

  • Error: "We cannot convert a value of type Table to type Record." You passed a table. Select a single row or use Table.First to get a record, then call Record.FieldCount.
  • Error: "We cannot convert a value of type Null to type Record." The value is null. Use try Record.FieldCount(value) otherwise null or filter out nulls first.
  • The count looks wrong. Check for hidden or nested fields. Expand the record in the editor to see all fields. Record.FieldCount counts only what is visible at the top level.
  • Custom column shows errors for some rows. Use the try/otherwise pattern and then filter out errors or replace them with a default.
  • Function not recognized. Ensure you are in the Power Query Editor, not a regular Excel cell. Record.FieldCount is a Power Query M function, not a worksheet function.

Frequently Asked Questions

What does Record.FieldCount return for an empty record?

It returns 0. An empty record [] has no fields, so the count is zero.

Can I use Record.FieldCount on a record from a JSON file?

Yes. After parsing JSON with Json.Document, each object becomes a record. You can call Record.FieldCount on any of them.

Does Record.FieldCount count fields with null values?

Yes. A field exists even if its value is null, so it is counted. The function counts field names, not non-null values.

Is there a similar function for tables?

Yes, use Table.ColumnCount to count columns in a table, or Table.RowCount to count rows.

Can I use Record.FieldCount in a calculated column in Power BI Desktop?

Record.FieldCount is an M function used in Power Query, not DAX. In Power BI Desktop, use it in the Power Query Editor before loading data.

What happens if the record has duplicate field names?

Power Query records cannot have duplicate field names; attempting to create one results in an error. So the count reflects unique field names only.