Forum Discussion

alex9999's avatar
alex9999
Icon for Helper I rankHelper I
4 years ago
Solved

Filter CALCULATE SUM by 2 string columns

Hi,   I am trying to create an inventory management system in Power BI to track materials moving through a unit. I need to be able to create measures for every single item that is available to be m...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi alex9999 , would you consider the following for your Inventory Management solution:

     

    DateTimeActionItem KeyQuantity
    01/01/2022 00:00Stocktake110
    15/01/2022 12:23In13
    16/01/2022 17:23Out1-10
    20/01/2022 11:23In15
    01/02/2022 00:00Stocktake17
    15/02/2022 12:23In13
    16/02/2022 17:23Out1-10
    20/02/2022 11:23In15

     

    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