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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
VoltesDev
Helper V
Helper V

How to use PREVIOUSQUARTER

Hi guys,

 

When I have this kind of data :

VoltesDev_0-1649741039699.png

I want to have a measure to current quarter (CQ) and Previous Quarter (PQ), what DAX I actually have to use ?

I have a Date table that relates to that column "Actual Closed Date" and also I have Page Filter context of this Date date table to be relative 1 year.

 

I I tried to count without any date filter in my DAX (Page filter still effected), like this :

Win Count = CALCULATE(
		DISTINCTCOUNT(Opportunities[opportunityid]),
		Opportunities,Opportunities[statecodename] = "Won"
		)

I have figures like the picture.

 

I created this measure for CQ :

Win Count CQ = CALCULATE(
                    DISTINCTCOUNT(Opportunities[opportunityid]),
                        Opportunities, Opportunities[statecodename] = "Won",
                        DATESQTD('Date Table'[Date])
                        )

 

And I'm using PreviousQuarter like this :

Win Count PQ = CALCULATE(
                    DISTINCTCOUNT(Opportunities[opportunityid]),
                        Opportunities, Opportunities[statecodename] = "Won",
                        PREVIOUSQUARTER('Date Table'[Date])
                        )

But it doesn't give me the correct result, as supposedly based on the picture, I expected to have value of 2 (01/14/22 and 03/14/22).

Can I ask for suggestion which DAX actually more appropriate for this purposes ?

 

Thanks

1 ACCEPTED SOLUTION

Hi @VoltesDev 
Please refer to the sample file with the solution https://www.dropbox.com/t/RftKbPYvOBYLJO4A
I have updated the Date table ti suit this type of calculation. 
As long as Win/Lost is kept on the slicer there is no need to hard code it. You can simply switch between Lost and Win from the slicer. If you still need it the same way before I guess no issue for you to do that.
The count measure is simple distinct count. If a quarter is selected it will simply calculate the count for the selected quarter. 
The Quarter to Year measure calculates the cumulative count staring from the start of the quarter up to the selected date. It will give the same result as the simple count except when you look at day or week level.
The previous quarter calculates the count of the selected date but shifted one quarter back in the time.
Same for the previous quarter to dater
1.png

Count = DISTINCTCOUNT ( Opportunities[opportunityid] )
Count PQ = 
VAR CurrentYearQuarterNumber = SELECTEDVALUE ( 'Date'[Year Quarter Number] )
VAR PreviousYearQuarterNumber = CurrentYearQuarterNumber - 1
VAR Result =
    CALCULATE (
        DISTINCTCOUNT ( Opportunities[opportunityid] ),
        REMOVEFILTERS ( 'Date' ),
        'Date'[Year Quarter Number] = PreviousYearQuarterNumber,
        VALUES ( 'Date'[Month In Quarter Number] )
    )
RETURN
    Result
Count QTD = 
VAR LastMonthAvailable =
    MAX ( 'Date'[Year Month Number] )
VAR LastYearQuarterAvailable =
    MAX ( 'Date'[Year Quarter Number] )
VAR Result =
    CALCULATE ( 
        DISTINCTCOUNT ( Opportunities[opportunityid] ), 
        REMOVEFILTERS ( 'Date' ),
        'Date'[Year Month Number] <= LastMonthAvailable,
        'Date'[Year Quarter Number] = LastYearQuarterAvailable
    ) 
RETURN 
    Result
Count PQTD = 
VAR CurrentYearQuarterNumber = SELECTEDVALUE ( 'Date'[Year Quarter Number] )
VAR PreviousYearQuarterNumber = CurrentYearQuarterNumber - 1
VAR Result =
    CALCULATE (
        [Count QTD],
        REMOVEFILTERS ( 'Date' ),
        'Date'[Year Quarter Number] = PreviousYearQuarterNumber,
        VALUES ( 'Date'[Month In Quarter Number] )
    )
