Dataview and a function with a switch that applies frontmatter data of the note it's in

Hi guys!

What I’m trying to do

For some notes, I would like to use a function that calculates on property values given in the yaml frontmatter of the notes. The function should only work within the file and calculate based on a condition given by the data.

PropertyString: "A"
PropertyNumber1: 10
PropertyNumber2: 20

I would like to use a function that is active as soon as all relevant properties are provided in the frontmatter. The function should operate based on a condition given by PropertyString. I try to outline what it should do below:

// Look if all neccessary data is provided in yaml frontmatter: 
// 1. `PropertyString` 2. `PropertyNumber1` 3. `PropertyNumber2`
// Pull those and operate conditionally based on 1. `PropertyString`

function MyFunc() {
if (PropertyString === "A") {
result = PropertyNumber1 - PropertyNumber2;
	} else if (type === "B") {
	result = PropertyNumber2 - PropertyNumber1;
		}
}

After the function is applied, I would like to use the result to make further calculations and in the end have a neat table of results…

Things I have tried

I tried to get this done using normal dataview using the choice() function but I was not successful. Would you please help me to get my foot in the door with dataviewjs?

Cheers!

Here’s a very ugly Dataview query that essentially matches the functionality of your code block…

```dataview
TABLE PropertyString, choice(regexmatch("A",PropertyString),PropertyNumber1 - PropertyNumber2,choice(regexmatch("B",PropertyString),PropertyNumber2 - PropertyNumber1,"Not A or B")) as "Result"
WHERE contains(file.name,this.file.name)

If you still want the dataviewjs approach, let us know.
Hint: You can use AI tools like copilot to help you write dataviewjs and dataview code blocks.

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