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
rderrickwhite
Helper I
Helper I

DAX FORMULA TO DECREMENT ROW BY ROW IN A COLUMN, BASED ON DATA FROM OTHER COLUMNS AND ROWS

I'm attempting to write a formula that decrements inventory in a column based on demand and qty available.  In the attached picture, column c is being decremented in column d, incrementally, based on the number of occurrences in column b.

 

Capture.PNG

1 ACCEPTED SOLUTION
Cmcmahan
Resident Rockstar
Resident Rockstar

You need some sort of index or other sortable column that indicates the order of these events.  Otherwise, if the column isn't in this exact order (which is not guaranteed by the DAX engine), you'll get results that don't make sense.

 

Once you have that index set up, you can simplify this math down to subtracting a count of the rows from this table where the index/sorting column is less than the current index from the initial stock qty.

 

Also, are you setting this up as a measure, or a calculated column?  I would suggest changing your set up and creating this up as a measure, but here's the DAX for a calculated column, since that looks like what you're creating.

QTY_REMAINING = 'Table'[stock qty] - CALCULATE(COUNTROWS('Table'), FILTER(ALLEXCEPT('Table', 'Table'[unit], 'Table'[part needed]), 'Table'[Index] <= EARLIER('Table'[Index])) 

 

View solution in original post

2 REPLIES 2
Cmcmahan
Resident Rockstar
Resident Rockstar

You need some sort of index or other sortable column that indicates the order of these events.  Otherwise, if the column isn't in this exact order (which is not guaranteed by the DAX engine), you'll get results that don't make sense.

 

Once you have that index set up, you can simplify this math down to subtracting a count of the rows from this table where the index/sorting column is less than the current index from the initial stock qty.

 

Also, are you setting this up as a measure, or a calculated column?  I would suggest changing your set up and creating this up as a measure, but here's the DAX for a calculated column, since that looks like what you're creating.

QTY_REMAINING = 'Table'[stock qty] - CALCULATE(COUNTROWS('Table'), FILTER(ALLEXCEPT('Table', 'Table'[unit], 'Table'[part needed]), 'Table'[Index] <= EARLIER('Table'[Index])) 

 

That worked perfectly.  Thanks so much!

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.