How do I query fields by section, instead of array?

Things I have tried

Searching multiple posts, forums, and github for some help.

What I’m trying to do

I am trying to do what is shown in the first post of this Github ticket.

I have a file with multiple inline fields with the same label, but different values. I cannot split these into separate files; They need to remain in a single file. Yet I need to have a Dataview table which can display each one of them on a separate row.

One thing to mention that I am doing different than the github ticket/image above: Each of my inline fields are strung together on a single line, and therefore I want each of these comma separated inline fields to be its own column, with also one column being the non-inline text on that same line (if that’s not possible, I can make it an inline).

Example: (fields named the same, values change per row)

  • Plain text some [Field1:: Value1a], [Field2:: Value2a], [Field3:: Value3a], [Field4:: Value4a]
  • Plain text other [Field1:: Value1b], [Field2:: Value2b], [Field3:: Value3b], [Field4:: Value4b]

I’ve heard that Dataview itself is not (yet) capable of doing this on its own, so DataviewJS is required instead. I am currently at a loss as to how to start writing a query that performs this described workflow. Can anyone help me out please?

It seems like there is some confusion here, so let’s see if we can’t clear up some of the issues. Fields do belong to two scopes, either the page scope or the list (and task) scope. So fields within one scope are connected to that scope, and if multiple fields are defined they become a list.

Take the following example (do feel free to copy this to a note of your own, and see the result, and play around with it):

[field1:: one]

## Another section

[field1:: two]

## Third section

- [field2:: three],   [field3:: alpha]
- [field2:: four]  and [field2:: five], [field3:: beta]

[field3:: gamma]

### Query in page scope
```dataview
TABLE WITHOUT ID field1, field2, field3
WHERE file.name = this.file.name 
```

### Query using FLATTEN on list
```dataview
TABLE WITHOUT ID Item.field1, Item.field2, Item.field3, field1
FLATTEN file.lists as Item
WHERE file.name = this.file.name 
```

The output of the queries are as follows:

In the first query we see that there is one row (see the (1) after field1?), and each of the columns are lists of values, as when we ask for field1, field2 and field3 in the page scope, they’re all present there, and we don’t the origin of either of them.

This is what the thread you referred to was talking about. You can’t really tell from this output which section they belong to, or not.

However, in the second query we use the FLATTEN file.lists as item, and this allows us to see what each item in the list scope has defined. And then we list that for the first three columns, and there we see that there is no field1 defined in any of the lists. We also see that for the first line, the first bullet point in the list, there is only definition of field1 and field3, and they’re listed. For the second line, we see multiple definitions of field2, and they’re listed as a list again.

Finally, in the second query, we still can access fields from the page scope, so if we list field1, it’ll still repeat the list of all values for that field.

Hopefully, this clears up some of the confusion.

The real answer to your question

To summarise, as long as you define your fields within a list item, you can relate the various fields to each other, and treat them as one unit. And to achieve this, you typically do FLATTEN file.list as item, and you can get this output:

Query used:

```dataview
TABLE WITHOUT ID item.Field1, item.Field2, item.Field3, item.Field4
FLATTEN file.lists as item
WHERE file.name = this.file.name
```

(The WHERE clause is used to limit the query to the current file )

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.