0

I am currently grouping my data by account but also need to group it by salesperson too. However, I cannot figure out how to successfully implement the functionality so that it is grouping by multiple fields at the same time. Could someone offer up some guidance as to how I can do that?

Here is my function that groups my data as of right now:

filteredData[rep.id] = _.chain(filteredData[rep.id]).groupBy('account').map(function(v, i) {
          return {
              customer: i,
              salesperson: _.reduce(v, function(res, val) {
                  return val['salesperson'];
              },0),
              sales_ytd: _.reduce(v, function(res, val) {
                  return res + Number(val['sales - ytd']);
              },0),
              mgn$_ytd: _.reduce(v, function(res, val) {
                  return res + Number(val['margin - ytd']);
              },0),
              sales_last: _.reduce(v, function(res, val) {
                  return res + Number(val['sales - pytd']);
              },0),
              mgn$_last: _.reduce(v, function(res, val) {
                  return res + Number(val['margin - pytd']);
              },0)
    }
}).value();
0

2 Answers 2

1

You need to combine the field values when you need to group by multiple fields.

Example (not tested):

filteredData[rep.id] = _(filteredData[rep.id])
  .groupBy(function(o) {
    return o.account + '-'  + o.salesperson;
  })
  .map(function(v, i) {
    var customer = _.head(_.words(i));

    return {
        customer: customer,
        salesperson: _.reduce(v, function(res, val) {
            return val['salesperson'];
        },0),
        sales_ytd: _.reduce(v, function(res, val) {
            return res + Number(val['sales - ytd']);
        },0),
        mgn$_ytd: _.reduce(v, function(res, val) {
            return res + Number(val['margin - ytd']);
        },0),
        sales_last: _.reduce(v, function(res, val) {
            return res + Number(val['sales - pytd']);
        },0),
    mgn$_last: _.reduce(v, function(res, val) {
        return res + Number(val['margin - pytd']);
    },0)
};

}).value();

Sign up to request clarification or add additional context in comments.

1 Comment

this works great and solved the problem...thank you!
0

It is a little unclear to understand your code without the input data and the expected result, but, I believe that you are grouping the data and formatting the result on one sweep. maybe you could consider separating the concerns of the function writing:

const formatData = (object, index) => {
  return =  {
    customer: index,
    salesperson: _.reduce(object, function(res, val) {
        return val['salesperson'];
    },0),
    sales_ytd: _.reduce(object, function(res, val) {
        return res + Number(val['sales - ytd']);
    },0),
    mgn$_ytd: _.reduce(object, function(res, val) {
        return res + Number(val['margin - ytd']);
    },0),
    sales_last: _.reduce(object, function(res, val) {
        return res + Number(val['sales - pytd']);
    },0),
    mgn$_last: _.reduce(object, function(res, val) {
        return res + Number(val['margin - pytd']);
    },0)
  }
}

Then you could iterate over a list of grouping clases and return an object containing several groups:

const groupFormatedDataBy = ( ...groupingClasses ) => {
  let output = {}
  grupingClasses.forEach(groupingClass => {

    output[groupingClass] = _.chain(filteredData[rep.id])
       .groupBy(groupingClass)
       .map( formatData )
       .value();

  })

  return output
}

PS: I could not test the code without sample data jejeje, hope it helps you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.