Dataview Date / Age Table Issues - Multiples Don't Work - How To Fix?

What I’m trying to do

Hiya, I’m trying to list the age of different people using Dataview.

I use a simple calculation to determine the age -

TABLE WITHOUT ID Name, BirthDate AS "Born", 
date(today) - BirthDate AS Age
WHERE Name

This works when there is one person, but fails when there is more than one:

Note: Their info is stored as -

Name:: Bob Bobly
BirthDate:: 2001-01-01
Name:: Sam Samly
BirthDate:: 1999-09-09

This is the error I’m getting -

Dataview: Every row during final data extraction failed with an error; first 3:
 - No implementation found for 'date - array'

Things I have tried

I tried quite a few different ways of addressing this, and stripped out everything extraneous to test the problem, but I think there may be something fundamental I’m missing or misunderstanding. My usage of DataView is fairly basic, but mostly successful so far.

Thanks for any help you can give.

Topic

Summary
  • How to calculate the ages of the persons from the sources like case_DVIF_DDD?

Q: How to use the dateformat function?

Answers:

sDate_to_stringA==dateformat(date("2022-09-30T23:59:58"),"yyyy-MM-dd HH:mm:ss")//=>“2022-09-30 23:59:58”

Q: How to use the dur function?

Answers:

Inline DQL: To get the hours

dur_result11==(date(2022-09-10T23:59:59) - date(2022-09-07T23:59:59)).hours
//=>72

dur_result13==(date(2022-09-10T23:59:59) - date(2022-09-07T23:59:59)).days
//=>3

Inline DQL: To compare by durations

b_result21==date(2022-09-10T23:59:59) - date(2022-09-07T23:59:59) <= dur(96 hours)
//=>true

b_result23==date(2022-09-10T23:59:59) - date(2022-09-07T23:59:59) <= dur(4 days)
//=>true

Q: How to use the round function which is named “Banker’s Rounding”?

Answers:

Inline DQL:
case10

n_result11==round(9.8249, 2)
//=>9.82


case20

n_result21==round(9.82671, 2)
//=>9.83


case31

n_result31a==round(9.8350, 2)
//=>9.84

n_result31b==round(9.8351, 2)
//=>9.84


case33

n_result33==round(9.8250, 2)
//=>9.82


case35

n_result35a==round(9.8251, 2)
//=>9.83

n_result35b==round(9.8252, 2)
//=>9.83


Q: How to use the choice function?

Answers:

result = choice(boolean-condition, Left_expression-when-true, Right_expression-when-false)

Inline DQL: boolean-condition=true

sDrinks_true== choice("Drinks" != "Coffee", "Black Coffee", "Green Tea")
//=>Black Coffee


Inline DQL: boolean-condition=false

sDrinks_false== choice("Drinks" = "Coffee", "Black Coffee", "Green Tea")
//=>Green Tea


Q: How to use the typeof function?

Answers:

Inline DQL

s_Result11==typeof("what happened")
//=>string

s_Result13==typeof([10, 20, 30])
//=>array

s_Result14==typeof(["A", "B", "C"])
//=>array

Q: How to use the map function?

Answers:

Inline DQL

a_Result11==map([10, 20, 30], (e) => e + 6)
//=>16, 26, 36

a_Result13==map(["A", "B", "C"], (e) => e + "P")
//=>AP, BP, CP

Q: How to modify this.YAML_today in the DQL if I would not like to use the field YAML_today?

Answers:

the Original Example
      map(
          choice(typeof(BirthDate) = "array", BirthDate, list(BirthDate)), 
          (e) => round(dur(this.YAML_today - e).years, 0)
          ) AS "Age"
the New Example
      map(
          choice(typeof(BirthDate) = "array", BirthDate, list(BirthDate)), 
          (e) => round(dur(date(today) - e).years, 0)
          ) AS "Age"

Test

Summary
  • dataview: v0.5.41

Input

Summary

the current note

  • filename : 20220910_DQL10
---
YAML_today: 2022-09-10
---

### DQL10


dictionary files

  • Location: “100_Project/02_dataview/Q89_DVIF_DDD/Q89_test_data”
  • filename : dic_20010301=>typeof(BirthDate) = “string”
---
Date: 2001-03-01
---
#person

Name:: Dwayne Johnson
BirthDate:: 1972-05-02
Height:: 196
Title:: Fast & Furious Presents: Hobbs & Shaw
Genre:: "Action", "Adventure", "Thriller"
Year:: 2019
Runtime:: 137 min
Poster:: https://m.media-amazon.com/images/M/MV5BOTIzYmUyMmE[email protected]@._V1_SX300.jpg



  • filename : dic_20010401=>typeof(BirthDate) = “array”
---
Date: 2001-04-01
---
#person

Name:: Jason Statham
BirthDate:: 1967-07-26
Height:: 178
Title:: Transporter 3
Genre:: Action, Crime, Thriller
Year:: 2008
Runtime:: 104 min
Poster:: https://m.media-amazon.com/images/M/[email protected]@._V1_SX300.jpg


Name:: Vin Diesel
BirthDate:: 1967-07-18
Height:: 183
Title:: F9: The Fast Saga
Genre:: Action, Crime, Thriller
Year:: 2021
Runtime:: 143 min
Poster:: https://m.media-amazon.com/images/M/MV5BMjI0NmFkYzEt[email protected]._V1_SX300.jpg



  • filename : dic_20010501=>typeof(BirthDate) = “array”
---
Date: 2001-05-01
---
#person

