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
Anonymous
Not applicable

Calculate montlhy cost average broken down by month, item ID and expense type

Hello,

 

This is my firt post ; Thank you for having me I've been lurking around here for some time and today I don't know how to solve this problem. I'm still very new at PBI and still reading training books on PBI and DAX. 

 

I am currently working on a project where I'm trying to compute monthly average for:

- each item (item ID#)

- each particular month (I created a Month-year column - some item only stay online for a few months some keep going until the end of the year)

- summing up all expense codes in the monthly average cost but have the ability to filter monthly average by expense code to compare later in the dashboard.

 

So ideally I would be able to:

- calculate monthly average for each ItemID

- filter by expense code (to remove or add some of them as needed)

 

Here is my sample table

 

IDDateMonth-yearExpense CodeTotal
12021-06-156-2021Code1600
12021-06-156-2021Code140
12021-06-156-2021Code225
12021-06-156-2021Code22
12021-06-156-2021Code21
12021-06-156-2021Code3250
22021-05-235-2021Code310
22021-05-155-2021Code1550
22021-05-155-2021Code145
22021-05-155-2021Code240
22021-05-155-2021Code24
22021-05-155-2021Code22
22021-05-155-2021Code3175
22021-05-145-2021Code370
22021-05-025-2021Code350

 

and my formula

 

Monthly cost  = 
SUMX(
    FILTER (
        table,
        table[ID] = EARLIER ( table[ID] )
            && DATEDIFF ( 'table'[Month-year], EARLIER ( 'table'[Month-year] ), MONTH ) = 0
    ),
    table[Total]
)

 

 

It's not too bad but some of the costs are wrong while some are right so I suspect that the costs breakdown are not working too well.

 

Thank you all for your help!

1 ACCEPTED SOLUTION

Hi  @Anonymous ,

 

Create a measure as below;

Measure =
VAR _count =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[ID] = MAX ( 'Table'[ID] )
                && 'Table'[Month-year] = MAX ( 'Table'[Month-year] )
        )
    )
VAR _sum =
    CALCULATE (
        SUM ( 'Table'[Total] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[ID] = MAX ( 'Table'[ID] )
                && 'Table'[Month-year] = MAX ( 'Table'[Month-year] )
        )
    )
RETURN
    DIVIDE ( _sum, _count, BLANK () )

And you will see:

vkellymsft_1-1629097765581.pngvkellymsft_2-1629097776542.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

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

View solution in original post

3 REPLIES 3
amitchandak
Super User
Super User

@Anonymous , Sumx or averagex as per need

 

A new column

 

Monthly cost =
AverageX(
FILTER (
table,
table[ID] = EARLIER ( table[ID] )
&& 'table'[Month-year] = EARLIER ( 'table'[Month-year] )
),
table[Total]
)

Anonymous
Not applicable

Hello,

 

So this formula is indeed calculating correct monthly cost as a whole but it is not able to "dynamically" recalculate the monthly cost when filtering by expense code from the report view. It does perform some calculation but the result is not correct - for instance it's telling me I pay more taxes as monthly cost than rent cost total which is not true.

 

Is it coming from the formula or maybe the way I setup the filter on the report page?

 

Again thank you for the help - at least it solved 50% of the problem.

Hi  @Anonymous ,

 

Create a measure as below;

Measure =
VAR _count =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[ID] = MAX ( 'Table'[ID] )
                && 'Table'[Month-year] = MAX ( 'Table'[Month-year] )
        )
    )
VAR _sum =
    CALCULATE (
        SUM ( 'Table'[Total] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[ID] = MAX ( 'Table'[ID] )
                && 'Table'[Month-year] = MAX ( 'Table'[Month-year] )
        )
    )
RETURN
    DIVIDE ( _sum, _count, BLANK () )

And you will see:

vkellymsft_1-1629097765581.pngvkellymsft_2-1629097776542.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

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

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.