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

Rolling 5 quarter restriction based on Year-Quarter Selection Filter

Hi all 

 

I want to show rolling 5 quarter in my chart 

 

Ex: I have selected 2019 Q3 in the Quarter selection filter so i want to show following quarter in Cumulative sales Trend 

  1. Current Quarter =  2019 Q3
  2. Previous Quarter = 2019 Q2
  3. This Quarter in Previous Year = 2018 Q3
  4. Other Quarte = 2019 Q1, 2018 Q4

Screenshot_5.png

Data:

Account IdOpportunity IdAmountClose DateUser Name
A1Opp1$150,0002/1/2018Sean Barton
A1Opp2$40,0005/28/2018Sean Barton
A1Opp3$10,0008/21/2018Rachel Stark
A1Opp4$20,00010/18/2018Sean Barton
A1Opp5$15,00011/7/2018Sean Barton
A1Opp6$75,0003/26/2019Sean Barton
A1Opp7$85,0005/30/2019Sean Barton
A1Opp8$150,0006/15/2019Rachel Stark
A1Opp9$15,0009/5/2019Rachel Stark
A1Opp10$50,00010/16/2019Sean Barton
A2Opp11$120,0003/1/2018Sean Barton
A2Opp12$5,0006/3/2018Rachel Stark
A2Opp13$10,0009/20/2018Sean Barton
A2Opp14$5,00011/24/2018Rachel Stark
A2Opp15$50,00012/31/2018Sean Barton
A2Opp16$120,0002/14/2019Sean Barton
A2Opp17$75,0005/18/2019Sean Barton
A2Opp18$75,0005/29/2019Rachel Stark
A2Opp19$85,0007/31/2019Sean Barton
A2Opp20$85,00010/31/2019Sean Barton
A2Opp21$5,00011/28/2019Sean Barton
A3Opp22$50,0002/22/2018Rachel Stark
A3Opp23$100,0004/28/2018Sean Barton
A3Opp24$40,0008/1/2018Rachel Stark
A3Opp25$35,0008/31/2018Sean Barton
A3Opp26$5,00011/14/2018Sean Barton
A3Opp27$10,00010/29/2018Rachel Stark
A3Opp28$85,0002/5/2019Rachel Stark
A3Opp29$40,0006/3/2019Sean Barton
A3Opp30$75,0007/26/2019Rachel Stark
A3Opp31$5,00012/21/2019Sean Barton

 

Following Things I tried:

 

 

Year-Q = Opportunity[Close Date].[Year] & "-Q" & Opportunity[Close Date].[QuarterNo]
Quarter Start Date = 
VAR TodaysDate = Opportunity[Close Date]
VAR Calendar1 = CALENDAR(DATE(YEAR(TodaysDate),1,1),DATE(YEAR(TodaysDate),12,31))
VAR Calendar2 = ADDCOLUMNS(Calendar1,"Quarter",ROUNDUP(MONTH([Date])/3,0))
VAR Calendar3 = ADDCOLUMNS(Calendar2,"WeekDay1",WEEKDAY([Date],2))
VAR TodaysMonth = ROUNDUP(MONTH(TodaysDate)/3,0)
VAR Calendar4 = FILTER(Calendar3,[Quarter]=TodaysMonth&&[WeekDay1]<6)
VAR FirstWorkingDay = MINX(Calendar4,[Date])
RETURN FORMAT(FirstWorkingDay,"mm/dd/yyyy")
Week Number = DATEDIFF(Opportunity[Quarter Start Date],Opportunity[Close Date], WEEK ) +1
Sales running total by Week = 
CALCULATE(
	SUM(Opportunity[Amount]),
	FILTER(
		ALLSELECTED(Opportunity[Week Number]),
		ISONORAFTER(Opportunity[Week Number], MAX(Opportunity[Week Number]), DESC)
	)
)

Finally, I have created a chart but I can't restrict rolling 5 quarter Trend It shows All Quarter Value.

 

how can I achieve this logic?

 

Screenshot_6.png

 

 

 

 

 

 

1 ACCEPTED SOLUTION
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,

 

First of all, we create a calculated table as slicer using following formula:

 

Table =
ADDCOLUMNS (
    CROSSJOIN ( DISTINCT ( 'Opportunity'[Close Date].[Year] ), { 1, 2, 3, 4 } ),
    "Year-Q", [Close Date].[Year] & "-Q" & [Value]
)

Then create Five measures to meet your requirement:

 

