PowerBI Getting ERROR A function 'CALCULATE' has been used - powerbi

I would like to show the card values:
if current month value is not available then it should compare with the previous month value automatically.
Like
In above image Apr-2017 value is not available then it should compare with the Mar-2017 value.
If Mar-2017 value also not available then the previous month value.Keep on goes.
And one more thing is I don't want to use the slicer anymore. Actually I am not going to use the slicer.
I just want to show the month comparison value in a card visuals.
I would like to give info of what kind of data that I have. I have table's plan rev, actual rev, and MonthYear
I have created a relationship using Date column from MonthYear table to plan rev and actual rev Date columns.
MonthYear table
And in plan rev, month year, plan rev, date, and in actual rev actual rev, month year, and date columns.
Then written measures for
For Sumof Plan rev-->
PlanSum = SUM('Revenue Report'[Planned Rev])
For Prev month value-->
PlanPrevMon = CALCULATE([PlanSum],PREVIOUSMONTH('Month Year'[Date]))
For Start and End Date of the months
FILTERDATESTART= FIRSTDATE('MonthYear'[Date])
FILTERDATEEND= LASTDATE('MonthYear'[Date])
Then for current month PlanArrows measure is.
PlanArrows = CALCULATE(
IF(
ISBLANK([PlanSum]),"No data Available",[PlanSum]
)& " "& IF(
[PlanSum]=[PlanPrevMon],
"",
IF([PlanSum]>[PlanPrevMon],
UNICHAR(8679),
UNICHAR(8681)
) & IF([PlanSum]<=0,
"",""
)
),
'Month Year'[Date]>=FILTERDATESTART,
'Month Year'[Date]<=FILTERDATEEND
)
But it is not working. I'm getting an error:
A function 'CALCULATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.
Any Suggestions please,
Thanks
Mohan V

I think i got the solution on my own.
I have written measure which solved this issue.
PlanArrows =
VAR s = [FILTERDATESTART]
VAR e = [FILTERDATEEND]
RETURN
CALCULATE (
IF ( ISBLANK ( [PlanSum] ), "No data Available", [PlanSum] ) & " "
& IF (
[PlanSum] = [PlanPrevMon],
"",
IF ( [PlanSum] > [PlanPrevMon], UNICHAR(8679), UNICHAR(8681) )
& IF ( [PlanSum] <= 0, "", "" )
),
'Month Year'[Date] >= s
&& 'Month Year'[Date] <= e
)

Related

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

How to get the value at the latest date, where values are not null?

