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
GJUDGE
Helper II
Helper II

Comparing Periods in the same table

I am sure this is a fairly straightforward problem for most but I'm fairly new to Power BI and any help is appreciated!

 

I have a single table that compiles monthly reporting information. I want to be able to calculate the difference between current month Output and Previous Month Output. What I can't figure out is how to do this dynamically so that each time a new month is added, the measure will compare the max reporting period with the max minus one?

 

Thanks in advance!

 

ProjectReporting PeriodOutput
A01/01/2021100
B01/01/202180
C01/01/2021150
A01/02/2021120
B01/02/202190
C01/02/2021140
A01/03/2021110
B01/03/202195
C01/03/2021140
 etc.  

 

 

 

 

1 ACCEPTED SOLUTION

Hi @GJUDGE 

You can use Edate() function to get last period, for example, 

Last Period = 
    var _currentDate=MAX('table'[date])
    var _lastDate=EDATE(_currentDate,-1)
return 
    CALCULATE(SUM('table'[Output]),'table'[date]=_lastDate)

vxiaotang_0-1634626372555.png

Kindly note:  Edate() function has some limitations,

vxiaotang_1-1634626438807.png

If you can't use Edate() on some occasions, you can try this measure,

Last Period = 
    var _maxDate=MAX('table'[date])
    var _lastDate=DATE(YEAR(_maxDate),MONTH(_maxDate)-1,1)
return 
    CALCULATE(SUM('table'[Output]),'table'[date]=_lastDate)

vxiaotang_2-1634626624400.png

 

 

Best Regards,

Community Support Team _Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

18 REPLIES 18
jppv20
Solution Sage
Solution Sage

hI @GJUDGE ,

 

You can create this measure:

 
OutputDifference =
var Output = SUM('Table'[Output])
var OutputLastPeriod = CALCULATE(SUM('Table'[Output]),ALLEXCEPT('Table','Table'[Project]),PREVIOUSMONTH('Table'[Reporting Period]))

Return

Output-OutputLastPeriod
 
This will give the following result:
jppv20_0-1633682178366.png

 

Jori

 

If I answered your question, please mark it as a solution to help other members find it more quickly.

Connect on Linkedin

 

@jppv20 This isn't quite giving me what I need (but I appreciate your help!). When I put the measure into matrix view I get the following, which appears to be summing all the output months. How can I adapt the measure to only show the change for the latest month? i.e. -10 for project A, 5 for project B and 0 for project C with a total of -5?

 

GJUDGE_0-1633683619524.png

 

@GJUDGE Thanks for clarifying, I understand the issue better now. Do you need this to be solved in the measure? Otherwise you can add a date filter to select the month for which you want to see the change:

jppv20_0-1633684197085.png

Jori

 

If I answered your question, please mark it as a solution to help other members find it more quickly.

Connect on Linkedin

@jppv20 Thanks! If this can be built into the measure that would be best if you could advise?

@GJUDGE 

In that case this measure should work:

OutputDifference =
var Output = CALCULATE(SUM('Table'[Output]),FILTER('Table','Table'[Reporting Period]=MAXX('Table','Table'[Reporting Period])))
var OutputLastPeriod = CALCULATE(SUM('Table'[Output]),FILTER('Table',MONTH('Table'[Reporting Period])=MONTH(MAXX('Table','Table'[Reporting Period]))-1))

Return

Output-OutputLastPeriod
jppv20_0-1633685201339.png

 

Jori

 

If I answered your question, please mark it as a solution to help other members find it more quickly.

Connect on Linkedin

 

@jppv20  The measure is giving me the total of the output column as opposed to the output difference column. I have copied my measure below with actual table names etc., if you can see any errors within this?

 

Monthly MW Change =

var Output = CALCULATE(SUM('Master Data'[Projected Capacity (MW)]),FILTER('Master Data','Master Data'[Reporting Period]=MAXX('Master Data','Master Data'[Reporting Period])))
var OutputLastPeriod = CALCULATE(SUM('Master Data'[Projected Capacity (MW)]),FILTER('Master Data',MONTH('Master Data'[Reporting Period])=MONTH(MAXX('Master Data','Master Data'[Reporting Period]))-1))

Return

Output-OutputLastPeriod

@jppv20 Sorry, it actually works! I still had a filter on!! Thank you for your help! Could you help me understand how would I modify this measure to then compare the start of the year (with a fixed date rather than reporting period included?)

to current month?

@GJUDGE Great that it works!

 

