Dataview parse as number

In my daily note, I have a dataviewJS query that totals all the different time I’ve tracked that day:
Total:: $= dv.pages('[[]] and #Time').hours.array().reduce((acc, val) => acc + val, 0)

This works great. But if I try to do any additional math with the result, dataview and dataviewjs queries treat the result as a string, so = this.total + 2 just adds a 2 to the end of the number (5.5 + 2 becomes 5.52 instead of 7.5).

Is there a way to tell dataview (DQL or JS) to parse as a float? I tried parseFloat, but it didn’t seem to work.

Thanks in advance for your help!

You can’t do that… Simply because the value in Total:: is this expression $= dv.pages('[[]] and #Time').hours.array().reduce((acc, val) => acc + val, 0), not the rendered value you see when dataview makes its magic.

When you run the query =this.total + 2 dataview reads this:

`$= dv.pages('[[]] and #Time').hours.array().reduce((acc, val) => acc + val, 0)` + 2

not

5 + 2

But then, when it renders this, by magic it renders also the first expression (the dvjs query), which, at start, isn’t a number. It’s a kind of render in two steps… but losing the calculation ability because in the first step you don’t have a number to sum with “2”.

This is one of the reasons to consider when use queries as values. For example, if you use dv.current in the value expression placed in field Total:: and call this field in a query in another note, then the dv.current() becomes the note where you render the query, not the original one.

1 Like

Thank you! That is super helpful. So is there any way to run this sort of compounded query that uses the result of a previous query?

I’m not versed enough in code to give a direct answer.
You can use values as queries… but the point is: what we can do with that?
If we considered the value only as a string, as a visual thing to be rendered, then no problem (only attention needed to “auto-references” as dv.current or similar); but if we want to do calculations, comparisions or something like that, then I don’t know any solution…

only $= dv.pages('[[]] and #Time').hours.array().reduce((acc, val) => acc + val, 0) + 2 in same query

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