Date based reduction calculation

20 views
Skip to first unread message

Stu

unread,
Jun 4, 2020, 11:58:23 AM6/4/20
to dc-js user group
Hi all,

This fiddle shows a table with basic reduction by date.
I have added a column (qtyPaul) that correctly shows values against the names that match "paul".
Is is possible to calculate all these values where the date is <= itself as in the table below:

Effectively a running total with criteria

table2.PNG



Thanks, Stu




Gordon Woodhull

unread,
Jun 4, 2020, 3:06:57 PM6/4/20
to dc.js user group
Hi Stu, 

I guess if you have added that column, you can just use accumulate_group from the FAQ on group.reduceSum(d => d.qtyPaul).

In order to eliminate that extra column, you could do group.reduceSum(d => d.name === 'paul' ? d.qty : 0)

For completeness, accumulate_group is below - works like any fake group.

Cheers,
Gordon

function accumulate_group(source_group) {
    return {
        all:function () {
            var cumulate = 0;
            return source_group.all().map(function(d) {
                cumulate += d.value;
                return {key:d.key, value:cumulate};
            });
        }
    };
}

On Jun 4, 2020, at 11:58 AM, Stu <stutr...@gmail.com> wrote:

Hi all,

This fiddle shows a table with basic reduction by date.
I have added a column (qtyPaul) that correctly shows values against the names that match "paul".
Is is possible to calculate all these values where the date is <= itself as in the table below:

Effectively a running total with criteria

<table2.PNG>



Thanks, Stu





--
You received this message because you are subscribed to the Google Groups "dc-js user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dc-js-user-gro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dc-js-user-group/7104303a-3eef-447f-bbae-d046f3680788%40googlegroups.com.
<table2.PNG>

Stu

unread,
Jun 5, 2020, 5:15:17 AM6/5/20
to dc-js user group
Thanks for this Gordon,

I already have the stuffByDate group. Am I able to integrate the accumulation here somehow ?
That way I can apply additional calcs within and present in the Table


var stuffBy_date = dateRef.group().reduce(
    function(p, v) {
        ++p.count;
  p.name = v.name;
          p.qty += +v.qty;
          p.qtyPaul += (p.name === 'paul') ? +v.qty : 0;
                p.paulToDate ......
        return p;
    },
    function(p, v) {
        --p.count;
  p.name = v.name;
          p.qty -= +v.qty;
          p.qtyPaul -= (p.name === 'paul') ? +v.qty : 0;
               
p.paulToDate ......
        return p;
    },
    function() {
        return {
  count: 0,
          name: 0,
          qty: 0,
          qtyPaul: 0,
          qtyToDate: 0
        };
    }
);

Cheers, Stu

Gordon Woodhull

unread,
Jun 5, 2020, 5:58:48 AM6/5/20
to dc-js-us...@googlegroups.com
No, I don’t think there is any way to do accumulation using a crossfilter reduction, because accumulation is something that works across multiple bins and reduction functions work on individual bins.

However, you could generalize accumulate_group() to work on a particular field of a value object:
function accumulate_group_field(source_group, field, accfield) {
    return {
        all:function () {
            var cumulate = 0;
            return source_group.all().map(function(d) {
                cumulate += d.value[field];
                return {key:d.key, value: {...d.value, [accfield]: cumulate}};
            });
        }
    };
}
var accStuffByDate = accumulate_group_field(stuffByDate, 'qtyPaul', 'paulToDate');

On Jun 5, 2020, at 5:16 AM, Stu <stutr...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "dc-js user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dc-js-user-gro...@googlegroups.com.

Stu

unread,
Jun 5, 2020, 11:32:53 AM6/5/20
to dc-js user group
Cheers Gordon, i'll play around with this.
To unsubscribe from this group and stop receiving emails from it, send an email to dc-js-us...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages