MATLAB Answers

0

Sum quarterly rows into annual results of revenue

Asked by Philip Chindamo on 28 Feb 2019
Latest activity Answered by Akira Agata
on 1 Mar 2019
Capture.PNG

  0 Comments

Sign in to comment.

1 Answer

Answer by Akira Agata
on 1 Mar 2019

How about the following?
% Create data table with same structure of yours
Date = repelem(datetime(2017,3,1)+3*calmonths(0:3),1,3)';
Code = repmat(categorical({'abc','def','ghi'}),1,4)';
Quantity = randi(100,12,1);
Price = randi(10,12,1)/10;
T = table(Date,Code,Quantity,Price);
% Calculate yearly revenue for each code
group = findgroups(T.Code);
Revenue =...
splitapply(@(x,y) sum(x.*y),T.Quantity,T.Price,group);

  0 Comments

Sign in to comment.