Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Running Stock total by SKU by date

Hi all,

 

New to DAX, but used Access, Excel, SQL for years.

 

I have a table of stock adjustments. Sales, receipts, manual adjustments, transefers, on hold, off hold etc.

I have created measures to identify Sales (based on adjCode includes SALE) and Receipts (base on adjCode includes GI). 

Now i want to calculate the stock balance of each product (skuID) on each date (adjDate).

I have tried several suggestions, but all seem to work on the principle that a product ID is numerical not text; This being the current formula:

CALCULATE(sum(qryGI_v_Sales[adjQty]),FILTER(ALL(qryGI_v_Sales),qryGI_v_Sales[adjDate] <= max(qryGI_v_Sales[adjDate]) && qryGI_v_Sales[skuID] <= max( qryGI_v_Sales[skuID])))
 
This is not returning the correct stock balances. 
 
I would need this measure to persist through filters/slicers on date and skuID so the stock balance displayed is correct at the date(s) shown.
 
The below is a sample of the data and how the balance measure should evaluate.
 
PWRBI STOCK BAL.PNG
 
 

 

1 ACCEPTED SOLUTION

This would be easiest as a column but you could do it as a measure.

 

Column = 
VAR __table = FILTER('Table',[skuID] = EARLIER([skuID]) && [adjDate] <= EARLIER([adjDate]))
RETURN
SUMX(__table,[adjQty])

Did that free-hand so be wary of syntax errors.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

So what is the expected output for that example data?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi Greg,

 

Thanks for taking the time.

 

The expected output is the last colum - Balance.

 

The data is the first 4 columns (adjDate, skuID, adjQty and adjCode). 

 

The measure needs to be calculating the value shown in the 5th column.

 

Essentially for any given row, i would be looking for the sum of all [adjQty] for that [skuID] upto and including the [adjDate]

 

Perhaps this should this be a column and not a measure?

This would be easiest as a column but you could do it as a measure.

 

Column = 
VAR __table = FILTER('Table',[skuID] = EARLIER([skuID]) && [adjDate] <= EARLIER([adjDate]))
RETURN
SUMX(__table,[adjQty])

Did that free-hand so be wary of syntax errors.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Greg many thanks, that works.

 

Tried it as a measure but got an error, so tried as a colum and worked fine.

 

Can i ask why your solution uses a variable? The below seems to work also?

SUMX(FILTER('Table',[skuID] = EARLIER([skuID]) && [adjDate] <= EARLIER([adjDate])),[adjQty])

 

I like using VAR's in order to make things more readable. Too many bad past experiences with unreadable code. (Perl)


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.