Need Help with Query with Dataview for Habit Tracking

Things I have tried

Hi all,

I have enjoyed reading many threads (in full) on how you use dataview. Especially those by @arminta. I have also enjoyed this guide on dataview. This is a great community.

What I’m trying to do

I am trying to leverage the solution provided by @mnvwvnm to query my weekly habits.

It was easily to successfully replicate his code for my needs.

My Habit Template:

  • AM:: Sleep | - | - | - | - | - | - | - | - |
  • AM:: Journal | - | - | - | - | - | - | - |
  • AM:: Lift | - | - | - | - | - | - | - |
  • AM:: Bike | - | - | - | - | - | - | - |

My query

TABLE
	map(AM, (z) => split(z, "\|")[0]) AS Habit,
	map(AM, (z) => split(z, "\|")[1]) AS M,
	map(AM, (z) => split(z, "\|")[2]) AS T,
	map(AM, (z) => split(z, "\|")[3]) AS W,
	map(AM, (z) => split(z, "\|")[4]) AS T,
	map(AM, (z) => split(z, "\|")[5]) AS F,
	map(AM, (z) => split(z, "\|")[6]) AS Sa,
	map(AM, (z) => split(z, "\|")[7]) AS Su
FROM #habits   
WHERE AM
SORT file.link ASC

This gives great group results for a week snapshot of my morning (AM) habits as a whole. But I have 10 other AM habits.

What I need help with

Now, I want to be able to query for individual habits - to better visualize individual trends over the year. What do I need to tweak in the query to be able to just search for my sleep data each week? Or is there a different way I should think about seeing the data for individual habits?

I suspect there is something relatively simple I can add to narrow down my results, but I am stuck, since I just started learning all things obsidian in the past month.

1 Like

Please forgive me if this is not okay, but I am bumping my question to hopefully get a response.

Hi.
I’m not sure if I understand the way you write your data and the wanted output…

  1. you add your data in only one note per week? a weekly note?
  2. your weekly note have a date in title?
  3. how you define the date interval? by folder?

For now try this

TABLE
	split(F, "\|")[0] AS Habit,
	split(F, "\|")[1] AS M,
	split(F, "\|")[2] AS T,
	split(F, "\|")[3] AS W,
	split(F, "\|")[4] AS T,
	split(F, "\|")[5] AS F,
	split(F, "\|")[6] AS Sa,
	split(F, "\|")[7] AS Su
FROM #habits 
WHERE AM
FLATTEN AM as F
WHERE contains(F, "Sleep")
SORT file.name ASC

You assumed correctly - one weekly note with a date in the title.

This is perfect - and after hours of searching and learning, I think I actually understand the logic of what you did. I couldn’t figure out how to properly use the flatten command. This is a huge help. Thank you so much!

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