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

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)

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]))
)
)

Keep current month selected in slicer in Power BI

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" )
)

DAX - Time Intelligence - Year to Quarter

My Dataset
am trying create a report based on the selected quarter
What I want is sum of sales by quarter grouped by product in one column and sum of sales for the year until selected quarter for that year.
Example: (this is what i got ... not right though)
Model:
DAX:
ByProductforselectedquarter =
VAR vTable =
SUMMARIZE (
sales,
Sales[Product], Sales[Sales] )
VAR Result =
SUMX ( vTable, Sales[Sales] )
RETURN
Result
how to get sales for the year until selected quarter for that year.
You can try something as below-
ByProductUptoselectedquarter =
var selected_max_date = max(Date[Date])
var selected_year = Year(selected_max_date)
return
calculate(
sum(Sales[Sales]),
filter(
all(Sales),
Year(Sales[Date]) = selected_year
&& Sales[Date] <= selected_max_date
)
)

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.

Total month to date for last year until a specific day of the month

I want to calculate the sum of MTD sales per day for the same period of last year, only until a specific day - the day of the last sales transaction.
So for example, if the filter is year= 2019 and month= 2, I need the MTD sales for february 2018 until the 5th, calculated day by day:
MTDSales=
VAR MyDay = 5
RETURN
CALCULATE(
TOTALMTD(SUM(Sales); Calendar[Date]);
SAMEPERIODLASTYEAR(Calendar[Date]);
//here I need another filter to stop on the 5th!
)
Edit:
Please have a look at this link to see the sample data.
The measures I'm trying to build are:
Sales MTD CY
Sales MTD LY
Sales MTD CY*
Sales MTD LY*
Sales MTD CY**
Sales MTD LY**
Thanks for helping!
I'm assuming that you are using 5 since today is February 5th.
You can get MTDSales for the current month like this:
MTDSales =
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(TODAY(), - 1) + 1, TODAY() )
RETURN CALCULATE( SUM( Sales ), DateRange )
To match that up with the previous year, just use SAMEPERIODLASTYEAR.
LastYearMTDSales =
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(TODAY(), - 1) + 1, TODAY() )
RETURN CALCULATE( SUM( Sales ), SAMEPERIODLASTYEAR(DateRange) )
If you want to use a different date than TODAY, just specify that date as a variable and pass it into the DateRange variable where TODAY appears.
If you want to find the MTDSales up to the 5th day of the month (assuming you have the month in your filter context), try this
MTDSales =
Var MyDay = 5
VAR MyDate = MIN( Calendar[Date] ) + MyDay - 1
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(MyDate, -1) + 1, MyDate )
RETURN CALCULATE( [Sum of Sales], DateRange )
Then for the previous year, you can reuse that measure, but shifted:
PrevYearMTDSales =
CALCULATE( [MTDSales], ALL( Calendar ), SAMEPERIODLASTYEAR( Calendar[Date] ) )
Edit: After looking at your PBIX, I realized that I had made the wrong assumption about your filter context. Since you are looking to write a measure at the date level, try this instead:
Sales MTD CY =
VAR MyDay = 5
VAR CurrentDate = MAX ( 'Calendar'[Date] )
VAR MyDate = EOMONTH ( CurrentDate, -1 ) + MIN ( MyDay, DAY ( CurrentDate ) )
RETURN
CALCULATE (
TOTALMTD ( SUM ( Sales[Sales] ), 'Calendar'[Date] ),
FILTER ( 'Calendar', 'Calendar'[Date] <= MyDate )
)
The previous year measure can still be done referencing this measure and shifting.
Replace my column names for yours and it should work
The formula remains the same:
MTDSales =
VAR MyDay = 5
RETURN
CALCULATE(
TOTALMTD([total sales], 'Calendar'[DateKey]),
SAMEPERIODLASTYEAR('Calendar'[DateKey]),
FILTER(
ALL(Sales),
Sales[DateKey] >= STARTOFMONTH('Calendar'[DateKey]) && Sales[DateKey] <= DATEADD(STARTOFMONTH(Sales[DateKey]),MyDay-1,DAY)
)
)