For comparisson with the start of the year you can use this measure:

 

Start of year Change =

var Output = CALCULATE(SUM('Master Data'[Projected Capacity (MW)]),FILTER('Master Data','Master Data'[Reporting Period]=MAXX('Master Data','Master Data'[Reporting Period])))
var OutputLastPeriod = CALCULATE(SUM('Master Data'[Projected Capacity (MW)]),FILTER('Master Data',YEAR('Master Data'[Reporting Period])=YEAR(TODAY())&&MONTH('Master Data'[Reporting Period])=MONTH(MINX('Master Data','Master Data'[Reporting Period]))))

Return

Output-OutputLastPeriod
 
Jori
 
If I answered your question, please mark it as a solution to help other members find it more quickly.

Connect on Linkedin

 

@jppv20 Hi Jori. This seems to be bringing back the total for the current month only and not deducting the values from the start of the year? Is there anything else I need to modify to the OutputLastPeriod element? 

 

Thanks,
Gary 

@GJUDGE Hi Gary. Are you sure you are not filtering anything? It gives me this result:

jppv20_0-1633691655854.png

 

Jori

 

If I answered your question, please mark it as a solution to help other members find it more quickly.

Connect on Linkedin

@JP Sadly no filters are on! Could it be an issue that my "start of the year" value is actually in Dec 2020 (so in reporting period column it is 01/12/2020)?

@GJUDGE I think that would be the problem indeed. In that case try:

 

Start of year Change =

var Output = CALCULATE(SUM('Master Data'[Projected Capacity (MW)]),FILTER('Master Data','Master Data'[Reporting Period]=MAXX('Master Data','Master Data'[Reporting Period])))
var OutputLastPeriod = CALCULATE(SUM('Master Data'[Projected Capacity (MW)]),FILTER('Master Data',YEAR('Master Data'[Reporting Period])=YEAR(TODAY())-1&&MONTH('Master Data'[Reporting Period])=MONTH(MAXX('Master Data','Master Data'[Reporting Period]))))

Return

Output-OutputLastPeriod
 
Jori
 
If I answered your question, please mark it as a solution to help other members find it more quickly.

Connect on Linkedin

@jppv20 Hi Jori, still providing me the same result - only the total output from the current month with no deduction for the start of the year 😞. I've managed to correct by applying the following, but not sure if this will work going forward into other years!

 

Yearly MW Change = var Output = CALCULATE(SUM('Master Data'[Projected Capacity (MW)]),FILTER('Master Data','Master Data'[Reporting Period]=MAXX('Master Data','Master Data'[Reporting Period])))
var OutputLastPeriod = CALCULATE(SUM('Master Data'[Projected Capacity (MW)]),FILTER('Master Data',('Master Data'[Reporting Period])=MINX('Master Data','Master Data'[Reporting Period])))

Return

Output-OutputLastPeriod

@GJUDGE 

I think in this case it will always compare to the December 2020 value, also in a couple of years.

Could you share some more of the table so I can replicate the problem?

 

Jori

@jppv20 I'm happy to stick with the current solution for the time being. Will need to remember that at the start of a new year though! Can I ask you one other question - how would I write a simple measure to sum the output for the previous month (i.e. max period minus 1)?

Hi @GJUDGE 

You can use Edate() function to get last period, for example, 

Last Period = 
    var _currentDate=MAX('table'[date])
    var _lastDate=EDATE(_currentDate,-1)
return 
    CALCULATE(SUM('table'[Output]),'table'[date]=_lastDate)

vxiaotang_0-1634626372555.png

Kindly note:  Edate() function has some limitations,

vxiaotang_1-1634626438807.png

If you can't use Edate() on some occasions, you can try this measure,

Last Period = 
    var _maxDate=MAX('table'[date])
    var _lastDate=DATE(YEAR(_maxDate),MONTH(_maxDate)-1,1)
return 
    CALCULATE(SUM('table'[Output]),'table'[date]=_lastDate)

vxiaotang_2-1634626624400.png

 

 

Best Regards,

Community Support Team _Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

vanessafvg
Super User
Super User

so you saying you want to sum this months and sum last month and then work out the difference as a change %?





If I took the time to answer your question and I came up with a solution, please mark my post as a solution and /or give kudos freely for the effort 🙂 Thank you!

Proud to be a Super User!




Not even a % change, just the difference, but my main worry is not needing to update the measure each month every time a new months worth of data is added. Any tips are welcomed!

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.