How to get the sum of field-values by filtering based on prefix of field-names

I want to get the sum of field-values by filtering based on prefix of field-names

Here is an example.
My field-names start with either ct- or cect- . For example ct-abdomen, cect-chest

I need to get the sum of all fields with prefix ct- and cect-
How to do it

Does this answer help? [tw5] Selectively summing together field values? - #4 by Dave_Kor

1 Like

Thank you @Scott_Sauyet for the help. I will check it tomorrow day time and respond. It’s late night here.

I tried a modification of the code given in the above link, again it doesn’t seem to work.

<$list filter="[<currentTiddler>fields[]prefix[ct-]] :map[<currentTiddler>get[fields]] +[sum[]]"/>

The following finds all the fields with those prefixes in the wiki

<$list filter="[fields[]prefix[ct-]] [fields[]prefix[cect-]]">

</$list>

The following gives me a sample of values on your wiki

<$list filter="[fields[]prefix[ct-]] [fields[]prefix[cect-]]" variable=fieldname>
    <<fieldname>> values: <$list filter="[each<fieldname>get<fieldname>]"></$list><br>
</$list>

Do you want a sum of each matching fieldname or all matching fieldnames?

  • In your test data it looks like each fieldname only occurs once except cect-abdomen

See reduce Operator

This lists all tiddlers containing each field

<$list filter="[fields[]prefix[ct-]] [fields[]prefix[cect-]]" variable=fieldname>
    <<fieldname>> {{{ [has<fieldname>] }}}<br>
</$list>

This gives me the sum of each fieldname

<$list filter="[fields[]prefix[ct-]] [fields[]prefix[cect-]]" variable=fieldname>
    <<fieldname>> {{{ [has<fieldname>] :reduce[get<fieldname>add<accumulator>] }}}<br>
</$list>
  • I had to change some of your test data to confirm this

To sum all values from all fieldnames is a little more complex but do you want that?

Thanks for helping @TW_Tones

This is what I actually want. I want sum of all fields with prefix ct- and cect- seperately and ct- plus cect- together

Hopefully someone else can help, this has being driving me crazy.

  • We need to get the values for all resulting fieldnames
  • We need to stop the values being deduplicated
  • We then need to apply a second reduce and accumulate, to sum the sum

Arghhhh!

I find your wiki annoying because I can’t use shift arrow keys to navigate the text

2 Likes

I guess there I have assigned shift arrows keys as keyboard shortcut for tiddler navigation. I will change it when I am back in desktop.

try

{{{<currentTiddler>[fields[]prefix[ct-]] :map[<..currentTiddler>get<currentTiddler>] +[sum[]]}}}

The difficulty is there are multiple fieldnames in multiple tiddlers that need to be accumulated :thinking:

for both:

{{{<currentTiddler>[fields[]prefix[ct-]] <currentTiddler>[fields[]prefix[cect-]] :map[<..currentTiddler>get<currentTiddler>] +[sum[]]}}}

@buggyj I am currently away from my desktop…will test and and respond once I am back home

@TW_Tones I need the sum for single tiddler only.I haven’t thought of the other possibility yet

Multiple fields in the same tiddler?

For which tiddler(s)?

@buggyj Thanks for sharing the code. Both of them works well.
What should I do to get the sum of the same fieldvalues ( with prefix ct- and cect- ) in all tiddlers with a particular field or tag (For example, field-name sth-rad with field-value data) for a particular month.

How to identify each month seperately ? I am ok with adding a field-name to these tiddlers to denote the month they belong to ( for example field-name month with field-value February2023).

This month data is displayed in the tiddlers whose title will be same as that of the field-value of the field-name month. That is month tiddler February2023 will show sum of all field-values from tiddlers with fields sth-rad=data and month=February2023

Here is an example data entry tiddler https://scribe.tiddlyhost.com/#16th%20February%202023 and month tiddler https://scribe.tiddlyhost.com/#February2023

@TW_Tones I am in the process of creating a data entry and analysing system for my work place. It is in the beginning stage only, that’s why I hadn’t reached the level of calculating the field values for a large number of tiddler. As a first step I wanted to get the sum of fields with prefix ct- and or cect- for the currentTiddler only. Now that buggyj have solved the first step, I am proceeding to the next step, that is calculating the field values for the entire month among tiddlers with a particular field or tag (For example, field-name sth-rad with field-value data)

1 Like

I cannot see a way of doing that with wikitext.

In all tiddlers with a particular field or tag (For example, field-name sth-rad with field-value data ) for a particular month.

I am confident all such things can be done in Wiki text, approaches include;

  • Making a list of tiddlers subject to a particular operation or filter, you may sometimes need to wikify the result.
  • Use one or More filters as needed rather than trying to make large compound filters, typically with nested lists

If we find something, anything that can’t be done in Wiki text ,we need to ensure it can be, eventually be done in the core.

As far as I can see that is currently no way to handle lists of lists with filtering, so either intermediary results would need to be stored and then ‘reduced’, or filters would need to be dynamically created (thru a js macro or wikifying (or possible a recursive wikitext macro)). The alternatives are to do the whole thing (or most or it) in a js macro, or created a new filter (i.e. writing some js).

Yes, It can be addressed by;

… the output of any macro, then use that in a subsequent macro, but its not very elegant.

I think the foreshadowed changes may offer an opportunity. We will see.