I have a table like this. Notice the blank values towards the bottom.
I would like to get the value of the latest date, but the value can't be null.
My DAX formula is bringing back the value of the latest date (19/12/2021) which is nullhowever I want to bring back the latest non-null value, which is for the date 21/11/2021.
Here is what I have tried so far:
Latest Value =
CALCULATE(
// get sum of value column
SUM('Table1'[Value]),
// where value is not blank, and date is max date
'Table1'[Value] <> BLANK() && Table1[Date] = MAX(Table1[Date])
)
I thought this should bring back the figure 305? Because my conditions are:
where value is not null AND where date = max date
Shouldn't the max date now be 21/11/21 because the nulls have been removed?
Another piece of DAX I've tried, using the fiter function.
Latest Value = CALCULATE(
SUM('Table1'[Value]),
FILTER(ALL('Table1'),
'Table1'[Value] <> BLANK()
&&
'Table1'[Date] = MAX('Table1'[Date]))
Where am I going wrong? I think it's something to do with my max date section.
Unfortunately all file hosters are blocked in work, so I can't share this dummy file.
The idea is to filter the table first and get the max value from the date column. In my case, I saved that date in a variable last_date. Then we just select a value from the Value column using filter by last_date.
LatestValue =
VAR last_date =
CALCULATE ( MAX ( 'Table1'[Date] ), 'Table1'[Value] <> BLANK () )
RETURN
CALCULATE ( SELECTEDVALUE ( 'Table1'[Value] ), 'Table1'[Date] = last_date )
or the same expression with SUM:
LatestSumOfValues =
VAR last_date = CALCULATE(MAX('Table1'[Date]),'Table1'[Value] <> BLANK())
RETURN
CALCULATE(SUM('Table1'[Value]),'Table1'[Date] = last_date)

DAX formula for previous month's values

I'm new to DAX. In order to get the previous month's sales, I have used DATESBETWEEN and passed MAX(Date)-1 as the start date and as the end date EOMONTH.
I got the correct results. However, what are the other simpler ways of getting the same?
CALCULATE (
SUM ( AW_Sales[Sales Amount] ),
DATESBETWEEN (
AW_Calender_Lookup[Date],
DATE ( YEAR ( MAX ( AW_Calender_Lookup[Date] ) ), MONTH ( MAX ( AW_Calender_Lookup[Date] ) ) - 1, 01 ),
EOMONTH (
DATE ( YEAR ( MAX ( AW_Calender_Lookup[Date] ) ), MONTH ( MAX ( AW_Calender_Lookup[Date] ) ) - 1, 01 ),
0
)
)
)
update - based on an answer given I added a new measure, but the value is empty.
If you have a Dates table which is basically a calendar table, there is a simple way. Your data/fact table has to be connected to the Dates table using the date column for say date. Now you can create your Date slicer from the Dates table. For say, you selected the month September-2020 from the slicer, this below measure will return the SUM for current month-
total_sales = SUM(table_name[sales])
And this below measure will return the total sales for previous month-
total_sales_previous_month =
CALCULATE(
[total_sales],
PREVIOUSMONTH('Dates'[date])
)
For more information on DAX function PREVIOUSMONTH, you can check this.

Rank and Rank Increment/Decrement from selected week vs previous week

I have a table visual with branch, client and revenue information. The revenue is coming from a measure which is affected by the week selector slicer.
I need to show Rank for selected week and rank increment/decrement from selected week's rank vs previous week from selected week. In snapshot, Rank # and Rank are the requirements.
I tried to create rank with AllSelected function but it is always affected by week slicer and I cannot get previous week rank to compare and put Rank increment/decrement.
DAX I tried for Rank
This Week GP =
var _Total =
CALCULATE(
SUM(V_TopClients[revenue]),
DATESBETWEEN('Date'[date],[current_week_start_date], [current_week_end_date])
)
return _Total
Previous Week GP =
var _Total =
CALCULATE(
SUM(V_TopClients[revenue]),
DATESBETWEEN('Date'[date],[previous_week_start_date], [previous_week_end_date])
)
return _Total
Rank This Week =
RANKX(
ALLSELECTED(V_TopClients),
CALCULATE(SUM(V_TopClients[Revenue])
)
)
--Update: added dax for measures
Here I am able to get the rank for this week i.e. the week selected on slicer.
But unable to get rank for previous week.
I have v_TopClients that has weekly revenue information linking to dimCalendar, Date tables.
Here is another and probably the best option for you-
Step-1:
Create a new custom table based on your table "TopClient". The code is as below-
group_by_result_new =
VAR group_wise_revenue =
GROUPBY (
TopClients,
TopClients[CalendarWeekKey],
TopClients[clientID],
"gp_this_week", SUMX(CURRENTGROUP(), TopClients[GrossProfit])
)
RETURN
SELECTCOLUMNS (
group_wise_revenue,
"CalendarWeekKey", TopClients[CalendarWeekKey],
"clientID", TopClients[clientID],
"gp_this_week", [gp_this_week]
)
Step-2:
Create relation between table "DimCalendar" and "group_by_result_new" using the column "CalendarWeekKey"
Step-3:
Create a new column (remember it a column) as below-
gp_prev_week =
VAR client_id = group_by_result_new[clientID]
VAR calendar_key_this_week = group_by_result_new[CalendarWeekKey]
VAR end_date_this_week =
LOOKUPVALUE(
DimCalendar[WeekEndingDate],
DimCalendar[CalendarWeekKey], CONVERT(calendar_key_this_week,INTEGER)
)
VAR end_date_prev_week = CONVERT(end_date_this_week,DATETIME) - 7
VAR calendar_key_prev_week =
LOOKUPVALUE(
DimCalendar[CalendarWeekKey],
DimCalendar[WeekEndingDate] , end_date_prev_week
)
VAR gp_prev_week =
LOOKUPVALUE(
group_by_result_new[gp_this_week],
group_by_result_new[CalendarWeekKey],calendar_key_prev_week,
group_by_result_new[clientID], CONVERT(client_id,INTEGER)
)
RETURN gp_prev_week
Step-4:
Create a new column (remember it a column) for RANK this week as below-
rank_this_week =
RANKX (
FILTER (
group_by_result_new,
group_by_result_new[CalendarWeekKey] = EARLIER (group_by_result_new[CalendarWeekKey])
),
group_by_result_new[gp_this_week],
,
DESC
// ,
// DENSE
)
Step-5:
Create a new column (remember it a column) for RANK prev week as below-
rank_prev_week =
RANKX (
FILTER (
group_by_result_new,
group_by_result_new[CalendarWeekKey] = EARLIER (group_by_result_new[CalendarWeekKey])
),
group_by_result_new[gp_prev_week],
,
DESC
// ,
// DENSE
)
And that's all! This should also work same as my previous solution.
Cheers!!
In DAX, there are builtin function PREVIOUSMONTH, PREVIOUSQUARTER and PREVIOUSYEAR available. But as you are searching for weekly data comparison, you required your own date periods to calculate. I just can give you some idea as below-
First, crate 4 measure based on your slicer week/date selection.
Example:
current_week_end_date = SELECTEDVALUE(Dates[Date])
current_week_start_date = SELECTEDVALUE(Dates[Date]) - 7
previous_week_end_date = SELECTEDVALUE(Dates[Date]) - 8
previous_week_start_date = SELECTEDVALUE(Dates[Date]) - 15
Now, you need 2 separate measure to calculate this week and previous week total revenue. Example Measures are given below-
1.
this_week_revenue =
CALCULATE(
SUM(table[revenue]),
DATESBETWEEN(
'Dates'[Date],
[current_week_start_date],
[current_week_end_date]
)
)
2.
previous_week_revenue =
CALCULATE(
SUM(table[revenue]),
DATESBETWEEN(
'Dates'[Date],
[previous_week_start_date],
[previous_week_end_date]
)
)
Now you have both weekly value in your hand and you can compare measure "this_week_revenue " with measure "previous_week_revenue" to generate the directions indicators.
Hope this will help!
Below image is just for reference:

How to find DateDiff from same column with condition in Power BI

I am having one table in PowerBI which is having 3 columns: 1.EnrollId 2.Status 3.StatusChangeDate. One EnrollId is having 4 statuses and their particular statusChangeDates. I want to find no. of days between two dates with status condition.
EnrollId Status StatusChangeDate
101 AppStart 15/02/2019
101 Application 27/03/2019
101 Enrollment 03/04/2019
101 Complete 28/04/2019
I want to create formula in DAX like
[StatusChangeDate (where Status="Enrollment") - StatusChangeDate(where status="AppStart)]
or
[StatusChangeDate (where Status="Complete")- StatusChangeDate(where status="Enrollment)]
i.e. 03/04/2019 - 15/02/2019 = 44 Days
28/04/2019 - 03/04/2019 = 25 Days
My advice is go for a Calculated Column, you can try and adjust this to a measure but there is more factors to consider regarding the filter context.
For a calculated column:
DaysLastChange =
VAR _currentStatusChangeDate = [StatusChangeDate]
VAR _currentEnrollId = [EnrollId]
RETURN
DATEDIFF(
CALCULATE(MAX('Table1'[StatusChangeDate]);FILTER('Table1';_currentEnrollId = [EnrollId] && _currentStatusChangeDate > [StatusChangeDate] ));
_currentStatusChangeDate;
DAY
)
If you want measures, try something like this. For each period between two Statuses, you need to create another measure. When more than one [EnrollId] is selected, the measure returns a blank.
Days Enrollment - Complete =
VAR scDateEnrollment =
CALCULATE ( MAX ( 'Table'[StatusChangeDate] ), 'Table'[Status] = "Enrollment" )
VAR scDateComplete =
CALCULATE ( MAX ( 'Table'[StatusChangeDate] ), 'Table'[Status] = "Complete" )
RETURN
IF (
HASONEVALUE ( 'Table'[EnrollId] ),
DATEDIFF ( scDateEnrollment, scDateComplete, DAY )
)
Cardvisuals or a table-visual would look like this. As you can see the table-visual is not affected by the slicer.
EDIT
This is the table i used: