cancel
Showing results for
Search instead for
Did you mean:
Fizicks1 Regular Visitor

## DAX Formula: Inventory QOH in reverse

Disclaimer: I just started working with DAX and I've read and watched a ton of content but I'm still getting hung up on very simple problems.  I learn faster if I can see the solutions to the problems in my head.

I have an inventory table which holds all item specifics: Quantity on Hand, PAR, Location, etc.

I also have a transaction table that shows all of the (+/-) transactions that happened to any given item.

Instead of simply calculating a units in stock like:

UnitsInStockSimple =
CALCULATE (
SUM ( 'Transaction Data'[Qty] ),
FILTER (
ALL ( 'Date Table'[Date] ),
'Date Table'[Date] <= MAX( 'Date Table'[Date] )
)
)

I want to work in reverse and pick up the QOH field from the Inventory table and work backwards through the dates.  The final result should be the QOH on for any given date:

Example for item 123

Current QOH is 400

Yesterday (during 1 day) there were 3 transactions in desc order: -3 (distribution), -1 (Adjustment), +6 (order receipt)

I would like to see the QOH for each date: 403, 404, 398

Because: If the last transaction subtracted 3 and today's QOH is 400, then the QOH before the transaction must have been 403.

11 REPLIES 11
Phil_Seamark Super Contributor

## Re: DAX Formula: Inventory QOH in reverse

I haven't tested this but have you tried reversing the operator?

```UnitsInStockSimple =
CALCULATE (
SUM ( 'Transaction Data'[Qty] ),
FILTER (
ALL ( 'Date Table'[Date] ),
'Date Table'[Date] >= MAX( 'Date Table'[Date] )
)
)
```

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Fizicks1 Regular Visitor

## Re: DAX Formula: Inventory QOH in reverse

Thanks,

I will try that in the morning at work.  The original problem with that formula is that it doesn't pull 'Inventory Data'[QOH] at all.  The transactions are in: 'Transaction Data' [Qty].

Also, that formula only shows me the total per date, while the sum of all totals does accurately reflect the current QOH, that's not entirely what I want.  I just want to be able to show a date and show the QOH for that date/item. (Maybe I'm displaying that measure incorrectly?)

v-ljerr-msft Super Contributor

## Re: DAX Formula: Inventory QOH in reverse

Hi @Fizicks1,

Could you try the formula(untested) below to see if it works in your scenario? ```Inventory QOH =
VAR currentQOH =
MAX ( 'Inventory Data'[QOH] )
VAR maxDate =
CALCULATE ( MAX ( 'Date Table'[Date] ), ALL ( 'Date Table' ) )
VAR currentDate =
MAX ( 'Date Table'[Date] )
RETURN
currentQOH
- CALCULATE (
SUM ( 'Transaction Data'[distribution] )
+ SUM ( 'Transaction Data'[Adjustment] )
+ SUM ( 'Transaction Data'[order receipt] ),
FILTER (
ALL ( 'Date Table'[Date] ),
'Date Table'[Date] >= currentDate
&& 'Date Table'[Date] <= maxDate
)
)
```

Regards

Highlighted
Fizicks1 Regular Visitor

## Re: DAX Formula: Inventory QOH in reverse   Thanks, I learned a lot from that formula.  I assume it was meant to be a measure but I tried both and the result is always 12,955,306 for every date and every item.  It appears it might be adding up all QOH in my Inventory Data table?

Also, the only field I have qty in is the 'Transaction Data' [Qty]. there is another field: 'Transaction Data' [Transaction Type] which captures "adjustment", "Receipt", etc...  I changed the 3 lines on your formula to : SUM ( 'Transaction Data'[Qty] ),

v-ljerr-msft Super Contributor

## Re: DAX Formula: Inventory QOH in reverse

Hi @Fizicks1,

Could you share a sample pbix file, so that we can better assist on this issue? You can upload it to OneDrive or Dropbox and post the link here. Do mask sensitive data before uploading. Regards

Fizicks1 Regular Visitor