Name:: Gal Gadot
BirthDate:: 1985-04-30
Height:: 178
Title:: Wonder Woman
Genre:: Action, Adventure, Fantasy
Year:: 2017
Runtime:: 141 min
Poster:: https://m.media-amazon.com/images/M/MV5BMTYzODQzYjQt[email protected]._V1_SX300.jpg


Name:: Anne Hathaway
BirthDate:: 1982-11-12
Height:: 173
Title:: Get Smart
Genre:: Action, Adventure, Comedy
Year:: 2008
Runtime:: 110 min
Poster:: https://m.media-amazon.com/images/M/[email protected]@._V1_SX300.jpg


Name:: Emma Watson
BirthDate:: 1990-04-15
Height:: 165
Title:: Harry Potter and the Deathly Hallows: Part 2
Genre:: Adventure, Fantasy, Mystery
Year:: 2011
Runtime:: 130 min
Poster:: https://m.media-amazon.com/images/M/MV5BMGVmMWNiMDkt[email protected]._V1_SX300.jpg



  • filename : dic_20010601=>typeof(BirthDate) = null
---
Date: 2001-06-01
---
#person

Name:: 
BirthDate:: 
Height:: 
Title:: 
Genre:: 
Year:: 
Runtime:: 
Poster:: 



  • filename : dic_20010701
---
Date: 2001-07-01
---
#person

Name:: Liv Tyler
BirthDate:: 1977-07-01
Height:: 178
Title:: Crimes of Passion: Escape from Terror - The Teresa Stamper Story
Genre:: Drama
Year:: 1995
Runtime:: 90 min
Poster:: https://m.media-amazon.com/images/M/[email protected]@._V1_SX300.jpg


Name:: 
BirthDate:: 
Height:: 
Title:: 
Genre:: 
Year:: 
Runtime:: 
Poster:: 




DQL10_map_lists_non-lists_durations_and_TABLE

Summary

Main DQL

Code Name Data type Group By Purposes Remark
DQL10_map_Lists_notLists
_duration_and_TABLE
lists or non-lists no 0.To require YAML_today: 2022-09-10 in the current note
1.To deal with lists or non-lists
2.To use choice, typeof and map
3.To filter by BirthDate
4.To sort by file.link in ascending order
5.To display the result as a table
6.The DQL ignores the note named dic_20010701 where the BirthDate list contains null.
1.To require YAML_today: 2022-09-10 in the current note

2.You could replace this.YAML_today in the DQL with date(today) if you would not like to use the field YAML_today.

DQL10_map_lists_non-lists_durations_and_TABLE

Summary_code
title: DQL10_map_lists_non-lists_durations_and_TABLE =>0.To require `YAML_today: 2022-09-10` in the current note 1.To deal with lists or non-lists 2.To use choice, typeof and map 3.To filter by BirthDate 4.To sort by file.link in ascending order 5.To display the result as a table 6.The DQL ignores the note named `dic_20010701` where the `BirthDate` list contains null.
collapse: close
icon: 
color: 
```dataview
TABLE WITHOUT ID
      file.link AS "File",
      Name AS "Name",
      
      map(
          choice(typeof(BirthDate) = "array", BirthDate, list(BirthDate)), 
          (e) => dateformat(e, "yyyy-MM-dd")
          ) AS "Born",

      map(
          choice(typeof(BirthDate) = "array", BirthDate, list(BirthDate)), 
          (e) => round(dur(this.YAML_today - e).years, 0)
          ) AS "Age"

FROM "100_Project/02_dataview/Q89_DVIF_DDD/Q89_test_data" AND #person
WHERE BirthDate != null
SORT file.link ASC

```

Screenshots(DQL10)


Reference

Summary

@justdoitcc: no one can understand whatever you trying to present.

@natex Let assume you have a folder called “PEOPLE”, contain person 1.md note that have the tag #people AND person 2.md note that also have the tag #people

AND on top of those notes you have a birthday and phone data in YALM frontmatter like this

birthday: 2000-12-01
phone: 0123456789

SCREENSHOT

  • Install another plugin called “Sortable” to sort that table in whatever you want by clicking on the column header.
  • More trick: you can have 3 table like this, one for family, one for coworker, one for friend … or whatever by tag each person note difference like #people/family #people/coworker #people/friend, THEN edit the tag in begining part of the code according to those group of people.

Now the dataviewjs gonna look like this :

```dataviewjs
// People birthdays using DataviewJS
let pages = dv.pages("#people").where(p => p.birthday);

var start = moment().startOf('day');
var end = moment(start).add(dv.current().duration);
var dateformat = "YYYY-MM-DD"

function nextBirthday(birthday) {
    var bday = moment(birthday.toString());
    var bdayNext = moment(bday).year(start.year());
    if (bdayNext.isBefore(start, 'day')) {
        bdayNext.add(1, "year");
    }
    return bdayNext;
}

function countdown(birthday){
	var bday = moment(birthday.toString())
	const setTime = new Date(bday);
	const nowTime = new Date();
	const restSec = setTime.getTime() - nowTime.getTime();
	const day = parseInt(restSec / (60*60*24*1000));
	const str = day + " days";
	return str;
}


dv.table(["Name", "Birthday", "Age","Countdown", "Phone"],
  pages.sort(p => moment(p.birthday.toString()).format("MM-DD"), 'asc')
  .sort (p => p.birthday, 'desc')
  .map(p => [
    // The name
    p.file.link,
    // Formatted birthday from YAML frontmatter
    moment(p.birthday.toString()).format("MMMM Do"),
    // Current age in years
    moment().diff(moment(p.birthday.toString()), 'years'),	
	countdown(nextBirthday(p.birthday)),
	p.phone,
	]
  )
);

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