Forum Discussion
Filter CALCULATE SUM by 2 string columns
- Anonymous4 years ago
Hi alex9999 , would you consider the following for your Inventory Management solution:
DateTime Action Item Key Quantity 01/01/2022 00:00 Stocktake 1 10 15/01/2022 12:23 In 1 3 16/01/2022 17:23 Out 1 -10 20/01/2022 11:23 In 1 5 01/02/2022 00:00 Stocktake 1 7 15/02/2022 12:23 In 1 3 16/02/2022 17:23 Out 1 -10 20/02/2022 11:23 In 1 5 The features of this approach is that In and Out quantity movements are assigned + and - sign accordingly, so it make summing to find the current Inventory quantity much easier. The second feature is the addition of the Stocktake action. This allows you to reset your inventory without reporting as In and Out. This could make it easier to track missing quantity or incorrect In/Out records.
Note you should create a Item table. This table will have the Item Key (primary) and Item details. You can also add a calculated column with the current quantity using the following DAX expression (note this is the quantity at point of time the model is refreshed):
Current Item Quantity = //Calculated Column for Item table
VAR _LastStocktake = SUMMARIZE( FILTER(Inventory, Inventory[Action] = "Stocktake") , "Last Stocktake", MAX(Inventory[DateTime]))
VAR _StockMovementsSinceLastStockTake = CALCULATE( SUM(Inventory[Quantity]) , Inventory[DateTime] >= _LastStocktake )
RETURN
_StockMovementsSinceLastStockTakeFrom a measure perspective, the following allows you to show stock levels over time:
Daily Stock Level =
//Measure
VAR _Test =
ISFILTERED ( 'Item'[Item Key] )
VAR _Date =
MIN (
MIN ( 'Calendar'[Date] ),
TODAY ()
)
VAR _LastStocktake =
SUMMARIZE (
FILTER (
Inventory,
Inventory[DateTime] <= _Date
&& Inventory[Action] = "Stocktake"
),
"LastStockTake", MAX ( Inventory[DateTime] )
)
RETURN
IF (
_Test,
CALCULATE (
SUM ( Inventory[Quantity] ),
Inventory[DateTime] >= _LastStocktake,
Inventory[DateTime] <= _Date
),
"No Result"
)Note the Isfilter stops the measure from calculating when the item is not filtered
Hi alex9999 , would you consider the following for your Inventory Management solution:
| DateTime | Action | Item Key | Quantity |
| 01/01/2022 00:00 | Stocktake | 1 | 10 |
| 15/01/2022 12:23 | In | 1 | 3 |
| 16/01/2022 17:23 | Out | 1 | -10 |
| 20/01/2022 11:23 | In | 1 | 5 |
| 01/02/2022 00:00 | Stocktake | 1 | 7 |
| 15/02/2022 12:23 | In | 1 | 3 |
| 16/02/2022 17:23 | Out | 1 | -10 |
| 20/02/2022 11:23 | In | 1 | 5 |
The features of this approach is that In and Out quantity movements are assigned + and - sign accordingly, so it make summing to find the current Inventory quantity much easier. The second feature is the addition of the Stocktake action. This allows you to reset your inventory without reporting as In and Out. This could make it easier to track missing quantity or incorrect In/Out records.
Note you should create a Item table. This table will have the Item Key (primary) and Item details. You can also add a calculated column with the current quantity using the following DAX expression (note this is the quantity at point of time the model is refreshed):
Current Item Quantity = //Calculated Column for Item table
VAR _LastStocktake = SUMMARIZE( FILTER(Inventory, Inventory[Action] = "Stocktake") , "Last Stocktake", MAX(Inventory[DateTime]))
VAR _StockMovementsSinceLastStockTake = CALCULATE( SUM(Inventory[Quantity]) , Inventory[DateTime] >= _LastStocktake )
RETURN
_StockMovementsSinceLastStockTake
From a measure perspective, the following allows you to show stock levels over time:
Daily Stock Level =
//Measure
VAR _Test =
ISFILTERED ( 'Item'[Item Key] )
VAR _Date =
MIN (
MIN ( 'Calendar'[Date] ),
TODAY ()
)
VAR _LastStocktake =
SUMMARIZE (
FILTER (
Inventory,
Inventory[DateTime] <= _Date
&& Inventory[Action] = "Stocktake"
),
"LastStockTake", MAX ( Inventory[DateTime] )
)
RETURN
IF (
_Test,
CALCULATE (
SUM ( Inventory[Quantity] ),
Inventory[DateTime] >= _LastStocktake,
Inventory[DateTime] <= _Date
),
"No Result"
)
Note the Isfilter stops the measure from calculating when the item is not filtered