I have a table Emails as below
Id,
Project,
Subject,
ReceivedDate
This table will hold emails . I am trying to add a calculate column Responded Status which should get populated on certain logic.
I have the SQL query which works well for me.
select E.Id,E.Project,E.Subject,E.ReceivedDate ,
case when (select count(E1.Id) from Emails E1
where E1.Project = E.Project and E1.ReceivedDate > E.ReceivedDate
and (E1.Subject like 'RE:%' or E1.Subject like 'FW:%') and E1.Subject like '%' + E.Subject + '%') ) > 0
then 'Yes'
else 'No' end as RespondedStatus
from Emails E
I need help in converting the SQL to DAX. I will use the DAX to populate the calculated col "RespondedStatus"
Appreciate suggestions/ ideas on this!!
ADDCOLUMNS(
SUMMARIZE(
Emails
,Id
,Project
,Subject
,ReceivedDate
)
,"RespondedStatus", VAR currentProj = [Project]
VAR currentReceivedDate = [ReceivedDate]
VAR currentSubject = [Subject]
VAR result =
COUNTROWS(
FILTER(
Emails
,[Project]=currentProj
&& [ReceivedDate]>currentReceivedDate
&& (
(LEFT( [Subject], 3 ) = "RE:"
|| LEFT( [Subject], 3 ) = "FW:"
)
&& CONTAINSSTRING(currentSubject, [Subject])
)
)
)
RETURN IF(result >0,"Yes","No")
)
Related
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
)
I have a table of the customers check out funnel through an app and a table of total sales. I want to find the number of sales that can be attributed to the app. I think the easiest way to do this is to concatenate the name and barcode in both of these tables. Then I can see if the values column, NameVIN, for the sales table appear in NameVIN for the checkout funnel table.
For obvious reasons I will not be sharing data for this question.
NameVIN =
if(
ConsumerFunnelTime[CustomerFirstName] <> BLANK() && ConsumerFunnelTime[CustomerLastName] <> BLANK(), ConsumerFunnelTime[CustomerFirstName] & ConsumerFunnelTime[CustomerLastName] & ConsumerFunnelTime[VIN],BLANK())
NameVIN =
if(
'Sales'[c_Customer1FirstName] <> BLANK() && 'Sales'[c_Customer1LastName] <> BLANK(), 'Sales'[c_Customer1FirstName] || 'Sales'[c_Customer1LastName] & 'Sales'[v_VIN],BLANK())
This is what I tried to see of there is a matching value for NameVIN in both Sales and Consumer Funnel
VAR x = VALUES(ConsumerFunnelTime[NameVIN])
RETURN
CALCULATE(
COUNTROWS(ConsumerFunnelTime),
ALL(),
TREATAS( x, 'Sales'[NameVIN])
) + 0
I also tried this, but it is giving me an error for the syntax of CALCULATE.
DarwinSales =
VAR UsersNameVIN = ConsumerFunnelTime[NameVIN]
CALCULATE(
COUNT('Sales'[NameVIN]),
'Sales'[NameVIN] = UsersNameVIN
)
WasSale =
LOOKUPVALUE('Sales'[NameVIN], 'Sales'[NameVIN], ConsumerFunnelTime[NameVIN])
DarwinSales =
DISTINCTCOUNT(ConsumerFunnelTime[WasSale])
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.
I have to find the count of invoices that never went to a successful state at all. I tried checking multiple sites but not able to understand and recreate the query. Kindly help me. Thanks in advance.
In the above sample invoice 14 never went to success state. So the InvoiceCount = 1 and UniqInvoice count is 2.
I want the uniqInvoice count from the above table.
Create a Custom Column and a Measure as below to achieve your required output-
Custom Column-
success_check =
var current_row_invoice = your_table_name[invoice]
var count_success =
CALCULATE(
COUNT(your_table_name[invoice]),
FILTER(
ALL(your_table_name),
your_table_name[invoice] = current_row_invoice
&& your_table_name[status] = "S"
)
)
RETURN IF(count_success>=1, 1,0)
Measure-
unique_count =
CALCULATE(
DISTINCTCOUNT(your_table_name[uniqinvoice]),
FILTER(
your_table_name,
your_table_name[success_check] = 0
)
)
Here is the output-
I finally found a way to frame the Dax Query, Hoping it will help someone.
FC = CALCULATE (
DISTINCTCOUNT ( InvoiceTable[UniqInvoice] ),
FILTER (
InvoiceTable,
NOT (
InvoiceTable[Invoice]
IN SELECTCOLUMNS (
FILTER (
SUMMARIZE ( InvoiceTable, InvoiceTable[Invoice], InvoiceTable[Status] ),
InvoiceTable[Status] = "S"
),
"InvoiceNo", InvoiceTable[Invoice]
)
)
)
)
I have a problem converting below t-sql query into DAX.
Overview - There are two sample tables - Table1 and Table2 with below schema
Table1 (ID varchar(20),Name varchar(30))
Table2 (CapID varchar(20),CAPName varchar(30), CapID_Final varchar(20))
Please note : There exists one to many relationship between above tables : [ID] in Table2 with [CapID] in Table1
I am trying to derive CapID_Final column in table2 based on conditions as per my t-SQL query in below which works perfectly fine -
SELECT CASE
WHEN [CapID] like 'CA%' and [CAPName]='x12345-Sample'
and [CapID] not in(select [ID] from Table1 where Name='x12345-Sample')
THEN 'Undefined_Cap_1'
WHEN [CapID] like 'CA%' and [CAPName]='z12345-Sample'
and [CapID] not in(select [ID] from Table1 where Name='z12345-Sample')
THEN 'Undefined_Cap_2'
WHEN [CapID] like 'CA%' and [CAPName]='a123-Sample'
and [CapID] not in(select [ID] from Table1 where Name='a123-Sample')
THEN 'Undefined'
ELSE [CapID]
END AS [CapID_Final] from Table2
However, I want the same derivation for CapID_Final column in Power BI in a calculated column using DAX.
So far, I have tried below code - but it returns "Undefined" for even matched conditions -
CapID_Final =
IF(LEFT(Table2[CapID],2)="CA" && Table2[CAPName]="z12345-Sample" &&
NOT
(COUNTROWS (
FILTER (
Table1,CONTAINS(Table1,Table1[ID],Table2[CapID])
)
) > 0),"Undefined_Cap_1","Undefined"
)
I am not familiar with DAX, however I tried and couldn't figure it out.
Could you please let me know how to convert my sql query to equivalent DAX in Power BI?
A SWITCH is basically the equivalent of a CASE clause here:
CapID_Final =
SWITCH (
TRUE (),
LEFT ( Table2[CapID], 2 ) = "CA"
&& Table2[CAPName] = "x12345-Sample"
&& NOT (
Table2[CapID]
IN CALCULATETABLE ( VALUES ( Table1[ID] ), Table1[Name] = "x12345-Sample" )
), "Undefined_Cap_1",
LEFT ( Table2[CapID], 2 ) = "CA"
&& Table2[CAPName] = "z12345-Sample"
&& NOT (
Table2[CapID]
IN CALCULATETABLE ( VALUES ( Table1[ID] ), Table1[Name] = "z12345-Sample" )
), "Undefined_Cap_2",
LEFT ( Table2[CapID], 2 ) = "CA"
&& Table2[CAPName] = "a12345-Sample"
&& NOT (
Table2[CapID]
IN CALCULATETABLE ( VALUES ( Table1[ID] ), Table1[Name] = "a12345-Sample" )
), "Undefined",
Table1[CapID]
)
You might even be able to refactor it a bit to be more code efficient. Assuming I didn't make any logic mistakes:
CapID_Final =
VAR IDs =
CALCULATETABLE ( VALUES ( Table1[ID] ), Table1[Name] = Table2[CAPName] )
RETURN
IF (
LEFT ( Table2[CapID], 2 ) = "CA"
&& NOT ( Table2[CapID] IN IDs ),
SWITCH (
Table2[CAPName],
"x12345-Sample", "Undefined_Cap_1",
"z12345-Sample", "Undefined_Cap_2",
"a12345-Sample", "Undefined"
),
Table1[CapID]
)
As a best-practice never use calculated column. In fact, if extensively used they slow down your model refresh and heavily increase your model weight (because they are not compressed). Instead, calculate it in your back-end database or using M Query.
Having said this, the solution to your question is very simple using a SWITCH function:
SWITCH ( <Expression>, <Value>, <Result> [, <Value>, <Result> [, … ] ] [, <Else>] )
In your case would be as follow:
CapIDFinal:=
SWITCH(TRUE(),
AND(CONDITION_1_1, CONDITION_1_2), "Value if condition 1 is true",
AND(CONDITION_2_1, CONDITION_2_2), "Value if condition 2 is true",
"Value if none of above conditions is true
)