## Re: DAX Formula: Inventory QOH in reverse

Thanks @v-ljerr-msft I sent you a private message with some options that I have to directly exchange files but most all types of file sharing sites/functions are turned off in my organization.

MAAbdullah47 Member

## Re: DAX Formula: Inventory QOH in reverse

I have some issue related to inventory is there a problem to share me the Pbix file in private email?

Fizicks1 Regular Visitor

## Re: DAX Formula: Inventory QOH in reverse

I would like to restart this ask in hopes that I will explain it better.

I have a perpetual inventory system where the program records all the previous transactions in a [Transaction] Table.  It also records all of the item inventory information including the Current QOH in an [Item] table. Below is a quick sample of the 2 tables combined.

The calculation that I need is the QOH for the previous transactions (i've added them in using a formula: Last Known QOH - Current Transaction = QOH after Transaction occured.)  IE: QOH for Apples is 10 and is stored in the [Item] Table.... 10 - 10 = 0.. so the previous QOH must have been 0 after the transaction occured.  Moving in reverse... 0- (5) = +5...

I would like this value to be in a column (in the Transaction Table) so that I can graph it and run measures against it.. IE: How many days have elapsed since the QOH was 0?... etc...

There are about 1.5 million lines and 300k items.  Would this make more sense to do in SQL prior to data pull? (not sure how'd I'd do that either)  Or would M code be a better candidate?

Here is the example.... The last number in the QOH column (for each item) is the known QOH from the [Item] Table.  The remainder QOH numbers before it are what I need to calculate.

 Category Date/Time Qty change QOH Apples 11/13/18 1:00PM -2 10 Apples 11/13/18 2:00PM -3 7 Apples 11/13/18 3:00PM -2 5 Apples 11/13/18 4:00PM -5 0 Apples 11/13/18 5:00PM 10 10 Banannas 11/12/18 6:00AM -30 100 Banannas 11/12/18 8:00AM -30 70 Banannas 11/13/18 1:00PM -30 40 Banannas 11/13/18 2:00PM 60 100 Banannas 11/13/18 3:00PM -30 70 Banannas 11/13/18 4:00PM -30 40 Banannas 11/13/18 5:00PM -30 10 Oranges 11/12/18 6:00AM -100 100 Oranges 11/12/18 8:00AM 100 200 Oranges 11/13/18 1:00PM -100 100 Oranges 11/13/18 2:00PM 100 200 Oranges 11/13/18 3:00PM -100 100 Oranges 11/13/18 4:00PM 100 200 Oranges 11/13/18 5:00PM -100 100

I've looked everywhere for this solution and I've tried several iterations of "Earlier" to get it to work but I'm still having trouble.  The closest I can find that somewhat resembles what I want to do is:

https://community.powerbi.com/t5/Desktop/Inventory-turnover-by-month/m-p/410252/highlight/true#M1885...

Nick_M New Contributor

## Re: DAX Formula: Inventory QOH in reverse

I decided to take a shot... Using the data you provided I had to add a row for each category as a "beginning" balance.  From there created an index using EARLIER (well, variables defined before the calculation which is the same as using EARLIER)

```Index =
VAR CurrentCategory = Table1[Category]
VAR CurrentDate = Table1[Date/Time]
RETURN

CALCULATE(
COUNTROWS(
FILTER ( ALL ( Table1),
CurrentCategory = Table1[Category]
&& CurrentDate >= Table1[Date/Time]
)
)
)```

That tells me how many rows are less then or equal to the current row's data/time, and in the same category: So then I can use that as a reference point of what came before the current row:

```QOH =
VAR CurrentCat= Table1[Category]
VAR CurrentIndex = Table1[Index]
VAR CurrentQtyChg= Table1[Qty change]

RETURN
CALCULATE(
SUM ( Table1[Qty change]),
FILTER(
Table1,
CurrentCat = Table1[Category]
&& CurrentIndex >= Table1[Index]
)
)

```

Then the final output: Maybe what you were looking for?