Dataview query to group tasks by tags

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.

Hello.

I have the MD belew:

SAB: %% fold %%

Colazione_SAB %% fold %%
– Colazione_SAB
  • #no_item Occhio di bue
    - Uova - [[tag_luogo_list|Luogo:]] #esselunga
  • #no_item Latte di riso
    - Latte di riso - [[tag_luogo_list|Luogo:]] #eurospin
  • #no_item Biscotti
    - Biscotti - [[tag_luogo_list|Luogo:]] #eurospin
Pranzo_SAB %% fold %%
– Pranzo_Primo_SAB
– Pranzo_Secondo_SAB
  • #no_item Tacchino a fette
    - Tacchino - [[tag_luogo_list|Luogo:]] #eurospin

  • #no_item Occhio di bue
    - Uova - [[tag_luogo_list|Luogo:]] #esselunga

– Pranzo_Contorno_SAB
  • #no_item Cetrioli, limone e olio
    - Cetrioli - [[tag_luogo_list|Luogo:]] #eurospin
    - limone - [[tag_luogo_list|Luogo:]] #esselunga
    - olio - [[tag_luogo_list|Luogo:]] #esselunga
– Pranzo_Frutta_SAB
  • Yogurth - [[tag_luogo_list|Luogo:]] #esselunga
Cena_SAB %% fold %%
– Cena_Primo_SAB
– Cena_Secondo_SAB
  • #no_item Treccia di mozzarella
    - Treccia di mozzarella - [[tag_luogo_list|Luogo:]] #migross

  • #no_item Stracchino
    - Stracchino - [[tag_luogo_list|Luogo:]] #migross

– Cena_Contorno_SAB
  • Zucchine - [[tag_luogo_list|Luogo:]] #esselunga

  • #no_item Rucola con pomodori e ceci
    - Rucola - [[tag_luogo_list|Luogo:]] #esselunga
    - Pomodori - [[tag_luogo_list|Luogo:]] #eurospin
    - Ceci - [[tag_luogo_list|Luogo:]] #migross

– Cena_Frutta_SAB
  • Banana - [[tag_luogo_list|Luogo:]] #esselunga

and I created the Dataview code below:

‘’'dataview
task
FROM “My Social Circle/:bust_in_silhouette: Magda”
WHERE !completed
AND
meta(section).subpath =“-- Colazione_SAB”
OR
meta(section).subpath =“-- Pranzo_Primo_SAB”
OR
meta(section).subpath =“-- Pranzo_Secondo_SAB”
OR
meta(section).subpath =“-- Pranzo_Contorno_SAB”
OR
meta(section).subpath =“-- Pranzo_Frutta_SAB”
OR
meta(section).subpath =“-- Cena_Primo_SAB”
OR
meta(section).subpath =“-- Cena_Secondo_SAB”
OR
meta(section).subpath =“-- Cena_Contorno_SAB”
OR
meta(section).subpath =“-- Cena_Frutta_SAB”

SORT tags

FLATTEN tags AS tag

GROUP BY tag

The result is the one below:

I have 2 problems:

  1. If a task is present more than one time, for example 2, Dataview show me it 2 times:
    In the screenshot you can see under the tag “#Esselunga” the Task “Uova” is shown 2 times.

    I would like to show it one time only. Is it possible to do it with Dataview Task ?

  2. I want to do not show the tag “#no_item”.
    I tried with che command:

    WHERE !contains(tags, “#no_item”)

    but it not works.

Can anyone help me ?

Thanks

A few comments to your issue. First of all that long combination of AND and OR might not work as you expect it to. Lets simplify it down to something like: !completed AND path = "Pranzo" OR path = "Frutta". It’s easy to think this should give you all tasks which aren’t completed and the path is either Pranzo or Frutta. It doesn’t… Check the following:

First: `= true AND false OR false`  
Second: `= true AND false OR true `  
Third: `= false AND false OR true `  

Before expanding the next section, try to evaluate each of those statements. One could argue that the first should be false since it’s not completed, and neither path is found. The second should be true since it’s not completed, and one path matces, and that the third should be false since that task would be completed.

The actual result, and how to fix it

The actual result, is that we get false, true, true as the response. This is due to AND having a higher precedence when evaluating the expression. This is the output:
image

In both the second and the third statement it evaluates the AND first, reducing those statements to false OR true since both true AND false and false AND false equals false. And then it evaluates the OR making false OR true be `true.

To fix this, you need to make sure to add parenthesis around the OR expression you want evaluated on the same level. So back to our example statement it needs to read: !completed AND ( path = "Pranzo" OR path = "Frutta" ).

Regarding the requirement to loose the second definition of the “Uowa” task. It’s defined twice in your list, so it kind of make sense to include it twice in the final list. It is possible to loose it, but it would require doing a second GROUP BY on the task text itself, and afterwards picking out only the key/unique tasks in that grouping.

This would complicate your query somewhat, and is that what you really want?

1 Like

Hi holroy.

I put those 9 conditions because under those headers there are the tasks.
I tried to insert in the command "meta(section).subpath =“ the upper header (to collect all the sub headers) but dataview couldn’t filter the tasks that in this case should be under 2 level.

In the code I posted, it filter all the task correctly. It works fine.
I have only 2 finished touches to have it perfect:

  1. I would to unique filter all the tasks. So then, if a task is the same as another, it shouldn’t display it twice.
    You told me that I could do it GROUPING BY on the task text itself, to have only one task per type.
    Have you some tricks to give me ?

  2. I would like to exclude from the display the tasks that have the tag “#no_item” so that I will not have tasks displayed under that tag.
    I tried the line code: … “WHERE !contains(tags, “#no_item”)” … but it doesn’t work.
    Is there a way to exclude it ?

Holroy,
you were right saying to take a look with parenthesis.
I put the code: … “!contains(tags, “#no_item”)
in the WHERE command with AND operator and now I can exclude the tag “#no_item”.
All the other line codes with the OR operator are now within the round parenthesis.

Remain now the problem of the duplicated task to exclude that I didn’t succeed to solve it.
Any advise to it ?

I might have eluded to something being slightly difficult to do using a normal query, as doing GROUP BY text would introduce another level in your task list. However, even if you managed to sort out the display, consider what would happen when you click to complete the task. Which of the non-unique tasks would you like to complete? And to get rid of the task from the query you’d actually need to click to complete every single duplicated version of the task…

So sorry for the slight misleading that this could be done easily, but as somewhat of a compensation I’m going to show you a query template which would identify duplicates of a task:

```dataview
LIST numberOfDuplicates
FLATTEN file.tasks as task
GROUP BY task.text
FLATTEN length(rows) as numberOfDuplicates
WHERE numberOfDuplicates > 1
SORT numberOfDuplicates desc
```

And if you wanted to also list the unique files holding those duplicates you could try this query:

```dataview
TABLE numberOfDuplicates, unique(rows.file.link)
FLATTEN file.tasks as task
GROUP BY task.text
FLATTEN length(rows) as numberOfDuplicates
WHERE numberOfDuplicates > 1
SORT numberOfDuplicates desc
```

PS! These queries are somewhat expensive, so they might take a little while to run if your vault is very large…

You are right holroy.
It’s a nonsense to have 2 tasks with same description and how to mark complete wich one!

I have used correctly TASKS plugin code and now I have grouped the tasks by Tags correctly.

Thanks.