Keep current month selected in slicer in Power BI - powerbi

I have a slicer having a list of MONTH-YEAR list like this:
Jul-22
Aug-22
Sep-22
Oct-22
...
...
From the above list i want current month to be selected.
I have tried with the code below:-
CurrMonth = if(month([Date])=Month(NOW()),month([Date]))
and got the current month but not able to set in the slicer.
any help please...

CurrMonth =
VAR _thismonthstart = DATE(YEAR(TODAY()),MONTH(TODAY()),1)
VAR _thismonthfinish = EOMONTH(_thismonthstart,0)
return
if [Date] >= _thismonthstart && [Date] <= _thismonthfinish then 1 else 0
alternatively, you can use the Filter on the visual
another alternative is to create a custom Special Dates Table which has a cross filter direction both ways with your date table.
Sample Solution

I have found the solution as below:
CurrMonth = if(month([Date])=Month(NOW()),CurMonth,FORMAT([Date], "MMM") & " " & [Year])
and set CurrMonth as the Field of the Slicer.

CurrMonth =
IF (
SELECTEDVALUE ( [Date] ) = FORMAT ( NOW (), "mmm-yy" ),
[Date],
FORMAT ( NOW (), "mmm-yy" )
)

Related

DAX Formula to filter the last 28 days in the table data

I have a measure that totals the values for each date in the table. I want to filter this measure so that I can display only the last 28 days present in the table instead of displaying values for all the dates. Following is the code that works for getting totals for full table:
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) ) )
The 'relative date' filter in the Filters pane does not work because it only accepts the last 28 days based on today's date and not the dates in the table. Please suggest a DAX formula that can filter for the last 28 days present in the table.
Try this code
VAR endDay = LastDate(Daily_Reports[Case_Date])
VAR startDay= DATEADD(endDay,-28,DAY)
VAR setOfDates = DATESBETWEEN(Daily_Reports[Case_Date], StartDate, EndDate )
RETURN
CALCULATE(
SUM(Daily_Reports[Confirmed])
,setOfDates
)
You can try this one:
MS =
CALCULATE (
SUM ( Daily_Reports[Confirmed] ),
FILTER (
ALL ( Daily_Reports[Case_Date] ),
Daily_Reports[Case_Date]
>= SELECTEDVALUE ( Daily_Reports[Case_Date] ) - 28
)
)
This is what finally worked for me. I created a measure (not a column), that returns 1 for the last 28 days with an IF clause, leaving it blank if the date is not in the last 28 days as follows:
Last28 =
VAR MaxDate = LASTDATE( ALL(Daily_Reports[Case_Date]) )
VAR MinDate = DATEADD( MaxDate, -28, DAY )
RETURN
IF( SELECTEDVALUE(Daily_Reports[Case_Date]) >= MinDate && SELECTEDVALUE(Daily_Reports[Case_Date]) <= MaxDate, 1 )
Then I incorporated this measure into the Calculate function as follows:
Daily Cases =
CALCULATE( SUM(Daily_Reports[Confirmed]),
FILTER( ALL(Daily_Reports),
Daily_Reports[Case_Date] = SELECTEDVALUE(Daily_Reports[Case_Date]) && NOT(ISBLANK(Daily_Reports[Last28]))
)
)

Show cumulative SalesAmount column chart to max date in slicer

I am looking to build a column chart in Power BI that displays cumulative sales by month up to max date selected in date slicer. I have a supplementary Date2 table that is related to Date table (inactive relation). Tried something like this, but I keep getting all the months irrespective which one is selected in slicer:
Sales to date selected =
VAR ReferenceDate = MAX ( 'Date'[Date] )
Return
CALCULATE ( [SalesAmount],
ALLEXCEPT('Date', 'Date'[Year]),
FILTER( all(Date2[Date]), Date2[Date] <= MAX( Date2[Date] )
&& Date2[Date] <= ReferenceDate),
USERELATIONSHIP ( 'Date'[Date], Date2[Date] )
)
This is what I get as a result (Slicer selects month 7):
It looks like CALCULATE does not honor filter Date2[Date] <= ReferenceDate . What am I doing wrong here?
OK, looks like this scenario solves my issue. I removed inactive relationship between Date and Date2 and introduced active relationship from Sales to Date2.
Sales to date selected =
VAR ReferenceDate = MAX ( 'Date'[Date] )
Return
IF( MAX(Date2[Date]) <= ReferenceDate,
CALCULATE ( [SalesAmount],
ALLEXCEPT('Date', 'Date'[Year]),
Date2[Date] <= MAX( Date2[Date] )
)
)
I made a correction to your code; but It seems that It takes a long time for it to be approved by community members:
So test this:
Sales to date selected =
VAR ReferenceDate =
MAX ( ALLSELECTED ( 'Date2'[Date] ) )
RETURN
CALCULATE (
[SalesAmount],
FILTER ( ALLEXCEPT ( 'Date2', 'Date2'[Year] ), 'Date2'[Date] <= ReferenceDate )
)
Or This:
Sales to date selected =
VAR ReferenceDate =
MAX ( ALLSELECTED ( 'Date2'[Date] ) )
RETURN
CALCULATE (
[SalesAmount],
FILTER ( ALL ( 'Date2'[Date] ), 'Date2'[Date] <= ReferenceDate )
)

