Can you use dataview to subtract a number from yesterday?

What I’m trying to do

I use a daily note and I have a log entry that looks like this:

log:: #biz/earnings 5600.65

Each day I am logging the lifetime earnings - what I would like to know is the daily earnings, ie today’s number minus yesterday’s.

Is there a way to do this in dataview?

Things I have tried

TABLE
WITHOUT ID
link(Source, dateformat(date(Source), "yyyy-MM-dd")) as "",
rows.Columnname as "Lifetime", length(rows) as "Daily Earnings"
FROM !"Templates"
FLATTEN log as Columnname
WHERE contains(Columnname, "#biz/earnings")
GROUP BY file.name as Source
SORT rows.file.day DESC

Sorry it took so long before getting back to you, but it’s not trivial to do what you ask. Especially the bit about carrying over information from yesterday to today within the same query is tricky, to say the least.

Lets start with calculating todays earnings though. In that respect you’re not to far off, you’re missing to do a sum() of the earnings, and you’re missing the usage of number(Columnname) to extract the number out of the log entry. So with slightly different, and in my opinion clearer variable names, your query could then look like:

```dataview
TABLE WITHOUT ID
  link(Source, dateformat(date(Source), "yyyy-MM-dd")) as "",
  sum(number(rows.entry)) as "Daily Earnings"
FROM !"Templates"
FLATTEN log as entry
WHERE contains(entry, "#biz/earnings")
GROUP BY file.name as Source
SORT rows.file.day DESC
```

Regarding the total earnings, one issue is related to carrying over the information, and another issue is what is the starting point? Do you always start from 0? And you say subtract yesterdays value? Shouldn’t you add them together?

One way to handle it could be to use dataviewjs surrounding the script above, and then use javascript to manipulate the result array doing the cumulutative calculations needed. It does however involve a little bit of javascript, which might not be to easy to read. And I’m a little uncertain regarding your logic, as stated in previous paragraph.

1 Like

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