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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Reddy5833
Helper II
Helper II

How to calculate difference between current quarter and previous quarter

Hi, can somebody help me with this.

Available data

Year-QTR           Quarter rank    usage
2021-111021.4
2021-22964
2021-331023.7
2021-441042.6
2022-151043
2022-261049
2022-371053
2022-481052
2023-191020

 

Expected result

Reddy5833_0-1676045153277.png

 

The differnce for the first (Base) value must be blank.

 

Any help would be much appriciated. Thanks in advance.

 

3 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

@Reddy5833 Try:

Measure =
  VAR __Current = SUM('Table'[usage])
  VAR __CurrentQR = MAX('Table'[Quarter rank])
  VAR __Previous = SUMX(FILTER(ALL('Table'),[Quarter rank] = __CurrentQR - 1),[usage])
  VAR __Result = IF(__Previous = BLANK(),BLANK(),__Current - __Previous)
RETURN
  __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

@Greg_Deckler Thanks a lot, it works perfect..

 

View solution in original post

 
I got the answer to my question, I found it from your video ( MSHGQM - Power BI DAX - Don't Use CALCULATE!
 
Measure to be used when there are multiple categories.

Measure 1 =
VAR __Site = VALUES('Sample'[Site Name])
 VAR __Current = sum('Sample'[Usage])
VAR __Table = FILTER(ALLSELECTED('Sample'),'Sample'[Site Name] in  __Site) 
  VAR __CurrentQR = MAX('Sample'[YQRank])
  VAR __Previous = SUMX(FILTER(__Table,[YQRank] = __CurrentQR - 1), [Usage])
  VAR __Result = IF(__Current=BLANK(),"0",__Current - __Previous)
RETURN
__Result

View solution in original post

7 REPLIES 7
Reddy5833
Helper II
Helper II

Hi @Greg_Deckler, I have usage data for 5 years, (around 1500 records), the table contains date field, formateed as date data type only, I want to calculate differnce of usage between current qtr and prvs qtr (same as i asked in previos question, but based on proper date field)

 

I'm using below measeu to calculate prev quarter usage,

but it is not giving the result as i expected unless I connect with date table (though I'm not using any field from date table in the measure )

 

 

Prev QTR Usage = CALCULATE(SUM('Sample'[Usage]), DATEADD('Sample'[Date],-1,QUARTER))
This measure is giving blanks, if i dont connect with date table.

 

measure to calculate diff: -- works fine when i connect with date table

Diff =
  CALCULATE(SUM('Sample'[Usage])-CALCULATE(SUM('Sample'[Usage]), DATEADD('Sample'[Date],-1,QUARTER))
  )
 
Measure to get first date as blank -- it is not working even if i connect with date table, giving same result as above
 
Diff 1 =
  IF(SUM('Sample'[YQ Rank])=1, BLANK(),  
  CALCULATE(SUM('Sample'[Water Usage])-CALCULATE(SUM('Sample'[Water Usage]), DATEADD('Sample'[Date],-1,QUARTER)))
  )
 

sample data

Reddy5833_0-1676091489043.png

 

result when connected with date dimension

Reddy5833_1-1676092805245.png

 

Result when not connected with date

Reddy5833_2-1676092858008.png

 

@Reddy5833 This is because you are using CALCULATE. CALCULATE almost requires a star schema to work properly. CALCULATE has a really hard time dealing with single table data models so you get wacky results. See this video:



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler , I saw the video, it's brilliant.. Thanks a lot

Greg_Deckler
Super User
Super User

@Reddy5833 Try:

Measure =
  VAR __Current = SUM('Table'[usage])
  VAR __CurrentQR = MAX('Table'[Quarter rank])
  VAR __Previous = SUMX(FILTER(ALL('Table'),[Quarter rank] = __CurrentQR - 1),[usage])
  VAR __Result = IF(__Previous = BLANK(),BLANK(),__Current - __Previous)
RETURN
  __Result


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg

Hope you are doing good

 

The Measure you share is working fine when I have only one category, but when I have multipel categories in my dataset it is unable to find the difference between current quarter and previous quarter when I choose a category from the slicer.

But it works fine when we select all categories from slicer it is showing the over all differnce between current quarter and prev quarter.


I'm unable to use calculate function in the measure it is giving wrong values if I do so

 

Below is the measre you shared a while ago.

Measure =
  VAR __Current = SUM('Table'[usage])
  VAR __CurrentQR = MAX('Table'[Quarter rank])
  VAR __Previous = SUMX(FILTER(ALL('Table'),[Quarter rank] = __CurrentQR - 1),[usage])
  VAR __Result = IF(__Previous = BLANK(),BLANK(),__Current - __Previous)
RETURN
  __Result

 

@Greg_Deckler Thanks a lot, it works perfect..

 

 
I got the answer to my question, I found it from your video ( MSHGQM - Power BI DAX - Don't Use CALCULATE!
 
Measure to be used when there are multiple categories.

Measure 1 =
VAR __Site = VALUES('Sample'[Site Name])
 VAR __Current = sum('Sample'[Usage])
VAR __Table = FILTER(ALLSELECTED('Sample'),'Sample'[Site Name] in  __Site) 
  VAR __CurrentQR = MAX('Sample'[YQRank])
  VAR __Previous = SUMX(FILTER(__Table,[YQRank] = __CurrentQR - 1), [Usage])
  VAR __Result = IF(__Current=BLANK(),"0",__Current - __Previous)
RETURN
__Result

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.