Calculation using using a conditional category field.

Answered

Comments

37 comments

  • Hamid

    Hi Francisco,
    Is your second app related to the expense app by relationship field ?

    1
    Comment actions Permalink
  • Francisco Gozalo

    Hi Hamid,

    Yes it is. Sorry I forgot to mention it previously. They are indeed related by a relationship field.

    0
    Comment actions Permalink
  • Hamid

    You can try with this code :

    var val = @sum of Value;
    var cat = @all of Situation;
    cat.indexOf("Paid") >= 0 ? val : 0
    

    it means : if Paid category is selected sum all of the values of the related items.

    3
    Comment actions Permalink
  • Francisco Gozalo

    Wow! It works perfectly. Would it be possible for me to add a currency symbol before the number? My previous formula was like this:

    "BRL"+(Sum of Valor).toFixed(2)

    BRL = Currency Symbol and .toFixed(2) is the mandatory 2 decimal cases

    Once again, thank you so much for your help! Cheers.

    0
    Comment actions Permalink
  • Hamid

    You can choose to display numbers with or without decimals - use the dropdown to the left of the block to select your preference. and add your unit
    OR

    
    cat.indexOf("Paid") >= 0 ? "BRL "+val.toFixed(2) : 0
    
    
    0
    Comment actions Permalink
  • Francisco Gozalo

    With the last string suggested I'm getting invalid value, (string) must be a number. I'll go ahead and use the dropdown menu (makes more sense that way). Thank you.

    0
    Comment actions Permalink
  • Hamid

    Yes the first way is more clean
    but if you want apply the second one (with the formula) you must create a new calculation field (clean) and it will be a text field not able to make any calculation with it.
    best regards;
    /Hamid

    0
    Comment actions Permalink
  • Francisco Gozalo

    Hi Hamid, sorry to bother you again.

    I just realized there may be something wrong with the code:

    I just added a new expense (with the value of 438,05) and marked it as "Paid", still, it's being added (summed) to calculation field. So, instead of just showing the amount for the unpaid expense value of 1.9, I'm actually getting the total value of 439,95, even though that should not be the case.

    The code is exactly the one you posted.

    0
    Comment actions Permalink
  • Hamid

    Sorry Francisco,
    I havn't read correctly your question :)
    replace in the code "Paid" by "Open"

    cat.indexOf("Open") >= 0 ? val : 0
    
    
    0
    Comment actions Permalink
  • Francisco Gozalo

    No problem. I've done this already, still it doesn't seem to be working. On the calculation's field, whether i change the final string of the code to:

    cat.indexOf("Open") >= 0 ? val : 0

    or

    cat.indexOf("Paid") >= 0 ? val : 0

    I'm getting the same result, even though there are 2 expenses added, and one is marked as paid and the other as open, as previously demonstrated.

    Any idea what I might be doing wrong?

    Thanks for the prompt feedback.

    0
    Comment actions Permalink
  • Hamid
    
    var val = @all of Value;
    var cat = @all of Situation;
    var total = 0;
    for(var i = 0; i < val.length; i++){
    if(cat[i] == "Open"){
    total += val[i];
    }
    }
    
    

    Try with this code (not tested it)

    1
    Comment actions Permalink
  • Hamid

    Good news :)
    It work for me.

    0
    Comment actions Permalink
  • Francisco Gozalo

    Awesome! It worked for me too. Cheers!

    -1
    Comment actions Permalink
  • Francisco Gozalo

    Hi, just some feedback from using the code as provided. It works great. But there seem's to be a little bug, nothing major. For every other item that don't have an expense reported, either as open or paid, The formula make shows a sum of 1. As if there were 1 unpaid money (whichever currency used).

    0
    Comment actions Permalink
  • Justin VanDervort

    Hamid TAIATI,

    Thank you, Thank you, Thank you!!   Works great!

    0
    Comment actions Permalink
  • Justin VanDervort

    .... is there a way to have this add two categories?  I have five categories and I want the numerical balance as along as one of the two certain categories is selected.   Can this be done in one calculation field? 

    0
    Comment actions Permalink
  • Justin VanDervort

    They way I wanted to do it is to total up the sum if it were categorized with 'Order Fulfilled' or 'Order Placed'.   I got it to work for me using this code.  Hope it works for anyone else looking to do this.  I am not a programer by any means so this may not be the best way or other use cases.  

     

    var val = @Order Total;
    var cat1 = @Order Status;
    var cat2 = @Order Status;
    (cat1.indexOf("Order Fulfilled") >= 0 ? val : 0) + (cat2.indexOf("Order Placed") >= 0 ? val : 0)

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Justin,

    if the category field is single choice you don't need `.indexOf()` (only necessary for multi choice category fields) and the whole code could be a bit shorter: 

    var val = @Order Total;
    var cat = @Order Status;
    cat == "Order Fulfilled") || cat == "Order Placed" ? val : 0

    || is used in Javascript for OR (&& is for AND) 

    Rainer

     

    0
    Comment actions Permalink
  • Isaac Miller

    hi!

    I've tried using the second script but it's not working for me:

    var val = @all of Value;
    var cat = @all of Situation;
    var total = 0;
    for(var i = 0; i < val.length; i++){
    if(cat[i] == "Open"){
    total += val[i];
    }
    }
    

    it says the script give and undefined result? Any thoughts on what might be wrong?

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Isaac,

    try as last line after the last }

    total || 123

    If that doesn't work, can you please share a screenshot of the calculation field. Which field type is cat - single choice or multi choice category?  Is  your calculation field number type?

    Rainer

    0
    Comment actions Permalink
  • Isaac Miller

    Hi Rainer! Totally working if there's some amount paid (thank God) only downside is that if there's nothing paid, calc returns the 123 value instead of just 0, any way around that?

    Calc field is number type, val field is currency and cat field is single choice category, thanks!!

    0
    Comment actions Permalink
  • Rainer Grabowski

    Just write 0 instead of 123 ("123" can be any number)

    var val = @all of Value;
    var cat = @all of Situation;
    var total = 0;
    for(var i = 0; i < val.length; i++){
    if(cat[i] == "Open"){
    total += val[i];
    }
    };
    total || 0
    1
    Comment actions Permalink
  • Francisco Gozalo

    This

    total || 0

    fixed the bug i was having when no value was entered. Thank you

    0
    Comment actions Permalink
  • Isaac Miller
    Worked wonders! Thank you so much!
    0
    Comment actions Permalink
  • Stacy Schufreider

    Hi Rainer (and all),

    I was able to successfully use the formula below that you had provided for one condition, but I'd like to expand it to two conditions - for example, if a category field Approved = Yes and if another category field Paid = No.  How can I modify it for two conditions?  Thanks in advance for your help!

    var val = @all of Value;
    var cat = @all of Situation;
    var total = 0;
    for(var i = 0; i < val.length; i++){
    if(cat[i] == "Open"){
    total += val[i];
    }
    };
    total || 0
    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Stacy,

    add a var for paid and a second condition: 

    var val = @All of Value; 
    var appr = @All of Approved;
    var paid = @All of Paid
    var total = 0; for(var i = 0; i < val.length; i++){
    if(appr[i] == "Yes" && paid[i] == "Yes"){
    total += val[i];
    }
    };
    total || 0

    Rainer

     
    0
    Comment actions Permalink
  • Stacy Schufreider

    Thank you very much Rainer, that worked!

    0
    Comment actions Permalink
  • Carson Young - Red Cliff Labs

    I just used this and it worked great! thanks for the contributions here

    0
    Comment actions Permalink
  • Ronson Test Account

    Does anyone know if this can be adapted to sum values on related items, but only from the related items with dates greater than and less than two dates on item A?

    Item A is an account with two dates, Start of This Month and End of This Month

    Items B are transactions on the account and I want to have a field on the account that sums the value of only the transactions with a transaction date that falls within the current month (i.e. between those two dates on the account).

    It seems this calculation you guys are working on, here, could easily be adapted though I have very limited knowledge of javascript.

     

    Additionally, I'm looking to hire a full-time Podio developer who is proficient with javascript, Globiflow, and API development.
    If anyone has a connection for me (could be yourself) please do share. :)

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Ronson,

    you're right, it can easily be adapted.

    var start = @start date;
    var end = @end date;
    var valTrans = @all of transaction value;
    var dateTrans = @all of transaction date;
    var sum = 0;
    for(var i = 0; i < dateTrans.length; i++){
    var date = moment(dateTrans[i]); 
    if(date >= start && date <= end){
    sum += Number(valTrans[i]) || 0;
    }};
    sum

    For the full-time Podio developer please contact me: rg@delos-consulting.com

    Rainer

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk