This is something that most places struggle with. The way I have this working is I first create a flattened BOM file. Then I create a related file to pull the correct inventory levels.
You could possibly do this in one statement with a case statement like:
SUM(CASE
When ITEM = ITEM AND Warehouse = X
Then SUM (Quantity On hand Calculation)
Else 0
End),