The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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.
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
Solved! Go to Solution.
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
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
User | Count |
---|---|
26 | |
12 | |
8 | |
8 | |
5 |
User | Count |
---|---|
30 | |
14 | |
12 | |
12 | |
7 |