I am trying to calculate miles driven for two different companies. I want to make a cell that will add up only data from Company A, and a cell that will add up data from only Company B.

EX:

``````COMPANY       DAYS MILAGE TOTAL        COMPANY A TOTAL     COMPANY B TOTAL
COMPANY A      10                            20               40

COMPANY B      20

COMPANY A      10

COMPANY B      20
``````

Does this make sense?

You can do something like this:

``````=QUERY(A2:B,"SELECT SUM(B) WHERE B IS NOT NULL PIVOT A")
``````

or

``````=SUMIF(\$A2:A,"=" & C2,\$B2:B)    // copy this into the adjacent cell
``````

or

``````=SUM(FILTER(\$B2:B,\$A2:A=C2))    // copy this into the adjacent cell
``````

See example file I created: Milage per Company

Remark

• The first solution is the shortest, because it covers two answers in the same query. It automatically "grows", when you start adding different companies. If you add something in column B, without adding something in column A, the table will get messed up. Add a company in column A again will return the table to normal.
• The second is the most straightforward of them all.
• The third solution allows for some additional tweaking afterwards