Conditional Cumulative Sum based on multiple columns - excel

I have a simple inventory table in excel that looks like this:
Number of Items | Date Incoming | Date Out
-------------------------------------------------------
10 | 1 Jan 2018 | 30 Jan 2018
30 | 15 Jan 2018 | 1 May 2018
20 ! 1 Feb 2018 | 15 Mar 2018
I would like something that can give me the the total number of items that are present in the inventory at each date, that is:
1 Jan 2018 | 10
15 Jan 2018 | 40
30 Jan 2018 | 30
1 Feb 2018 | 50
15 Mar 2018 | 30
1 May 2018 | 0
What I was thing is some sort of cumulative sum where the number of items are added at "Date Incoming" and substracted at "Date Out".
Can you help me? I would prefer to avoid macros but even a vba solution if fine.

For a given date, you can do:
=sumif(#DateIn, "<="&#CellWithGivenDate, #NumberOfItems) - sumif(#DateOut, "<="&#CellWithGivenDate, #NumberOfItems)
With #NumberOfItems, #DateIn, and #DateOut being columns 1 to 3 of your sample, and #CellWithGivenDate being the relevant cell in column 1 of your expected result sample.

Related

SQL aggregating a text field based on latest date text value

I have the following script:
DROP TABLE IF EXISTS [dbo].[test]
CREATE TABLE [dbo].[test]
(
[Name] [varchar](50) NULL,
[Amount] [int] NULL
)
GO
INSERT INTO [dbo].[test]
(
[Name]
,[Amount]
)
VALUES
('Abc - 20 april to 7 june 2020',25)
,('Abc - 20 april to 7 june 2020',33)
,('Abc - 20 april to 29 june 2020',15)
,('Abc - 20 april to 29 june 2020',55)
,('Abc - 20 april to 10 may 2020',20)
,('Abc - 20 april to 10 may 2020',75)
,('Abc - 20 april to 10 may 2020',89)
GO
SELECT *
FROM [dbo].[test]
The resulting table gives the following results:
Name | Amount
-------------------------------|-------
Abc - 20 april to 7 june 2020 | 25
Abc - 20 april to 7 june 2020 | 33
Abc - 20 april to 29 june 2020 | 15
Abc - 20 april to 29 june 2020 | 55
Abc - 20 april to 10 may 2020 | 20
Abc - 20 april to 10 may 2020 | 75
Abc - 20 april to 10 may 2020 | 89
I would like to be able to determine the most recent end date in the text field and eliminate the repetitive data by grouping it showing the record with the latest end date and aggregating the amount. The result should be a single record with the following data:
Name | Amount
-------------------------------|-------
Abc - 20 april to 29 june 2020 | 312
I've played around with some group bys and text manipulation functions and this is as far as I got using the following code:
select (case when [Name] like '%-%'
then trim(left([Name], charindex('-', reverse([Name])) - 1))
else [Name]
end) as [Name]
,sum(Amount) as Amount
from [dbo].[test]
group by
(case when [Name] like '%-%'
then trim(left([Name], charindex('-', reverse([Name])) - 1))
else [Name]
end)
The above code doesn't really do so much as to just aggregate unique records only. I was looking for something more intelligent that will find and recognize that all of my records really mean the same thing and that the latest end date in the Name field is 29 june 2020 and will only display that single record with the total aggregated Amount.
Any help would be much appreciated.

Subtracting row values in Cognos

I have the following report:
Id Product Revenue Month
1 A 302 jan
1 A 342 feb
1 A 133 mar
For this report, I need to find the difference in revenues for months, i.e difference in revenue from Jan to Feb and feb to march.
I want to add another column like this:
Id Product Revenue Month Profit
1 A 302 jan
1 A 342 feb 40
1 A 142 mar -200
I can write a case statement for it and create a Calculated field but I don't know how to refer the rows to do my subtraction. Can someone help me with this.
What happens when you try running-difference([Revenue])?

Get specific day of week of the upcoming Canada day on Linux

How to use cal command to add the calendar of next July to the end of the file, for example, myfile, and what day of the week the upcoming Canada Day fall on?
So far I just have this command:
cal July 2017 >> myfile
I feel like I am not doing it correct and I don't know which command to use, to find the day of the week for specific date.
Use this command:
cal 7 2017 >> file
The output is:
July 2017
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
You can find out day of week of a particular day with the GNU date command:
date -d"2017-07-01" # what day of week is Canada Day this year?
=> Sat Jul 1 00:00:00 UTC 2017
If you just want the week day, then:
date -d"2017-07-01" +%A
=> Saturday
You can check more about these commands with man cal or man date.
On a Mac, you could do this:
date -j -vJulm -v1d -v2017y +%A
See more on this post: date command on Mac OS

Parse CSV file with some dynamic columns

I have a CSV file that I receive once a week that is in the following format:
"Item","Supplier Item","Description","1","2","3","4","5","6","7","8" ...Linefeed
"","","","Past Due","Day 13-OCT-2014","Buffer 14-OCT-2014","Week 20-OCT-2014","Week 27-OCT-2014", ...LineFeed
"Part1","P1","Big Part","0","0","0","100","50", ...LineFeed
"Part4","P4","Red Part","0","0","0","35","40", ...LineFeed
"Part92","P92","White Part","0","0","0","10","20", ...LineFeed
...
An explanation of the data - Row 2 is dynamic data signifying the date parts are due. Row 3 begins the part numbers with description and number of parts due on a particular date. So looking at the above data: row 3 column7 shows that PartNo1 has 100 parts due on the Week of OCT 20 2014 and 50 due on the Week of OCT 27, 2014.
How can I parse this csv to show the data like this:
Item, Supplier Item, Description, Past Due, Due Date Amount Due
Part1 P1 Big Part 0 20 OCT 2014 100
Part1 P1 Big Part 0 27 OCT 2014 50
Part4 P4 Red Part 0 20 OCT 2014 35
Part4 P4 Red Part 0 27 OCT 2014 40
....
Is there a way to manipulate the format in Excel to rearrange the data like I need or what is the best method to resolve this?

Problems with converting text to date (YYYY-MM-DD) in Excel (Mac)

I have been able to convert this date to YYYY-MM-DD HH:mm but not anymore. What can I do to convert this date.
Sep 15, 2014 9:30:32 AM
You need to know that I'm using Swedish keyboard, date and region.
Example:
Order # Purchased On
100026881 Sep 15, 2014 9:30:32 AM
100026880 Sep 15, 2014 9:10:56 AM
100026879 Sep 15, 2014 9:09:10 AM
100026878 Sep 15, 2014 9:03:27 AM
100026877 Sep 15, 2014 8:57:02 AM
100026876 Sep 15, 2014 8:38:37 AM
100026875 Sep 15, 2014 6:54:29 AM
100026874 Sep 15, 2014 5:03:23 AM
100026873 Sep 15, 2014 2:45:50 AM
100026872 Sep 15, 2014 1:42:26 AM
100026871 Sep 14, 2014 11:20:31 PM
100026870 Sep 14, 2014 11:16:29 PM
100026869 Sep 14, 2014 11:11:15 PM
100026868 Sep 14, 2014 11:10:06 PM
100026867 Sep 14, 2014 10:42:56 PM
100026866 Sep 14, 2014 10:41:22 PM
100026865 Sep 14, 2014 10:36:43 PM
100026863 Sep 14, 2014 10:26:13 PM
Formatting a date in Excel 2011 for Mac
You have at least three different ways to apply a date format. Perhaps the fastest is to select a cell or cell range, and then click the Home tab of the Ribbon. In the Number group, click the pop-up button under the Number group title and choose Date to display the date as m/d/yy, where m represents the month's number, d represents the day number, and yy represents a two-digit year.
Excel has many more built-in date formats, which you can apply by displaying the Format Cells dialog by pressing Command-1 and then clicking the Number tab. You can also display the Number tab of the Format Cells dialog by clicking the Home tab on the Ribbon. Then click the pop-up button under the Number group title and choose Custom from the pop-up menu.
When the Format Cells dialog displays, select the Date category. Choose a Type from the list. Choosing a different Location (language) or Calendar type changes the date types offered.
I hope this may helps..
This should be a comment since I have neither Swedish settings not a Mac but I am suggesting a lookup table:
+-----+----+
| Jan | 1 |
| Feb | 2 |
| Mar | 3 |
| Apr | 4 |
| May | 5 |
| Jun | 6 |
| Jul | 7 |
| Aug | 8 |
| Sep | 9 |
| Oct | 10 |
| Nov | 11 |
| Dec | 12 |
+-----+----+
say named Marray, along with:
=TEXT(DATE(MID(B2,9,4),VLOOKUP(LEFT(B2,3),Marray,2,0),MID(B2,5,2))+VALUE(TRIM(RIGHT(B2,11))),"[$-41D]mmmm dd, yyyy h:mm:ss AM/PM")
in C2 and copied down to suit (assuming Sep 15, 2014 9:30:32 AM is in B2).
For single digit dates, perhaps:
=TEXT(DATE(TRIM(MID(B2,8,5)),VLOOKUP(LEFT(B2,3),Marray,2,0),SUBSTITUTE(TRIM(MID(B2,4,3)),",",""))+VALUE(TRIM(RIGHT(B2,11))),"[$-41D]mmmm dd, yyyy h:mm:ss AM/PM")
For me (Windows, Excel 2013, English!) this returns:
It may be necessary to replace all ,s with ;, except one inside SUBSTITUTE.
I think that Jeeped might be close to the problem.
My guess is that now the data may have been pasted as text, instead of recognizing the date. (pnuts had an answer but it's a lot more work than using the builtin Excel functions.)
If the dates are in their own column, like:
Sep 15, 2014 9:30:32 AM
Sep 15, 2014 9:10:56 AM
Sep 15, 2014 9:09:10 AM
etc
Then you might have to get Excel to parse the text dates.
If the date text is in B2, put this formula in another cell (say B3):
=DATEVALUE(B2) + TIMEVALUE(B2)
Then you can format with this custom formatting string: yyyy-mm-dd h:mm:ss AM/PM
Which will give you:
2014-09-15 9:30:32 AM
2014-09-15 9:10:56 AM
2014-09-15 9:09:10 AM
etc.
Hope this helps.

Resources