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
UM
New Member

DAX help for previous row calculation

I am trying to calculate the projected inventory. Can someone help me in this.

 

Capture.JPG

1 ACCEPTED SOLUTION

Thanks Ashish. It works great for me now.

View solution in original post

8 REPLIES 8
AlB
Super User
Super User

Hi @UM

 

This would seem to lend itself better to excel but let's give it a try.

 

1. Create a column with week numbers. Extract the week number from the the Attribute column ("Wk 1" will be a 1 (number, not text)). Assign, for instance, a -1 to "Past Wk". We can then use this new column as index. Let's call it WeekNum.

 

2. Use something along these lines for the Projected inventory column:

 

VAR CurrentWk = Table1[WeekNum]
VAR CurrentSOH = Table1[SOH]
VAR PreviousSOH =
    CALCULATE (
        LASTNONBLANK ( Table1[SOH]; 1 );
        Table1[WeekNum] < CurrentWk;
        ALLEXCEPT ( Table1; Table1[Part] )
    )
VAR Sup_Dem = Table1[Supply] - Table1[Demand]
RETURN
    IF ( ISBLANK ( PreviousSOH ); CurrentSOH; PreviousSOH + Sup_Dem )

Note the above code bears the implicit assumption that the weeks are sorted as you show. Were that not the case,  the LASTNONBLANK would yield an erroneus number. You wold then need to check numerically for the previous week number.

 

In actual fact, the column with the WeekNum might not be necessary. I believe we could use the Attribute column as (proxy to an) index as the values are sorted alphabetically.   

Hi AIB , Thanks for your help. As suggested , i added Index and enter the codes. But i am still encountering error. When i try to use the solution, i encounter to use Min/MAX under variable declaration. Thus i tried with Min/MAX declaration and didnt get the desired out. Can please suggest what am i missing here ?

 

The desired output is :

 

Capture.JPG

 

But I am getting the below one in BI :

 

BI.JPG

Hi,

 

You may download my PBI file from here.

 

Hope this helps.

 

Untitled1.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi Ashish,

The solution is amazing.

The download link is not available.

Could you share the file to me?

 

You are welcome.  The link is working fine.  I just clicked on it.  Please try again.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

It is working fine.

Thank you.

 

Thanks Ashish. It works great for me now.

You are welcome.  If my reply helped, please mark it as Answer.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.