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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Leandro
Advocate I
Advocate I

Inventory Average Cost

INVENTORY.PNG

 

 Hello guys,

I am having difficulty in a calculation in DAX, the calculation is of weighted average cost of stock, here in Brazil we have many variations of cost, then this method is the most used.

Basically my problem is to calculate the average cost, because the end of a month is the base for the next one, then I find a kind of circular reference in this calculation.

In the image of the to see the formula that changes from line 1 to line 2.

This calculation is really giving me headaches, and I appreciate any help you can give me to solve this problem.

Best Regards.

 

8 REPLIES 8
andrewhaicalis
Frequent Visitor

Hi @Leandro 

I have exactly the same issue that I am trying to solve. Did you ever get the average price to work with DAX? As I think you mentioned, it is simple with Excel but my data set as circa a million lines so running the system with excel formulas is not that effecient.

Kind regards,
Andrew

AnthonyTilley
Solution Sage
Solution Sage

i Have created a Pbix file that will do waht your asking for.

it does it across a few colunms that are needed as you will need to do several look ups to refrence back to the previous month.

i have shared this file below

i apologise i did not have time to format the dax correctly and add comments but you should be ablet o follow the logic

PBIX FILE





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thank you very much for you help and time, AnthonyTilley.

 

I have to analize very carefuly your pbix file. I coulnd't understand the logic at first time I read it.

And I can´t understand yet if it will do with a lot of products.

 

Thanks a lot for your time. I´ll give you feeedback as soon as possible.

v-ljerr-msft
Microsoft Employee
Microsoft Employee

Hi @Leandro,

 

Could you try the formula below to see if it works in your scenario? Smiley Happy

 

Average Cost = 
VAR currentPeriod = Table1[Period]
VAR minPeriod =
    CALCULATE ( MIN ( Table1[Period] ), ALL ( Table1 ) )
RETURN
    IF (
        currentPeriod = minPeriod,
        DIVIDE (
            Table1[Incial Total Value] + Table1[Input Total Value],
            Table1[Incial Quant] + Table1[Input Quant]
        ),
        DIVIDE (
            CALCULATE (
                SUMX ( Table1, Table1[Incial Total Value] + Table1[Input Total Value] ),
                FILTER ( ALL ( Table1 ), Table1[Period] < currentPeriod )
            )
                + Table1[Input Total Value],
            CALCULATE (
                SUMX ( Table1, Table1[Incial Quant] + Table1[Input Quant] + Table1[Out Quant] ),
                FILTER ( ALL ( Table1 ), Table1[Period] < currentPeriod )
            )
                + Table1[Input Quant]
        )
    )

 

Regards

Hi @v-ljerr-msft 

Thanks a lot for your help, i tried the formula that you sent, but there is something that still not working, but i can see the metod is the right, setting "VAR" for the calculation to avoid circular relationships.

Maybe my description of the problem was not precise enough, so i posted the file on google drive, this way it can light the problem.

And again, thanks so much for the help, it will really make my job a lot easier.


EXCEL FILE

Hi @Leandro,

 

After a few try, I was still not able to figure it out. Smiley Mad So I did some research, then it turns out that it may be not possible to do it with DAX in this scenario, because of the circular dependency. Here is the similar thread for your reference. Smiley Happy

 

Regards

@v-ljerr-msft Thanks a lot for your atention on this issue, seems like you're right, if in the future if a find some way to solve this, i'll let you know!

Thanks for your help!
Regards!

Hello Leandro!!

 

In meanwhile do you have you issue solved?

 

I have exactly the same need.

 

Greetings from Portugal.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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