Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
Requirement:
Hi,
I am new to complex Power BI tasks. I have a task in which I have to show data for SELECTED PERIOD (This Year, This Month, This Quarter, This Week, This Day). Here, THIS means CURRENT. There should be no DATE SLICER. The report should show current vs previous data depending on TODAY's date.
I have to show comparision between Current vs Previous periods using CARDS or KPIs which ever suits.
For ecample:
If user selects THIS MONTH from PERIODS dropdown, the CARD or KPI should show CURRENT MTD REVENUE vs PREIOUS MTD REVENUE along with %age change. Also, if current Revenue is greater than last month revenue so far, color it GREEN or else color it RED.
What i have done so far:
Periods = {
("This Year", NAMEOF('Date'[Year]),0,"This Year"),
("This Quarter", NAMEOF('Date'[Quarter]), 1,"This Quarter"),
("This Month", NAMEOF('Date'[Month]), 2,"This Month"),
("This Week", NAMEOF('Date'[WeekOfMonth]), 3,"This Week"),
("This Day", NAMEOF('Date'[Day]), 4,"This Day")
}
Selected Period Previous Revenue =
VAR SelectedPeriod = SELECTEDVALUE('Dates'[Period],"This Day")
VAR DynamicValue =
SWITCH(
TRUE(),
SelectedPeriod = "This Day", CALCULATE([Revenue], DATEADD('Date'[Date],-1, DAY)),
SelectedPeriod = "This Week", CALCULATE([Revenue],DATEADD('Date'[Date],-7, DAY)),
SelectedPeriod = "This Month", CALCULATE([Revenue], DATEADD('Date'[Date],-1, MONTH)),
SelectedPeriod = "This Quarter", CALCULATE([Revenue],DATEADD('Date'[Date],-1, QUARTER)),
SelectedPeriod = "This Year", CALCULATE([Revenue],DATEADD('Date'[Date],-1, YEAR)),
BLANK()
)
RETURN
DynamicValue
Selected Period Current Revenue =
VAR SelectedPeriod = SELECTEDVALUE('Dates'[Period],"This Day")
VAR DynamicValue =
SWITCH(
TRUE(),
SelectedPeriod = "This Day", CALCULATE([Revenue], FORMAT ('Date'[Date],"yyyy-mm-dd") = FORMAT(TODAY(),"yyyy-mm-dd")),
SelectedPeriod = "This Week", CALCULATE([Revenue],'Date'[WeekOfMonth] = WEEKNUM(TODAY()),'Date'[Month] = MONTH(TODAY()),'Date'[Quarter] = QUARTER(TODAY()),'Date'[Year] = YEAR(TODAY())),
SelectedPeriod = "This Month", CALCULATE([Revenue],'Date'[Month] = MONTH(TODAY()) , 'Date'[Year] = YEAR(TODAY())),
SelectedPeriod = "This Quarter", CALCULATE([Revenue],'Date'[Quarter] = QUARTER(TODAY()),'Date'[Year] = YEAR(TODAY())),
SelectedPeriod = "This Year", CALCULATE([Revenue],'Date'[Year] = YEAR(TODAY())),
BLANK()
)
RETURN
DynamicValue
Need help to achieve this case. Or share anyother better way to achieve this.
Thanks,
Rizwan.
Hi @MRIZ ,
If I understand correctly, the issue is that you want to show data for selected period. Please try the following methods and check if they can solve your problem:
1.Ensure that the Date table in Power BI that it contains a continuous date range.
2.Review the logic within the DAX formula.
Selected Period Previous Revenue =
VAR SelectedPeriod = SELECTEDVALUE('Dates'[Period], "This Day")
VAR StartDate = TODAY()
VAR EndDate =
SWITCH(
SelectedPeriod,
"This Day", StartDate,
"This Week", StartDate - WEEKDAY(StartDate, 2) + 1,
"This Month", DATE(YEAR(StartDate), MONTH(StartDate), 1),
"This Quarter", DATE(YEAR(StartDate), QUARTER(StartDate) * 3 - 2, 1),
"This Year", DATE(YEAR(StartDate), 1, 1),
StartDate
)
VAR PeriodLength =
SWITCH(
SelectedPeriod,
"This Day", 1,
"This Week", 7,
"This Month", DAY(EOMONTH(StartDate, -1)),
"This Quarter", DAY(EOMONTH(StartDate, -MONTH(StartDate) MOD 3)),
"This Year", DAY(EOMONTH(StartDate, -MONTH(StartDate) + 1)),
1
)
VAR PreviousPeriod = DATESINPERIOD('Date'[Date], EndDate, -PeriodLength, DAY)
RETURN
CALCULATE([Revenue], PreviousPeriod)
3.Check that the measures are evaluated within the correct context.
Best Regards,
Wisdom Wu
Hi Brother,
Thanks for taking time and responding to help me out.
Yes the Date Table has continus Date Range.
Unfotunaltely the DAX you shared did not work. See the output in the image below. I have selected "This Year" but you can see that the Previous Year is showing "2099" and showing "1.9K" revenue. Actual Revenue in 2023 was "8.5K" .Also attaching DAX again here which i used ( check the MOD method if i used it correctly as your MOD was giving error).
Selected Period Previous Revenue =
VAR SelectedPeriod = SELECTEDVALUE('Dates'[Period],"This Day")
VAR StartDate = TODAY()
VAR EndDate =
SWITCH(
SelectedPeriod,
"This Day", StartDate,
"This Week", StartDate - WEEKDAY(StartDAte,2) + 1,
"This Month", DATE(YEAR(StartDate), MONTH(StartDate) * 3 - 2, 1),
"This Quarter", DATE(YEAR(StartDate), QUARTER(StartDate) * 3 - 2, 1),
"This Year", DATE(YEAR(StartDAte), 1, 1),
StartDate
)
VAR PeriodLength =
SWITCH(
SelectedPeriod,
"This Day", 1,
"This Week", 7,
"This Month", DAY(EOMONTH(StartDate, -1)),
"This Quarter", DAY(EOMONTH(StartDate, MOD (-MONTH(StartDate) , 3))),
"This Year", DAY(EOMONTH(StartDate, -MONTH(StartDate) + 1)),
1
)
VAR PreviousPeriod = DATESINPERIOD('Date'[Date], EndDate, -PeriodLength, DAY)
RETURN
CALCULATE([Revenue], PreviousPeriod)
Regards,
Rizwan.
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
113 | |
107 | |
99 | |
39 | |
30 |