Bases: How to reproduce Dataview's "date(file.day)"?

What I’m trying to do

I want to convert a dataview query in my daily notes to an embedded base to display certain properties from the past week’s daily notes as a sort of habit tracker - not the “current past week”, but the past week counted from the daily note it’s embedded into.

The relevant part of my Dataview query is:

FROM "journals"
WHERE date(file.day) <= date(this.file.day) AND date(file.day) >= date(this.file.day) - dur(7d)

which I converted with this online tool to:

filters:
  and:
    - file.inFolder("journals")
    - date(file.day) <= date(this.file.day)
    - date(file.day) >= date(this.file.day) + "-7 days"

But Bases seems to have a problem with date(file.day).

My daily notes use the YYYY-MM-DD dddd name format, and I don’t always create them on the actual day, so can’t really use cdate.

Things I have tried

This thread looks sort of similar, but I couldn’t extrapolate it to my needs: 'On this day' base embedded in daily notes

Deleted – my solution didn’t work when I tested it in a second vault. :roll_eyes:

Hi.

This appears to work in a couple of local vaults. :crossed_fingers:

```base
views:
  - type: table
    name: Table
    filters:
      and:
        - date(file.name) >= date(this.file.name) - "7d"
        - date(file.name) <= date(this.file.name)
    sort:
      - property: file.name
        direction: DESC

```

Just noticed you use YYYY-MM-DD dddd. This might work:

```base
views:
  - type: table
    name: Table
    filters:
      and:
        - date(file.name.slice(0, 10)) >= date(this.file.name.slice(0, 10)) - "7d"
        - date(file.name.slice(0, 10)) <= date(this.file.name.slice(0, 10))
    sort:
      - property: file.name
        direction: DESC

```

Thanks, your solution worked for my core problem!

Now the only issues I’ve ran into are:

  • Bases doesn’t seem to render HTML (I like to use <span class="blabla"></span> that I can then format via CSS) - but I can sorta circumvent this with emojis.
  • The if(habit_boolean.contains(true), "yes", if(date(file.name.slice(0, 10)) >= now(), "unknown", "no")) formula always results in “no”. Even for today’s date or true values.

Edit: I just realised that the way to render HTML in Bases with html() is coming to Obsidian in v1.10.0, only in Catalyst version for now.

now() is a datetime. Since you sliced the file name to ten characters, the function you’re after is likely today().

And if habit_boolean lives up to its name by being a checkbox property, then you’ll need to do habit_boolean == true because .contains() doesn’t work on boolean type. But if habit_boolean a list of booleans, then carry on.

1 Like

Thanks, yes, the habit_boolean is a checkbox. I’ve tried this just now, but it still only giving “no” results even when the “habit boolean” property is checked:

html(if(habit_boolean == true, "yes", if(date(file.name.slice(0, 10)) >= today(), "<span class='unknown'></span>", "no")))

At least now it’s marking unknown, so the if date statement works.

There’s no such thing in HTML as all of those other functions you put inside of the html() function. Use html() on only the actual HTML.

Also, you’re creating a span that has a class but no content, so the result will show nothing when the file name’s date is today or later. If you meant to show something, then put your something into the formula where I wrote PUT SOMETHING HERE. But if you did mean for it to show nothing, then just delete that rudely shouting placeholder.

if(habit_boolean == true, "yes", if(date(file.name.slice(0, 10)) >= today(), html("<span class='unknown'>PUT SOMETHING HERE</span>"), "no"))

There’s no such thing in HTML as all of those other functions you put inside of the html() function. Use html() on only the actual HTML.

I followed an example I’ve seen on Discord, where they wrapped the entire formula in html(), and it seems to work all right for them. It also renders the HTML correctly for me, it just trips up on the if statement.

Also, you’re creating a span that has a class but no content, so the result will show nothing when the file name’s date is today or later. If you meant to show something, then put your something into the formula where I wrote PUT SOMETHING HERE. But if you did mean for it to show nothing, then just delete that rudely shouting placeholder.

Yes, I format the empty span via CSS, and include whatever content I want with ::before. (I picked up this habit with Dataview so that I wouldn’t need to modify many notes to change the looks, though it might be of reduced utility now that I can embed Bases.)

The formatted empty span works for the unknown status in the Bases code above, so that’s not the issue. It has to be something with the if(habit_boolean == true, "yes", part, but I can’t figure out what. That property is just a standard true/false checkbox, and using underscore in the property name instead of space is what that Dataview to Bases converter gave, so I assume it’s correct.

You can test just the part you suspect. Add a property formula:

if(habit_boolean == true, "yes", "no")

and see what results you get.

As a function, it’s correct and works.

If that test doesn’t return the results you expect, my first two thoughts are:

  • Check your YAML in Source mode (not Live Preview nor Reading) to be sure they’re formatted as:
---
habit_boolean: false
---

… without any extras such as quotation marks.

  • Check that the file names are properly formatted thus passing or failing the if statement the way you expect.

If you can’t find what’s amiss, feel free to share examples of your YAML where it’s not working as you expect. Syntax there might or might not be the issue, but people could help you look for it.

1 Like

Thanks!

After some testing, I figured out that the cause was that, unlike in Dataview, replacing spaces with underscores in property names doesn’t work in Bases.

I couldn’t find anything about this in the documentation or forums, but digging around in Discord revealed that the correct way to refer to property names with spaces in Bases is
note["property name"]. And finally the formula works :tada: