DAX IF Statement Can't Use Table Column - powerbi

I have the following DAX expression:
Students_Who_Viewed = var max_report_date = TOPN(1,DISTINCT(table_a_Historical[ReportDate]),table_a_Historical[ReportDate])
var last_week_report_date = max_report_date - 2
/////
var this_week_student_viewed = CALCULATE(
SUM(table_a_Historical[CntViewedByStudent_CY]),
KEEPFILTERS(table_a_Historical[ReportDate] = max_report_date)
)
/////
var last_week_student_viewed = CALCULATE(
SUM(table_a_Historical[CntViewedByStudent_CY]),
KEEPFILTERS(table_a_Historical[ReportDate] = last_week_report_date)
)
/////
var students_viewed_wow_change = COALESCE(this_week_student_viewed,0) - COALESCE(last_week_student_viewed,0)
/////
RETURN
IF (
**table_a_Historical[exempt_status_cy] = "exempt" || table_a_Historical[exempt_status_py],**
"N/A",
IF (
students_viewed_wow_change > 0 //This means views this week is more than last week.
,1
,IF (
students_viewed_wow_change < 0 //This means there were less views this week than last week
,-1
,0 //This value will be used if there is no change in views this week relative to last week.
)
)
)
In the part surrounded by **, I keep getting an error message. Those columns are valid in that table. Why is this?

As DAX search for aggregation, can you please try this below where you directly referred to the column name:
min(table_a_Historical[exempt_status_cy])
Hopefully this will work.

Related

AND function in Power BI

