I need turnover for last month from my table
1 view (last 30 days)
Show older comments
Hello, I have a table like the one below:
summary =
6×2 table
Time Monthly_Turnover
___________________ _______________
'start' 0
'01-Jul-2019 17:00:39' 76
'24-Jul-2019 14:00:18' 56
'01-Aug-2019 16:00:35' 76
'15-Aug-2019 15:40:24' 98
'21-Aug-2019 13:50:01' 10
I need to get the sum of the Monthly_Turnover-column for a full month based on the timestamp in the first column (from today*). If the timestamp-column does only represent, say, two weeks, I only need the sum of those two weeks.
*) the timeformat is the same at the following line of code:
datestr(datetime('now'))
ans =
'21-Aug-2019 21:22:58'
In this case the result should be: 56+76+98+10 = 240
Does anyone have an idea how to proceed with such a problem? Thanks in advance
0 Comments
Accepted Answer
Star Strider
on 21 Aug 2019
Edited: Star Strider
on 21 Aug 2019
Try this:
dv = datetime({'01-Jul-2019 17:00:39'; '24-Jul-2019 14:00:18'; '01-Aug-2019 16:00:35'; '15-Aug-2019 15:40:24'; '21-Aug-2019 13:50:01'});
tv = [76; 56; 76; 98; 10];
T = table(dv, tv, 'VariableNames',{'Time','Monthly_Turnover'});
TT = table2timetable(T);
TTR = retime(TT, 'monthly','sum')
producing:
TTR =
2×1 timetable
Time Monthly_Turnover
____________________ ________________
01-Jul-2019 00:00:00 132
01-Aug-2019 00:00:00 184
EDIT —
If you only want the month and year in ‘TTR’:
TT = table2timetable(T);
TT.Time.Format = 'MMM-yyyy';
TTR = retime(TT, 'monthly','sum')
produces:
TTR =
2×1 timetable
Time Monthly_Turnover
________ ________________
Jul-2019 132
Aug-2019 184
11 Comments
Star Strider
on 22 Aug 2019
As always, my pleasure.
I initially experimented with setting ‘Something_Else’ to NaN for values <0 , and using the nanmean function, however it did not give what I understood to be the desired result. I may not have completely understood what you wanted.
More Answers (0)
See Also
Categories
Find more on Timetables in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!