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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

Reply
arun0386
Frequent Visitor

Cumulative calculation with Calculated column

Hi, I am trying to do cumulative calculation. I have inventory data till latest week and i have forecast delta for remaining weeks. I need this calculation :  if inventory data is there, then forecast is the same as inventory, otherwise add previous week inventory with forecast data to create a new forecast. I tried Max, earlier, coalesce, but all failed. even saw an example with ALLEXCEPT, but it didnt work. Please help. Sorry, i am new to Power BI. Below is an example of how it is power bi and how i want it in excel highlighted in yellow.

 

arun0386_1-1730324182485.png

 

arun0386_0-1730324130285.png

 

1 ACCEPTED SOLUTION

Hi @arun0386 ,
Now that you're able to retrieve week 34, this code should definitely work:

 

Step.01.DrillDown = 
VAR _LastWeek = CALCULATE( MAX('Table'[Week]), FILTER( ALL('Table'), NOT(ISBLANK('Table'[Inventory]))) )
RETURN
IF( 
    // checks to see if I have an inventory week
    SELECTEDVALUE('Table'[Week]) > _LastWeek, 
    // if not, let's "fill down" and return the value for the last week
    CALCULATE(SUM('Table'[Inventory]), KEEPFILTERS('Table'[Week] = _LastWeek), ALL('Table')), 
    // if there is, return me normal inventory value
    SUM('Table'[Inventory])
)

 



Sharing the pbix that I used for your example so you can take a look at it as a reference.

window-function-previous-row.pbix (link 1)

window-function-previous-row.pbix (link 2)

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

10 REPLIES 10
arun0386
Frequent Visitor

Cant seem to access the pbix file. Can you re-share please? and thanks a lot again.

I've added a secondary link. you can try again.



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
arun0386
Frequent Visitor

From the first query

arun0386_0-1730415225104.png

From the adjusted one

arun0386_1-1730415449324.png

 

Hi @arun0386 

Looks like we found your issue. Seems it's attempting to pick up week 53 but your maximum week it should pick up is week 34. 

I've modified the last week variable a bit. Hopefully this should return the desired week 34.

Send me a screenshot of the output

Step.01.DrillDown =  
VAR _LastWeek = CALCULATE( MAX('Table'[Week]), FILTER( ALL('Table'), NOT(ISBLANK('Table'[Inventory]))) )

RETURN

_LastWeek

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Any calculation other than a hardcoded numeric value isnt populating. LOL. FYI, inventory is a column in the table. rest everything is a measure.

 

arun0386_0-1730480704780.png

 

Hi @arun0386 ,
Now that you're able to retrieve week 34, this code should definitely work:

 

Step.01.DrillDown = 
VAR _LastWeek = CALCULATE( MAX('Table'[Week]), FILTER( ALL('Table'), NOT(ISBLANK('Table'[Inventory]))) )
RETURN
IF( 
    // checks to see if I have an inventory week
    SELECTEDVALUE('Table'[Week]) > _LastWeek, 
    // if not, let's "fill down" and return the value for the last week
    CALCULATE(SUM('Table'[Inventory]), KEEPFILTERS('Table'[Week] = _LastWeek), ALL('Table')), 
    // if there is, return me normal inventory value
    SUM('Table'[Inventory])
)

 



Sharing the pbix that I used for your example so you can take a look at it as a reference.

window-function-previous-row.pbix (link 1)

window-function-previous-row.pbix (link 2)

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

The revised query is giving 34 as the latest week. And i also tried adding the query line from Message 4 to get 100 in blank rows; it worked; however, when i changed it to the query line from your first response, it doesnt fill down.

arun0386_0-1730476670223.png

 

arun0386
Frequent Visitor

Hi @hnguy71 , Thanks for responding. Step 2 & 3 worked like a charm. But something is not working in Step 1. it is not filling up the following weeks like your screenshot. mystery!!!

arun0386_0-1730412749196.png

 

Hi @arun0386 

Glad it's halfway working for ya! We'll need to debug as to understand why the first measure is not filling down. Let's modify the first measure twice and see what we return.

Adjust to this expression and share me the screenshot of the result:

Step.01.DrillDown = 
VAR _LastWeek = CALCULATE(MAX('Table'[Week]), 'Table'[Inventory] <> BLANK(), ALL('Table'))
RETURN
_LastWeek & " | " & SELECTEDVALUE('Table'[Week])


Then, adjust the expression again and share me the screenshot of the next result:

Step.01.DrillDown = 
VAR _LastWeek = CALCULATE(MAX('Table'[Week]), 'Table'[Inventory] <> BLANK(), ALL('Table'))

RETURN

IF( 
    // checks to see if I have an inventory week
    SELECTEDVALUE('Table'[Week]) > _LastWeek, 
    // if not, let's "fill down" and return the value for the last week
    100, 
    // if there is, return me normal inventory value
    SUM('Table'[Inventory])
)


Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
hnguy71
Memorable Member
Memorable Member

Hi @arun0386 ,

This is a unique challenge... but... possible...

I'll break it down into three separate measures:

First step is to fill down:

Step.01.DrillDown = 
VAR _LastWeek = CALCULATE(MAX('Table'[Week]), 'Table'[Inventory] <> BLANK(), ALL('Table'))
RETURN
IF( 
    // checks to see if I have an inventory week
    SELECTEDVALUE('Table'[Week]) > _LastWeek, 
    // if not, let's "fill down" and return the value for the last week
    CALCULATE(SUM('Table'[Inventory]), KEEPFILTERS('Table'[Week] = _LastWeek), ALL('Table')), 
    // if there is, return me normal inventory value
    SUM('Table'[Inventory])
)


Second step is to generate the cumulative total on forecast values: 

Step.02.Window = 
VAR _CurrWeek = SELECTEDVALUE('Table'[Week])

RETURN

SUMX(
    // peer into the previous row context
    WINDOW(-1, REL, 0, REL, VALUES('Table'[Week]),,,PARTITIONBY('Table'[Week])), 

    // generate my cumulative total
    CALCULATE(SUM('Table'[Measure]), 'Table'[Inventory] = BLANK(), ALL('Table'), KEEPFILTERS('Table'[Week] <= _CurrWeek))
) 


Last step is to add them all together:

Step.03.Final = [Step.01.DrillDown] + [Step.02.Window] // combine it all together

 

And the final results:

hnguy71_1-1730330416845.png

 

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.