RETURN
    Result

View solution in original post

9 REPLIES 9
tamerj1
Super User
Super User

Hi @VoltesDev 

In your visual you have to use the date column from date table not the actual close date from the data table

Hi @tamerj1 ,

 

I changed it to use from Date table instead, but it doesn't look good either ->

VoltesDev_0-1649743200636.png

Those are hierarchy from auto-date-hierarchy, but it refer to the correct date, but somehow the value is wrong.

 

In fact, If I included the Win Count measure, it will give me all the Date tables rows.

 

Thanks,

@VoltesDev 
Can you please share a sample file?

Hi, I want to, but how to share file (pbix) here ?

Thanks

@VoltesDev 

You may upload to WeTransfer for example and share the link

Hi,

 

I just setup Box, hopefully can do the same -> https://app.box.com/s/jvrp20g3hytyabusgewvxynbfkgysidd

So basically what I expected is (with the blue background chart), the guy CQ (current quarter) is 0 and PQ (previous quarter) is 2

VoltesDev_0-1649758069286.png

 

Thanks

Hi @VoltesDev 
Uou don't have data for the previuos quarter! Do you mean same quarter perviuos year?

No, I mean Previous Quarter for this quarter. Actually now I have doubt, does QTD means like today is 12 April, so QTD is 1 Apr - 12 Apr ? So in that sense, what I want for Previous Quarter is the sum of 1 Jan - 30 Mar.

Hi @VoltesDev 
Please refer to the sample file with the solution https://www.dropbox.com/t/RftKbPYvOBYLJO4A
I have updated the Date table ti suit this type of calculation. 
As long as Win/Lost is kept on the slicer there is no need to hard code it. You can simply switch between Lost and Win from the slicer. If you still need it the same way before I guess no issue for you to do that.
The count measure is simple distinct count. If a quarter is selected it will simply calculate the count for the selected quarter. 
The Quarter to Year measure calculates the cumulative count staring from the start of the quarter up to the selected date. It will give the same result as the simple count except when you look at day or week level.
The previous quarter calculates the count of the selected date but shifted one quarter back in the time.
Same for the previous quarter to dater
1.png

Count = DISTINCTCOUNT ( Opportunities[opportunityid] )
Count PQ = 
VAR CurrentYearQuarterNumber = SELECTEDVALUE ( 'Date'[Year Quarter Number] )
VAR PreviousYearQuarterNumber = CurrentYearQuarterNumber - 1
VAR Result =
    CALCULATE (
        DISTINCTCOUNT ( Opportunities[opportunityid] ),
        REMOVEFILTERS ( 'Date' ),
        'Date'[Year Quarter Number] = PreviousYearQuarterNumber,
        VALUES ( 'Date'[Month In Quarter Number] )
    )
RETURN
    Result
Count QTD = 
VAR LastMonthAvailable =
    MAX ( 'Date'[Year Month Number] )
VAR LastYearQuarterAvailable =
    MAX ( 'Date'[Year Quarter Number] )
VAR Result =
    CALCULATE ( 
        DISTINCTCOUNT ( Opportunities[opportunityid] ), 
        REMOVEFILTERS ( 'Date' ),
        'Date'[Year Month Number] <= LastMonthAvailable,
        'Date'[Year Quarter Number] = LastYearQuarterAvailable
    ) 
RETURN 
    Result
Count PQTD = 
VAR CurrentYearQuarterNumber = SELECTEDVALUE ( 'Date'[Year Quarter Number] )
VAR PreviousYearQuarterNumber = CurrentYearQuarterNumber - 1
VAR Result =
    CALCULATE (
        [Count QTD],
        REMOVEFILTERS ( 'Date' ),
        'Date'[Year Quarter Number] = PreviousYearQuarterNumber,
        VALUES ( 'Date'[Month In Quarter Number] )
    )
RETURN
    Result

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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