Current Quarter = 
IF (
    HASONEVALUE ( 'Table'[Year-Q] ),
    CALCULATE (
        SUM ( Opportunity[Amount] ),
        FILTER (
            ALLSELECTED ( Opportunity[Week Number] ),
            ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
        ),
        FILTER ( ALLSELECTED ( Opportunity ), [Year-Q] = VALUES ( 'Table'[Year-Q] ) )
    )
)
Previous Quarter = 
IF (
    HASONEVALUE ( 'Table'[Year-Q] ),
    CALCULATE (
        SUM ( Opportunity[Amount] ),
        FILTER (
            ALLSELECTED ( Opportunity[Week Number] ),
            ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
        ),
        FILTER (
            ALLSELECTED ( Opportunity ),
            [Year-Q]
                = LOOKUPVALUE (
                    'Table'[Year-Q],
                    'Table'[Year], IF (
                        MAX ( 'Table'[Value] ) = 1,
                        CALCULATE ( MAX ( 'Table'[Year] ) ) - 1,
                        CALCULATE ( MAX ( 'Table'[Year] ) )
                    ),
                    'Table'[Value], IF ( MAX ( 'Table'[Value] ) = 1, 4, CALCULATE ( MAX ( 'Table'[Value] ) ) - 1 )
                )
        )
    )
)
This Quarter in Previous Year = 
IF (
    HASONEVALUE ( 'Table'[Year-Q] ),
    CALCULATE (
        SUM ( Opportunity[Amount] ),
        FILTER (
            ALLSELECTED ( Opportunity[Week Number] ),
            ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
        ),
        FILTER (
            ALLSELECTED ( Opportunity ),
            [Year-Q]
                = LOOKUPVALUE (
                    'Table'[Year-Q],
                    'Table'[Year], CALCULATE ( MAX ( 'Table'[Year] ) ) - 1,
                    'Table'[Value], CALCULATE ( MAX ( 'Table'[Value] ) )
                )
        )
    )
)
Previous Two Quarter = 
VAR q =
    SWITCH ( CALCULATE ( MAX ( 'Table'[Value] ) ), 1, 3, 2, 4, 3, 1, 4, 2 )
RETURN
    IF (
        HASONEVALUE ( 'Table'[Year-Q] ),
        CALCULATE (
            SUM ( Opportunity[Amount] ),
            FILTER (
                ALLSELECTED ( Opportunity[Week Number] ),
                ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
            ),
            FILTER (
                ALLSELECTED ( Opportunity ),
                [Year-Q]
                    = LOOKUPVALUE (
                        'Table'[Year-Q],
                        'Table'[Year], IF (
                            MAX ( 'Table'[Value] ) <= 2,
                            CALCULATE ( MAX ( 'Table'[Year] ) ) - 1,
                            CALCULATE ( MAX ( 'Table'[Year] ) )
                        ),
                        'Table'[Value], q
                    )
            )
        )
    )
Previous Three Quarter = 
VAR q =
    SWITCH ( CALCULATE ( MAX ( 'Table'[Value] ) ), 1, 2, 2, 3, 3, 4, 4, 1 )
RETURN
    IF (
        HASONEVALUE ( 'Table'[Year-Q] ),
        CALCULATE (
            SUM ( Opportunity[Amount] ),
            FILTER (
                ALLSELECTED ( Opportunity[Week Number] ),
                ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
            ),
            FILTER (
                ALLSELECTED ( Opportunity ),
                [Year-Q]
                    = LOOKUPVALUE (
                        'Table'[Year-Q],
                        'Table'[Year], IF (
                            MAX ( 'Table'[Value] ) <= 3,
                            CALCULATE ( MAX ( 'Table'[Year] ) ) - 1,
                            CALCULATE ( MAX ( 'Table'[Year] ) )
                        ),
                        'Table'[Value], q
                    )
            )
        )
    )

3.PNG


BTW, pbix as attached.

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,

 

First of all, we create a calculated table as slicer using following formula:

 

Table =
ADDCOLUMNS (
    CROSSJOIN ( DISTINCT ( 'Opportunity'[Close Date].[Year] ), { 1, 2, 3, 4 } ),
    "Year-Q", [Close Date].[Year] & "-Q" & [Value]
)

Then create Five measures to meet your requirement:

 

