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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Broot
New Member

Dynamic average specific cost calculation

Hi

I am trying to create a report for production costs across multiple production units, but I am having issues with calculating average specific costs.

 

I have cost and production data as two separate tables. These are gatherered from different sources and summed up a bit differently. Production is daily totals and costs are monthly totals (but in the tables I treat it like all costs occur on the first of every month, so the date format between the two tables are the same. The structure of the tables are as follows:

(the Items-column is referring to different types of items used/consumed in production)

 

Cost Table

Unit  Product  Item  Cost  ts                    UnitProduct
ASWingA12020-01-01A SW
AHWingB32020-01-01A HW
BSWingD22020-01-02B SW
AHWingA52020-01-02A HW
CHWingB32020-01-02C HW
CHWingC22020-01-02C HW
..................

 

Production Table

Unit      Product    Production   ts                   UnitProduct
ASW32020-01-01A SW
BSW32020-01-02B SW
BHW22020-01-03B HW
CSW12020-01-04C SW
AHW42020-01-05A HW
CHW32020-01-06

C HW

............

...

 

Now what I am trying to achieve is to be able to select a period of time in the report and have it calculate the average item cost per product for each type of product at each unit, so if I for example selected the period 2020-01-01 to 2020-12-31 I would expect something like the following:

 

Unit   Product   Item     Cost/Product
AHWingB0,42
ASWingA0,31
BSWingD0,22
CHWingB0,34
CHWingC0,37
............

(All values are just made up examples so I do not expect specifically the results shown, it is just to demonstrate the desired format of the results)

 

I have (sort of) been able to do this by creating an active relationship between the two tables (many to many) between the UnitProduct columns and then create a measure with the following formula:

Specific_Cost = DIVIDE(SUM('Cost_Data'[value]), SUM('Production'[value]))

 

This works, EXCEPT I have to use TWO date-slicers, one to select the time period for the cost data and another to select the time period for the production data (which of course needs to be the same). I have tried creating a passive connection between the tables date columns, create a separat date table that connects to both tables, switching orders of relationships, adjusting the formula etc but I have not been able to get it working as I want: to be able to select a period from one slicer and then get the average item cost per product at each unit.

 

Im guessing this is a fairly easy problem to solve (most likely in more than one way) but my mind is just completely blocked at the moment. Hopefully someone can explain what I am missing.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Broot ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want.

1. Create a date dimension table(don't create any relationship with other tables)

2. Apply the date field in the above date dimension table on the slicer

3. Create a measurea as below to get the special cost

specific_Cost = 
VAR _mindate =
    MIN ( 'Date'[Date] )
VAR _maxdate =
    MAX ( 'Date'[Date] )
VAR _cost =
    CALCULATE (
        SUM ( 'Cost_Data'[Cost] ),
        FILTER (
            'Cost_Data',
            'Cost_Data'[ts] >= _mindate
                && 'Cost_Data'[ts] <= _maxdate
        )
    )
VAR _product =
    CALCULATE (
        SUM ( 'Production'[Production] ),
        FILTER (
            'Production',
            'Production'[ts] >= _mindate
                && 'Production'[ts] <= _maxdate
        )
    )
RETURN
    DIVIDE ( _cost, _product )

yingyinr_0-1633327739602.png

Since I don't know what your actual calculation logic is for the Cost/Product field in the Expected Results table, it's possible that the final value returned is not the result you want. Later on, you can update the formula of above measure to get the results you want. Or if you can share more information about the calculation logic, we can provide you with a solution...


Expected Results

Unit    Product    Item      Cost/Product??
A HW ingB 0,42
A SW ingA 0,31
B SW ingD 0,22
C HW ingB 0,34
C HW ingC 0,37

Best Regards

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @Broot ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want.

1. Create a date dimension table(don't create any relationship with other tables)

2. Apply the date field in the above date dimension table on the slicer

3. Create a measurea as below to get the special cost

specific_Cost = 
VAR _mindate =
    MIN ( 'Date'[Date] )
VAR _maxdate =
    MAX ( 'Date'[Date] )
VAR _cost =
    CALCULATE (
        SUM ( 'Cost_Data'[Cost] ),
        FILTER (
            'Cost_Data',
            'Cost_Data'[ts] >= _mindate
                && 'Cost_Data'[ts] <= _maxdate
        )
    )
VAR _product =
    CALCULATE (
        SUM ( 'Production'[Production] ),
        FILTER (
            'Production',
            'Production'[ts] >= _mindate
                && 'Production'[ts] <= _maxdate
        )
    )
RETURN
    DIVIDE ( _cost, _product )

yingyinr_0-1633327739602.png

Since I don't know what your actual calculation logic is for the Cost/Product field in the Expected Results table, it's possible that the final value returned is not the result you want. Later on, you can update the formula of above measure to get the results you want. Or if you can share more information about the calculation logic, we can provide you with a solution...


Expected Results

Unit    Product    Item      Cost/Product??
A HW ingB 0,42
A SW ingA 0,31
B SW ingD 0,22
C HW ingB 0,34
C HW ingC 0,37

Best Regards

Hello @Anonymous 

Thank you for the solution, it did exactly what I was trying to do 👍

I was actually able to solve this issue myself by creating a separate Date table and connecting that to both the Cost and Production table with an active relationship (many to one, single direction) and also creating a separate UnitProduct table and connection that to both the Cost and Production tables with an active relationship (many to one, single direction) as well. I remade the report from scratch and the second time around it worked so I probably got something mixed up the first time.

But I do prefer your solution over my own as I don't have to set an active relationship between the Date table and the other tables, which means I have more freedom to set up other connections between the tables for other functions in the report.

Your solution also inspired me to create some other useful measures I can use in the report, without having to think about current relationships.

Thank you!

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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