I'm trying to create a new mesure in order to create a gantt chart, but the dax field keeps spitting that there's an error. Where in fact i literally copied the same code in the tutorial i'm following.
And the new mesure is as follows:
CF Gantt =
var startdate =
CALCULATE(
min(Contracts_range[Start Date ],
REMOVEFILTERS(TabelaCalculada)
), var EndDate =
CALCULATE(
min(Contracts_range[End Date]),
REMOVEFILTERS(TabelaCalculada)
)
var ProjectPeriod =
min(TabelaCalculada[Date]) >= startdate
&& min(TabelaCalculada[Date]) <= startdate
var result =
if(
TabelaCalculada,
1
)
return
You see PBI is saying the following: "the syntax for '&&' is incorrect", i even searched the documentation for this particular AND method but i don't see how it's wrong.
This DAX is invalid in multiple ways which I point out on the assumption that you are trying to learn what valid DAX is.
Comma after the definition of var startdate.
Project Period is a bunch of filters with no actual function.
Project Period is trying to filter on an aggregate, which is not allowed.
return statement is missing the return argument
var result is incomprehensible.
This will return a 1 for each row where the date falls between minimum Start Date and maximum End Date, per your latest comment.
CF Gantt =
var startdate =
CALCULATE(
min(Contracts_range[Start Date]),
REMOVEFILTERS(TabelaCalculada)
)
var EndDate =
CALCULATE(
max(Contracts_range[End Date]),
REMOVEFILTERS(TabelaCalculada)
)
var result = if(
selectedvalue(TabelaCalculada[Date] >= startdate
&& TabelaCalculada[Date] <= EndDate,1)
)
return result
I've found a third syntax alternative that worked. So my goal as i explained was to create a Gantt Chart using the matrix innate visual and for that one way to do it is to create a measure that returns a 1 if i have a date that's either on or after the starting date and on or before the ending date.
BetweenDates =
VAR beginning = min ( Contracts_range[Start Date])
VAR finish = max ( Contracts_range[End Date] )
VAR duration = max ( Contracts_range[Duration] )
VAR colorVar = 0
RETURN
SWITCH (
TRUE(),
AND ( MAX ( 'Calendar'[Date] ) >= beginning , MIN ( 'Calendar'[Date] ) <= finish ) && duration >0,1
)

Error in last 3 months calculation in PowerBi

calculating the last 3months or average was working in one PBIX file.
Now when I try to use it in another it comes up empty.
Naming seems to be right and the Calendar is a copy from the working PBIX.
What can have gone wrong between the two files, is there something obvious you can see?
Is there something I should check and possibly change?
SLS L3m (USD) =
var months = 3
var sum_period =
CALCULATE(
[Sales (USD)],
DATESINPERIOD('Calendar'[Date] , FIRSTDATE('Calendar'[Date])+1 , -months, MONTH )
)
return
IF( NOT(ISBLANK([Sales (USD)])), sum_period)
INV avg L12m (USD) =
var months = 12
var sum_period =
CALCULATE(
[Inventory (USD)],
DATESINPERIOD('Calendar'[Date] , LASTDATE('Calendar'[Date]) , -months, MONTH )
)
return
IF( NOT( ISBLANK( [Inventory (USD)] )), sum_period/months )
enter image description here

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

QoQ changes by Customer in DAX

I would like to create a DAX formula to calculate the increases and decreases of customers across periods. Following is a sample of the data that I have
Year-Quarter|Customer|Credit-Limit
2019Q2|A|50
2019Q2|B|100
2019Q2|C|100
2019Q2|D|200
2019Q2|E|1000
2019Q2|F|200
2019Q3|A|50
2019Q3|B|200
2019Q3|C|100
2019Q3|D|50
2019Q3|E|500
2019Q3|F|300
I am looking to create a summary by Year-Quarter showing the number of customers that had an increase/decrease/none of their Credit-Limit.
Note that this is just a sample and the actual data is >10M rows. So even though I can create another table, I think from a computation standpoint, a measure would be more useful
Desired Output:
A commentary like the following: "There are 2 customers that have increased credit limit in 2019Q3"
Done so far:
Prev Quarter Credit Limit =
VAR CurrentYearQuarter = MAX(Sheet1[Year-Quarter])
VAR Quarter_Year =
LEFT (CurrentYearQuarter, 4)
VAR Quarter_period =
RIGHT (CurrentYearQuarter, 1 )
RETURN
IF (
Quarter_period = "1",
CALCULATE (
SUM ( Sheet1[Credit Limit] ),
Sheet1[Year-Quarter]
= ( Quarter_Year - 1 )
& "Q"
& ( Quarter_period + 3 )
),
CALCULATE (
SUM ( Sheet1[Credit Limit] ),
Sheet1[Year-Quarter]
= Quarter_Year
& "Q"
& Quarter_period - 1
)
)
Inc/Dec = IF(SUM(Sheet1[Credit Limit]) - [Prev Quarter Credit Limit] > 0,"Inc",
IF(SUM(Sheet1[Credit Limit]) - [Prev Quarter Credit Limit] < 0,"Dec","None"))
Commentary = "There are " &
CALCULATE(DISTINCTCOUNT(Sheet1[Customer]),
FILTER(Sheet1, [Inc/Dec] = "Inc" && Sheet1[Year-Quarter] = "2019Q3"))
Current output:
Commentary: "There are 4"
I am not sure why I am getting 4 as compared to 2 as the number here. Help or guidance would be really appreciated
I've slightly altered the input data for experimental purpose, I've added
2018Q3 | A | 200
2018Q4 | A | 50
2019Q1 | A | 50
I've added a Quarter-Calendar (which is a calculated table using VALUES(Sheet1[Year-Quarter])
Then by adding more columns to this new table, extracting the current year and quarter using LEFT and RIGHT, then calculating the previous quarter, previous year and combining to a Prevoius-Year-Quarter column:
]
Using this Q-Calendar I create a 1:* relationship to the Sheet1 table between [Year-Quarter] and [Year-Quarter], then I create a second inactive 1:* relationship between [Previous-Year-Quarter] and [Year-Quarter] like this:
I then create two measures, one for sum of previous quarter credit an one for current quarter credit:
Current-Quater CL =
var currentQ = MAX('Q-Calendar'[Year-Quarter])
var tempTable =
FILTER(
ALL('Q-Calendar');
'Q-Calendar'[Year-Quarter] = currentQ
)
return
CALCULATE(
SUM('Sheet1'[Credit-Limit ]);
tempTable
)
In the [Previous-Quarter CL] measure I use USERELATIONSHIP to activate the passive relationship I added from the Q-Calendar.
Prev-Quater CL =
var currentQ = MAX('Q-Calendar'[Year-Quarter])
var tempTable =
FILTER(
ALL('Q-Calendar');
'Q-Calendar'[Year-Quarter] = currentQ
)
return
CALCULATE(
SUM('Sheet1'[Credit-Limit ]);
tempTable;
USERELATIONSHIP('Sheet1'[Year-Quarter]; 'Q-Calendar'[Previous-Year-Quarter])
)
Then create the Inc/Dec measure like this:
Increase/Decrease =
var temp = [Current-Quater CL]-[Prev-Quater CL]
return
SWITCH(
TRUE();
temp > 0; "Increase";
temp < 0; "Decrease";
"No change"
)
And finally created a new (actually 3 new) Commentary measures like this:
Commentary_2 = "There are " &
var customers = VALUES(Sheet1[Customer])
return
SUMX(
customers;
CALCULATE(
IF(
[Current-Quater CL]-[Prev-Quater CL] > 0;
1;
0
)
)
)&" customers how have increased their credit"
Using the Year-Quarter column from the Q-calendar as a slicer I get the current status and I can select a previous quarter to see the status at that time:
N.B: The code in my measures can be optimised, I just kept them as detailed as this to make it more understandable.