How to calculate percentage

In my budget wiki, I have month, category and subcategory tiddlers with debit, credit fields.

credit field has <$list filter="[month<currentRecord>input_type[credit]get[amount]sum[]]"/>

debit field has <$list filter="[month<currentRecord>input_type[debit]get[amount]sum[]]"/>

Also there are input tiddlers with fields like amount, input_type, month, category, subcategory etc.

In the month tiddler, I want to show the percentage of expense under each category and subcategory for that particular month tiddler. How to do it ?

For example, consider the month tiddler May2022. All the petrol related tiddler in the month of May2022 will have the following fields

  1. amount which will be manually filled.
  2. category which will be Car
  3. subcategory which will be Fuel
  4. month which will be May2022
  5. input_type which will be debit

Now in the May2022 tiddler I want to show the percentage expense in the category Car and subcategory Fuel for that month. Similar is the case for other categories and subcategories. How to do it ?

Here is a demo wiki - https://demo-complete.tiddlyhost.com/

I’m not totally certain but I think :reduce can be useful here.

Try adding divide<length> to the examples there:

[tag[shopping]] :reduce[get[quantity]add<accumulator>divide<length>]

After a bit of experimenting I came up with this based on the example:

<$let sum={{{[tag[shopping]] :reduce[get[price]add<accumulator>]}}}
num={{{[tag[shopping]count[]]}}}
avg={{{[<sum>divide<num>]}}}>
avg: <<avg>>
</$let>

I seem to have gotten fixated on average when you want percent, but i think the basic prinicple applies.

edit2:
So I made an example with percents. There’s probably a more clever way to do it. Paste in a new tiddler at tiddlywiki.com

<table>
<$let total={{{[tag[shopping]] :reduce[get[price]multiply{!!quantity}add<accumulator>]}}}>
<$list filter="[tag[shopping]]">
<$let subtotal={{{[{!!price}multiply{!!quantity}]}}}>
<tr><td>{{!!title}}</td><td>{{!!quantity}}</td><td>${{!!price}}</td><td>$<<subtotal>></td><td>{{{[<subtotal>divide<total>multiply[100]]}}}</td></tr>
</$let>
</$list></$let></table>
1 Like

Thank you @amreus
I am getting some ideas after reading your code. Will test the code after making the changes need for my budget wiki and give back the feedback once I am back on my desktop.

<$let sum={{{[month[June2022]category[Car]get[amount]sum[]]}}}
tot={{{[month[June2022]input_type[debit]get[amount]sum[]]}}}
per={{{[<sum>divide<tot>]}}}>
percentage: <<per>>
</$let>
<$let sum={{{[month[June2022]category[Car]] :reduce[get[amount]add<accumulator>]}}}
tot={{{[month[June2022]input_type[debit]] :reduce[get[amount]add<accumulator>]}}}
per={{{[<sum>divide<tot>]}}}>
percentage: <<per>>
</$let>

I did a short testing and based on @amreus your suggestion and a previous suggestion by @Mark_S , I created two codes. Both needs to get mutliplied with 100 to get a percentage values. My attempts to add multiply operator didn’t work as expected. Any suggestions?

Edit: @amreus I forgot to study your second code showing the percentage. I will comment after trying it out.

One problem is you have no tiddlers which meet all the conditions of your filter. So there are no tiddlers with field month of “June2022” and category of “Car” and an amount that is not blank. Which means your variables are blank.

1 Like

The demo does not have all the tiddlers (I have shared only a sample in the demo). The code is working in my personal wiki. I will update the demo later today since I am travelling currently.

I see. Try this for the percent variable:

per={{{[<sum>divide<tot>multiply[100]fixed[2]]}}}

<$let sum={{{[month[June2022]category[Car]get[amount]sum[]]}}}
tot={{{[month[June2022]input_type[debit]get[amount]sum[]]}}}
per={{{[<sum>divide<tot>multiply[100]fixed[2]]}}}>
percentage: <<per>>
</$let>

@amreus Thank you. It works now.

Can you tell me why you added fixed[2]. I had tried the multiply operator alone which wasn’t working.

fixed rounds to the given number of decimal places.

The fixed operator returns a string representation of the input number that does not use exponential notation and has exactly the specified number of digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length.

1 Like

Using this Donut Chart in Tiddlywiki with Animation shared by @Mohammad to show the percentage graphically. Thank you to Mohammad also

1 Like