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
gumis_rulez
Helper I
Helper I

Previous period sales based on choosen granularity

I would like to create a measure which will calculate sales for previous period based on choosen granularity.

 

I followed: https://www.youtube.com/watch?v=hilfglpKNRQ to create granularity and slicer.

 

Przechwytywanie.JPG

first table is connected with slicer with visual table and this table should recalculate appropriately the sales for previous period based on selection.

 

I started to create a measure:

 

Sales Previous Period = 
VAR SelectedType = SELECTEDVALUE('hlp_Dynamic Date Selections'[Type])

RETURN
SWITCH(
    TRUE(),
    SelectedType = "Y", 
        CALCULATE(
            [Sales]
        )
)

but when I refer to dimCalendar or use time intelligence functions the result does not show. How to achieve such functionality?

 

https://drive.google.com/file/d/15UslwRjGZy-yEifeGWuSPq7_BPcB_yV-/view?usp=sharing 

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You need to remove the filters on the helper table as well as manipulating the dates in the date table.

Sales Previous Period = 
VAR SelectedType = SELECTEDVALUE('hlp_Dynamic Date Selections'[Type])
VAR PrevYear = DATEADD( dimCalendar[Date], -1, YEAR )
VAR PrevQuarter = DATEADD( dimCalendar[Date], -1, QUARTER )
VAR PrevMonth = DATEADD( dimCalendar[Date], -1, MONTH )
VAR PrevWeek = DATEADD( dimCalendar[Date], -7, DAY )
VAR PrevDay = DATEADD( dimCalendar[Date], -1, DAY )

VAR Result = SWITCH( SelectedType,
	"Y",
	CALCULATE( 
		[Sales], 
		PrevYear,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	),
	"Q",
	CALCULATE( 
		[Sales], 
		PrevQuarter,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	),
	"M",
	CALCULATE( 
		[Sales], 
		PrevMonth,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	),
	"W",
	CALCULATE( 
		[Sales], 
		PrevWeek,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	),
	"D",
	CALCULATE( 
		[Sales], 
		PrevDay,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	)
)
RETURN Result

View solution in original post

1 REPLY 1
johnt75
Super User
Super User

You need to remove the filters on the helper table as well as manipulating the dates in the date table.

Sales Previous Period = 
VAR SelectedType = SELECTEDVALUE('hlp_Dynamic Date Selections'[Type])
VAR PrevYear = DATEADD( dimCalendar[Date], -1, YEAR )
VAR PrevQuarter = DATEADD( dimCalendar[Date], -1, QUARTER )
VAR PrevMonth = DATEADD( dimCalendar[Date], -1, MONTH )
VAR PrevWeek = DATEADD( dimCalendar[Date], -7, DAY )
VAR PrevDay = DATEADD( dimCalendar[Date], -1, DAY )

VAR Result = SWITCH( SelectedType,
	"Y",
	CALCULATE( 
		[Sales], 
		PrevYear,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	),
	"Q",
	CALCULATE( 
		[Sales], 
		PrevQuarter,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	),
	"M",
	CALCULATE( 
		[Sales], 
		PrevMonth,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	),
	"W",
	CALCULATE( 
		[Sales], 
		PrevWeek,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	),
	"D",
	CALCULATE( 
		[Sales], 
		PrevDay,
		REMOVEFILTERS( 'hlp_Dynamic Date Selections' )
	)
)
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.