Forum Discussion

mwimberger's avatar
mwimberger
Icon for Resolver II rankResolver II
7 years ago

Inventory changes using DAX - intra-location opening and closing dilemma

 

Hi Folks

 

I have a interesting problem to solve. It involves calculating opening and closing balances for inventory locations or stockpiles. The operation has a material tracking system which tracks material moved from  source and to destinations for an open pit operation. In most cases, the opening and closing balances are solved using the following measures:

 

Opening Stock Tonnes =
CALCULATE ( [Tonnage Movement],
     FILTER (
         ALLEXCEPT ( Data, Data[Stockpile_ID],Data[Stockpile_ID_Detail] )
       ,'Data'[Date] < min('Data'[Date])
    )
	

Closing Stock Tonnes = 
CALCULATE (
    [Tonnage Movement],
    FILTER (
        ALLEXCEPT ( Data, Data[Stockpile_ID] ),
        'Data'[Date] <= MAX ( 'Data'[Date] )
    )
)

Tonnage Movement =
CALCULATE ( SUMX ( 'Data', 'Data'[Tonnes] ), 'Data'[In_Code] = "IN" )
    - CALCULATE ( SUMX ( 'Data', 'Data'[Tonnes] ), 'Data'[In_Code] = "OUT" )

This all works fine until there are intrastockpile movements - or movements between stockpiles. It shouldn't occur - but reality is of course at work.

 

The problem is summarized in the following pictures:

The issue occurs at the exclamation point ! above - in that Stockpile C has tonnes subtracted, Stockpile D has tonnes added, all in one transaction line in the database. My "In_Code" column isn't able to obviously handle both an "In" and "Out" code simulaneously.

The above spreadsheet is an attempt to show in formulas what should  happen to the tonnages.

 

Does anyone have an idea to handle this elegantly in DAX? I have about 1.4M rows of transactions and I know I need to do some optimising on the calcs ( I think my SUMX iterators are possibly slowing things down) plus the fact the users want this information in a matrix table which is also adding to overheads. But first things first - how to handle the intra-stockpile movements? 🤷‍♂️😥

Any help would be hugely appreciated. Been trying /struggling with USERELATIONSHIP but I was getting out of memory issues, and I am flat out of hair to pull out.

 

Thanks in advance

Manfred

5 Replies

  • Hi Folks

     

    I have a interesting problem to solve. It involves calculating opening and closing balances for inventory locations or stockpiles. The operation has a material tracking system which tracks material moved from  source and to destinations for an open pit operation. In most cases, the opening and closing balances are solved using the following measures:

     

    Opening Stock Tonnes =
    CALCULATE ( [Tonnage Movement],
         FILTER (
             ALLEXCEPT ( Data, Data[Stockpile_ID],Data[Stockpile_ID_Detail] )
           ,'Data'[Date] < min('Data'[Date])
        )
    	
    
    Closing Stock Tonnes = 
    CALCULATE (
        [Tonnage Movement],
        FILTER (
            ALLEXCEPT ( Data, Data[Stockpile_ID] ),
            'Data'[Date] <= MAX ( 'Data'[Date] )
        )
    )
    
    Tonnage Movement =
    CALCULATE ( SUMX ( 'Data', 'Data'[Tonnes] ), 'Data'[In_Code] = "IN" )
        - CALCULATE ( SUMX ( 'Data', 'Data'[Tonnes] ), 'Data'[In_Code] = "OUT" )

    This all works fine until there are intrastockpile movements - or movements between stockpiles. It shouldn't occur - but reality is of course at work.

     

    The problem is summarized in the following pictures:

    The issue occurs at the exclamation point ! above - in that Stockpile C has tonnes subtracted, Stockpile D has tonnes added, all in one transaction line in the database. My "In_Code" column isn't able to obviously handle both an "In" and "Out" code simulaneously.

    The above spreadsheet is an attempt to show in formulas what should  happen to the tonnages.

     

    Does anyone have an idea to handle this elegantly in DAX? I have about 1.4M rows of transactions and I know I need to do some optimising on the calcs ( I think my SUMX iterators are possibly slowing things down) plus the fact the users want this information in a matrix table which is also adding to overheads. But first things first - how to handle the intra-stockpile movements? 🤷‍♂️😥

    Any help would be hugely appreciated. Been trying /struggling with USERELATIONSHIP but I was getting out of memory issues, and I am flat out of hair to pull out.

     

    Thanks in advance

    Manfred

  • roset03's avatar
    roset03
    Frequent Visitor
    Hi Manfred,

    I also need to solve a similar problem, also for ore block to stockpile to crusher movements for an open pit operation... with an identical table.
    Just wondering if you managed to solve this one, and if you could kindly share some details!

    Many thanks,

    Tim
    • mwimberger's avatar
      mwimberger
      Icon for Resolver II rankResolver II

      Hi Tim

       

      I did manage to solve this. My initial solution was to use USERELATIONSHIP, but this wasn't completely without issue as it seemed to slow things down.

      What I then settled on was a second referenced query ( subset of the the initial data)  and a relationship of this query to the Distinct list of locations ( in and outs) 
      I will detail exactly how I did this with a longer post soon. Good to see some mining related interest out there! 

      Cheers 

      Manfred

      • roset03's avatar
        roset03
        Frequent Visitor

        Appreciate the update Manfred! Would be great if you could update us in due course. Hopefully we can compare notes, and work a really nice solution.

         

        I assume your end goal is similar to mine - for blend planning for crusher feed grade?

         

        Many thanks!

        Tim