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

Cost comparison

Hi Experts,

 

Can any one please help me to arrive the soultion?

 

I have a month wise cost details for each application individually, now i want to compare the current month cost with previous month cost and take top 10 application groups and plot the graph. Every month, the cost will get change depens on the usage. 

cost.png

Thanks,

saranya

 

 

 

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous , if this the base table you need to unpivot. and convert month into number or date.

https://radacad.com/pivot-and-unpivot-with-power-bi

With a date or month date

 

//Date and date table


MTD Sales = CALCULATE(Average(Sales[Price]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(Average(Sales[Price]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))

 

//Month no or rank

 

Month Rank = RANKX(all('Date'),'Date'[Month Start date],,ASC,Dense)
This Month = CALCULATE(Average('Sales'[Price]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])))
Last Month = CALCULATE(Average('Sales'[Price]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])-1))

 

View solution in original post

2 REPLIES 2
v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

According to my understand, you want to calculate the difference between Current Month and Last Month,right?

I did it in two ways, you could take a look at  my pbix file here

 

Method1:

 

YearMonth =
YEAR ( [Date] ) * 100
    + MONTH ( [Date] )
differenceMethod1 =
VAR _app =
    SELECTEDVALUE ( 'Table'[Application] )
VAR _yearmonth =
    SELECTEDVALUE ( 'Table'[YearMonth] )
VAR _lastmonth =
    CALCULATE (
        SUM ( 'Table'[Cost] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Application] = _app
                && 'Table'[YearMonth]
                    = CALCULATE (
                        MAX ( 'Table'[YearMonth] ),
                        FILTER ( ALL ( 'Table' ), 'Table'[YearMonth] < _yearmonth )
                    )
        )
    )
RETURN
    IF (
        _lastmonth = BLANK (),
        MAX ( 'Table'[Cost] ),
        CALCULATE ( SUM ( 'Table'[Cost] ) - _lastmonth )
)

My visualization looks like this:

9.17.2.1.png

 

Method2:

YearMonth =
YEAR ( 'Table1'[Date] ) * 100
+ MONTH ( 'Table1'[Date] )
DateSlicer =
ALLSELECTED ( Table1[YearMonth] )
differenceMethod2 =
VAR _sele =
    SELECTEDVALUE ( DateSlicer[YearMonth] )
VAR _diff =
    _sele - MAX ( 'Table1'[YearMonth] )
VAR _sum =
    IF ( _diff = 0 || _diff = 1 || _diff = 89, SUM ( 'Table1'[Cost] ), BLANK () )
VAR currMinusPre =
    CALCULATE (
        SUM ( 'Table1'[Cost] ),
        FILTER ( Table1, 'Table1'[YearMonth] = _sele )
    )
        - CALCULATE (
            SUM ( 'Table1'[Cost] ),
            FILTER (
                Table1,
                'Table1'[YearMonth] = _sele - 1
                    || 'Table1'[YearMonth] = _sele - 89
            )
        )
RETURN
    IF ( HASONEVALUE ( 'Table1'[YearMonth] ), _sum, currMinusPre )

9.17.2.2.gif

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.

If not, please upload some insensitive data samples and expected output.

 

Best Regards,

Eyelyn Qin

 

 

amitchandak
Super User
Super User

@Anonymous , if this the base table you need to unpivot. and convert month into number or date.

https://radacad.com/pivot-and-unpivot-with-power-bi

With a date or month date

 

//Date and date table


MTD Sales = CALCULATE(Average(Sales[Price]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(Average(Sales[Price]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))

 

//Month no or rank

 

Month Rank = RANKX(all('Date'),'Date'[Month Start date],,ASC,Dense)
This Month = CALCULATE(Average('Sales'[Price]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])))
Last Month = CALCULATE(Average('Sales'[Price]), FILTER(ALL('Date'),'Date'[Month Rank]=max('Date'[Month Rank])-1))

 

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.