You are viewing limited content. For full access, please sign in.

Question

Question

Multi row table: product calculation for mileage (kms*rate)

asked on April 18, 2018

I'm having some troubles getting my form to work properly. Here is the section of form with the issue:

I want each row to show kms*mileage rate for that row. I have tried the following two options without the right results:

Option 1: use the built in formula option

In this case, for the "Amount" field of the table, I went into the advanced setting of the variable and added in the formula:

=PRODUCT(metrage.kms,metrage.mileageratemetrage)

This worked for the first row but each row added a) shows the table total instead of the row total and 2) seems to multiply the new amount with the last, thereby exponentially growing the total amount:

 

Option 2: use javascript to calculate

I inserted the following Javascript (based on the solution from this LF Answer):

$(document).ready(function () {
    $('.cf-table-block').on('blur', 'input', sumtotal);
    function sumtotal() {
        var metragekm = 0;
        var mileagerate = 0;
        var total = 0;
        var subtotal = 0;
 
        $('.cf-table-block tbody tr').each(function () {
            metragekm =  parseNumber($(this).find('.metragekm input').val()); 
            mileagerate = parseNumber($(this).find('.mileagerate input').val());
            
            subtotal = (metragekm * mileagerate);
 
            $(this).find('.total input').val(subtotal);
            total += subtotal;
        });
        $('.metragetotal input').val(total);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

 

This was an improvement, because I can show a total at the bottom but it still isn't calculating anything for the rows:

I know that this is possible because I saw the person in this LF Answers with a table that worked.

A couple more concerns:

The form subtotal doesn't seem to update consistently:

Looks like that is dealt with in this LF answer but I'm no sure how to apply it...

Hoping someone can help!

0 0

Answer

SELECTED ANSWER
replied on April 19, 2018

If you use the product function with Index () and Row () functions it will calculate each row separately.

 

Here is the formula:

=PRODUCT(INDEX(Table.KMs,ROW()),INDEX(Table.Mileage_Rate,ROW()))

 

 

Here is how the table would look:

 

 

 

4 0

Replies

replied on April 19, 2018

That worked! And the subtotal seems to be updating correctly too. Thank you Dean!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.