Do a select on different column than where clause in DAX?

I wanted to do achieve this “sql” code in a DAX variable
Select WeekYearWeekNumber from DimDate where Date = today()+7
How can achieve it?
So far I have this code, but it doesn’t work because nextw is more than a column…
var nextw =
FILTER(
SUMMARIZE('Date','Date'[Week Year Week Number],'Date'[Date]) ,
'Date'[Date]=TODAY()+7
)
var NextWCS =
FILTER(
SUMMARIZE(Staff, Staff[K_Emp], Staff[StaffingType], 'Date'[Week Year Week Number], Staff[IsTmed],
"sumat", SUM(Staff[Hours])
),
'Date'[Week Year Week Number]=nextw
)
This will return a single value for nextw:
var nextw = LOOKUPVALUE('Date'[WeekYearWeekNumber],'Date'[Date],TODAY()+7)

Trying to create a messure in PowerBI which give me last 6 month accuracy based on single selected value from date slicer

While executing the below DAX expression, I am getting an error "USERELATIONSHIP function can only use the two columns reference participation in relationship".
So could you please help me with that what's wrong with the expression?
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX(Calender[Date])
VAR Last_6Month =
DATESINPERIOD(
Calendar_Last6Month[Date].[Date],
ReferenceDate,
-6,
MONTH
)
VAR Result =
CALCULATE(
[Accuracy],
REMOVEFILTERS(Calender[Date]),
KEEPFILTERS(Last_6Month),
USERELATIONSHIP(Calender[Date],Calendar_Last6Month[Date].[Date])
)
RETURN
Result
Relationship created between tables as inactivated form:
Columns used in both the table:
You should be able to use a single Calendar. Your second calendar is redundant.
I would write something like this:
Accuracy_Last_6_Month =
CALCULATE([Accuracy],
FILTER(ALL(Calender),
Calender[Date] > MAX(Calender[Date])-180 &&
Calender[Date] <= MAX(Calender[Date])))
I'm guessing the error is because you are using Calendar_Last6Month[Date].[Date] inside USERELATIONSHIP, which isn't actually a table column. Try deleting that .[Date] suffix everywhere in your measure:
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX ( Calender[Date] )
VAR Last_6Month =
DATESINPERIOD ( Calendar_Last6Month[Date], ReferenceDate, -6, MONTH )
VAR Result =
CALCULATE (
[Accuracy],
REMOVEFILTERS ( Calender[Date] ),
KEEPFILTERS ( Last_6Month ),
USERELATIONSHIP ( Calender[Date], Calendar_Last6Month[Date] )
)
RETURN
Result
I generally avoid using these Time Intelligence suffixes entirely.

Power BI - How to create new column that summarize data by week and by Online or Store Channel?

I have WTDdata table that contains ThisYearRevenue and LastYearRevenue summarized by week:
I need to create 4 more columns LastYearOnlineRevenue, LastYearStoreRevenue, ThisYearOnlineRevenue, and ThisYearStoreRevenue from another table (RevenueByDate) that looks like this:
W column in this table means Fiscal Week.
I tried using this aproach:
LastYearOnlineRevenue =
SUMMARIZE(FILTER(ALL(FiscalCalendar),FiscalCalendar[FiscalWeek]),FiscalCalendar[FiscalWeek]),
"LastYearOnlineRevenue",CALCULATE(SUM(RevenueByDate[Revenue]),FiscalCalendar[FiscalYear] =
YEAR(TODAY())-1 && RevenueByDate[Channel] = "Online")
If you can help me with at least one column, I am assuming the logic will be the same for the other 3.
Thank you in advance.
Here is the location for sample data and .pbix file:
https://1drv.ms/u/s!AhhZq1add5YwjYIvuASi76lCL3R1eA?e=C7ObDZ
Try this:
Since there is no way of telling the current year from the WTDdata table, I have assumed that the current year is always 2021, you can also replace that with:
VALUE ( MAX ( RevenueByDate[FiscalYear] ) )
LastYearOnlineRevenue =
VAR CurrentFiscalWeek = WTDdata[FiscalWeek]
VAR CurrentYear = 2021
VAR ImmediatePreviousYear =
CALCULATE (
MAX ( FiscalCalendar[FiscalYear] ),
FiscalCalendar[FiscalYear] < CurrentYear,
REMOVEFILTERS ( WTDdata )
)
VAR Result =
CALCULATE (
SUM ( RevenueByDate[Revenue] ),
FiscalCalendar[FiscalWeek] = CurrentFiscalWeek,
FiscalCalendar[FiscalYear] = ImmediatePreviousYear,
RevenueByDate[Channel] = "Online",
REMOVEFILTERS ( WTDdata )
)
RETURN
Result