Obsidian Dataview Query changing meters to time

What I’m trying to do

I have a template that I record certain things related to my knitting projects. For yarn, these include among other things (this is how much is in one skein):

yarn:
  weight: 100g
  length: 400m

When I’m making a table of this, with the dataview code:

TABLE WITHOUT ID
	link(file.link, file.title) AS "Socks",
	yarn.weight AS "Yarn Weight",
	yarn.length AS "Yarn Length",
	needles.used AS "Needles"
FROM #beta/crafts/knitting/sock-project AND -"z-system"

The meters are changed to time: I don’t understand what I’m doing wrong! See below the picture for the result of the above query.

Basically dataview is thinking my 400m(eters) is 400m(minutes), but I don’t understand why…

That’s an unfortunate side effect of Dataview trying to understand durations and dates in your properties. You can circumvent it by using file.frontmatter.yarn.length

Had the same problem and I couldn’t find a solution, so I used dataviewjs and pars the numbers.

// Function to parse and convert strings like "15,20 km" or "32 m" to numbers
function parseNumber(text) {
    if (!text) return 0;
    // Remove non-numeric characters (except commas and dots), and parse as a number
    let number = parseFloat(String(text).replace(/[^\d,.-]/g, '').replace(',', '.'));
    return isNaN(number) ? 0 : number;
}

And than at the bottom when I create a table I call this function:

// Render the table with parsed values
dv.table(["Distance", "Ascent"], 
    entries.map(p => [
        parseNumber(p.Distance) + " km",  // Parse the Distance and append " km"
        parseNumber(p.Ascent) + " m"  // Parse the Ascent and append " m"
    ])

Hope this helps you to get going.

Cheers, Marko :nerd_face:

1 Like

Thanks, this is why I don’t believe in AI, it doesn’t understand me :joy:
I actually circumvented it with using yarn.m instead of yarn.length. Just means that I need to convert to meters if the length is in yards.

I will try this! My circumvention worked (I only had like 3 notes so far, so it wasn’t a lot of work to change), but in general I like solutions that work for more general cases… I had the original idea that I could record the length in meters or in yards, but in general I use meters, so my circumvention is not a bad one…

But this solution might make me learn something :slightly_smiling_face:

1 Like