Current Quarter = 
IF (
    HASONEVALUE ( 'Table'[Year-Q] ),
    CALCULATE (
        SUM ( Opportunity[Amount] ),
        FILTER (
            ALLSELECTED ( Opportunity[Week Number] ),
            ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
        ),
        FILTER ( ALLSELECTED ( Opportunity ), [Year-Q] = VALUES ( 'Table'[Year-Q] ) )
    )
)
Previous Quarter = 
IF (
    HASONEVALUE ( 'Table'[Year-Q] ),
    CALCULATE (
        SUM ( Opportunity[Amount] ),
        FILTER (
            ALLSELECTED ( Opportunity[Week Number] ),
            ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
        ),
        FILTER (
            ALLSELECTED ( Opportunity ),
            [Year-Q]
                = LOOKUPVALUE (
                    'Table'[Year-Q],
                    'Table'[Year], IF (
                        MAX ( 'Table'[Value] ) = 1,
                        CALCULATE ( MAX ( 'Table'[Year] ) ) - 1,
                        CALCULATE ( MAX ( 'Table'[Year] ) )
                    ),
                    'Table'[Value], IF ( MAX ( 'Table'[Value] ) = 1, 4, CALCULATE ( MAX ( 'Table'[Value] ) ) - 1 )
                )
        )
    )
)
This Quarter in Previous Year = 
IF (
    HASONEVALUE ( 'Table'[Year-Q] ),
    CALCULATE (
        SUM ( Opportunity[Amount] ),
        FILTER (
            ALLSELECTED ( Opportunity[Week Number] ),
            ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
        ),
        FILTER (
            ALLSELECTED ( Opportunity ),
            [Year-Q]
                = LOOKUPVALUE (
                    'Table'[Year-Q],
                    'Table'[Year], CALCULATE ( MAX ( 'Table'[Year] ) ) - 1,
                    'Table'[Value], CALCULATE ( MAX ( 'Table'[Value] ) )
                )
        )
    )
)
Previous Two Quarter = 
VAR q =
    SWITCH ( CALCULATE ( MAX ( 'Table'[Value] ) ), 1, 3, 2, 4, 3, 1, 4, 2 )
RETURN
    IF (
        HASONEVALUE ( 'Table'[Year-Q] ),
        CALCULATE (
            SUM ( Opportunity[Amount] ),
            FILTER (
                ALLSELECTED ( Opportunity[Week Number] ),
                ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
            ),
            FILTER (
                ALLSELECTED ( Opportunity ),
                [Year-Q]
                    = LOOKUPVALUE (
                        'Table'[Year-Q],
                        'Table'[Year], IF (
                            MAX ( 'Table'[Value] ) <= 2,
                            CALCULATE ( MAX ( 'Table'[Year] ) ) - 1,
                            CALCULATE ( MAX ( 'Table'[Year] ) )
                        ),
                        'Table'[Value], q
                    )
            )
        )
    )
Previous Three Quarter = 
VAR q =
    SWITCH ( CALCULATE ( MAX ( 'Table'[Value] ) ), 1, 2, 2, 3, 3, 4, 4, 1 )
RETURN
    IF (
        HASONEVALUE ( 'Table'[Year-Q] ),
        CALCULATE (
            SUM ( Opportunity[Amount] ),
            FILTER (
                ALLSELECTED ( Opportunity[Week Number] ),
                ISONORAFTER ( Opportunity[Week Number], MAX ( Opportunity[Week Number] ), DESC )
            ),
            FILTER (
                ALLSELECTED ( Opportunity ),
                [Year-Q]
                    = LOOKUPVALUE (
                        'Table'[Year-Q],
                        'Table'[Year], IF (
                            MAX ( 'Table'[Value] ) <= 3,
                            CALCULATE ( MAX ( 'Table'[Year] ) ) - 1,
                            CALCULATE ( MAX ( 'Table'[Year] ) )
                        ),
                        'Table'[Value], q
                    )
            )
        )
    )

3.PNG


BTW, pbix as attached.

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Thank You @v-lid-msft 

 

Now I got the Expected Result. I spent almost two days on this result. Thank Very much for sharing your knowledge 

amitchandak
Super User
Super User

One of the ways is to use visual or page level filters. There you can use relative date filtering. But It only has option week, month and year.

 

Or use a formula like this

 

five_qtr_data = 
Var _start_date= minx(ALLSELECTED('Date'),STARTOFMONTH(dateadd('Date'[Date Filer],-5,QUARTER)))
Var  _end_date =maxx(ALLSELECTED('Date'),ENDOFMONTH('Date'[Date Filer]))

Var _5_qtr_data= CALCULATE(sum(Sales[Sales Amount]),Sales[Sales Date] >= _start_date && (Sales[Sales Date]) <= _end_date)
return
_5_qtr_data

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks.

 

Anonymous
Not applicable

Thanks @amitchandak  for the quick response

 

I tried Your solution but not getting expected result 

 

can you please share your Pbix file which you have tried using my sample data.

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.