Sum if same ID and same date - only once

Mariella

New Member
Joined
Jan 12, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I am trying to sum quantities in a database. Thereby it is important that for each ID, when the date is the same, the value is only added once. I am absolutely struggling to calculate that.

Maybe someone can help me? I would really appreciate it!

Best,
Mariella
 

Attachments

  • simplifiedExcel.PNG
    simplifiedExcel.PNG
    15.8 KB · Views: 49

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
like this?
grp.png


you can do that with Power Query (Get&Transform)
Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    Group = Table.Group(Type, {"ID", "Date"}, {{"Qty", each List.Sum([Quantity]), type number}}),
    Sort = Table.Sort(Group,{{"ID", Order.Ascending}, {"Date", Order.Ascending}})
in
    Sort
 
Upvote 0
There are probably many ways that you could do this, the first one that comes to mind is to add a helper column with adjusted quantities then sum that column.

Try this formula in D2, filled down to D10, then sum column D with a pivot table.

=C2/COUNTIFS($A$2:$A$10,A2,$B$2:$B$10,B2)
Book1
ABCDEFG
1iddateqtyadj qtyIDSum of adj qty
2112/12/201952.5119
3113/12/201955211
4207/12/201952.536
5313/12/201966Grand Total36
6213/12/201963
7213/12/201963
8207/12/201952.5
9112/12/201952.5
10110/12/201999
Sheet7
Cell Formulas
RangeFormula
D2:D10D2=C2/COUNTIFS($A$2:$A$10,A2,$B$2:$B$10,B2)
 
Upvote 0
or like this
grp2.png

Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    Dups = Table.Distinct(Type, {"ID", "Date"}),
    Group = Table.Group(Dups, {"ID", "Date"}, {{"Qty", each List.Sum([Quantity]), type number}}),
    Sort = Table.Sort(Group,{{"ID", Order.Ascending}, {"Date", Order.Ascending}})
in
    Sort
 
Upvote 0
There are probably many ways that you could do this, the first one that comes to mind is to add a helper column with adjusted quantities then sum that column.

Try this formula in D2, filled down to D10, then sum column D with a pivot table.

=C2/COUNTIFS($A$2:$A$10,A2,$B$2:$B$10,B2)
Book1
ABCDEFG
1iddateqtyadj qtyIDSum of adj qty
2112/12/201952.5119
3113/12/201955211
4207/12/201952.536
5313/12/201966Grand Total36
6213/12/201963
7213/12/201963
8207/12/201952.5
9112/12/201952.5
10110/12/201999
Sheet7
Cell Formulas
RangeFormula
D2:D10D2=C2/COUNTIFS($A$2:$A$10,A2,$B$2:$B$10,B2)

Thanks Jason for the quick answer! This is the right idea and works perfectly for the given dataset. Unfortunately, if I have a third time (or more) the same ID and date it doesn't work anymore...
Do you have an idea how I could adjust this. I don't really understand the formula so it is hard to adapt for me.

Best,
Mariella
 
Upvote 0
or like this
View attachment 3768
Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    Dups = Table.Distinct(Type, {"ID", "Date"}),
    Group = Table.Group(Dups, {"ID", "Date"}, {{"Qty", each List.Sum([Quantity]), type number}}),
    Sort = Table.Sort(Group,{{"ID", Order.Ascending}, {"Date", Order.Ascending}})
in
    Sort

Hey :)
This one looks right. I have absolutely no idea how to use code in Excel but I will try to figure that out!
Thanks you!!
Best,
Mariella
 
Upvote 0
select your original table
from the ribbon Data - From Table
then in Power Query Editor open Advanced Editor
and replace all code there with code copied from the post
then Done , Close&Load

after that:
totalrow.gif
 
Last edited:
Upvote 0
There are probably many ways that you could do this, the first one that comes to mind is to add a helper column with adjusted quantities then sum that column.

Try this formula in D2, filled down to D10, then sum column D with a pivot table.

=C2/COUNTIFS($A$2:$A$10,A2,$B$2:$B$10,B2)
Book1
ABCDEFG
1iddateqtyadj qtyIDSum of adj qty
2112/12/201952.5119
3113/12/201955211
4207/12/201952.536
5313/12/201966Grand Total36
6213/12/201963
7213/12/201963
8207/12/201952.5
9112/12/201952.5
10110/12/201999
Sheet7
Cell Formulas
RangeFormula
D2:D10D2=C2/COUNTIFS($A$2:$A$10,A2,$B$2:$B$10,B2)

OMG never mind - it works! It is then dividing by 3. I also understood the formula!
 
Upvote 0
If the same ID and Date always has the same qty like your samples, then without a helper column, you could try this, copied down.

Book1
ABCDEFG
1iddateqtyIDSum Once
2112/12/20195119
3113/12/20195211
427/12/2019536
5313/12/20196
6213/12/20196
7213/12/20196
827/12/20195
9112/12/20195
10110/12/20199
Sum
Cell Formulas
RangeFormula
G2:G4G2=SUMPRODUCT((A$2:A$10=F2)*(MATCH(A$2:A$10&"|"&B$2:B$10,A$2:A$10&"|"&B$2:B$10,0)=ROW(A$2:A$10)-ROW(A$2)+1),C$2:C$10)
 
Upvote 0
If the same ID and Date always has the same qty like your samples, then without a helper column, you could try this, copied down.

Book1
ABCDEFG
1iddateqtyIDSum Once
2112/12/20195119
3113/12/20195211
427/12/2019536
5313/12/20196
6213/12/20196
7213/12/20196
827/12/20195
9112/12/20195
10110/12/20199
Sum
Cell Formulas
RangeFormula
G2:G4G2=SUMPRODUCT((A$2:A$10=F2)*(MATCH(A$2:A$10&"|"&B$2:B$10,A$2:A$10&"|"&B$2:B$10,0)=ROW(A$2:A$10)-ROW(A$2)+1),C$2:C$10)

Hi Peter,
Thank you very much! Indeed, this would work and I thought about this before but my original dataset contains 95000 rows and I would have to list the options before :)

So nice that Excel even provides different ways to come to a solution! Everything worked out!

Best,
Mariella
 
Upvote 0

Forum statistics

Threads
1,214,814
Messages
6,121,711
Members
449,049
Latest member
THMarana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top