I have been using Tableau for quite some time but am getting some new experience with Power BI and the use of DAX, and am wondering how I would go about recreating this formula to DAX.
IF [Threshold] <0 and [Threshold] <[Value] then 'Below'
elseif [Threshold] <0 and [Threshold] >[Value] then 'Above'
ELSEIF [Threshold] < [Value] THEN 'Above'
ELSEIF [Threshold] > [Value] THEN "Below"
ELSEIF [Threshold] = [Value] THEN "At"
ELSE "N/A"
END
Looking forward to whoever can assist, thank you!
Assuming you already have a measure called Threshold and a measure called Value:
Indicator = SWITCH(
TRUE(),
[Threshold] <0 && [Threshold] <[Value], "Below",
[Threshold] <0 && [Threshold] >[Value], "Above",
[Threshold] < [Value], "Above",
[Threshold] > [Value], "Below",
[Threshold] = [Value], "At",
"N/A"
)
Related
{ FIXED [ID] : MIN(
IF [Temp] >= 0 AND [Temp] <= 35.2 THEN [Value]
END
)}
How to write the same calculation in DAX
You can try this:
FIXED[ID] =
MINX ( FILTER ( YourTable, [Temp] >= 0 && [Temp] <= 35.2 ), [Value] )
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")
)
in my data set I have a performance issue. I want to speed up my measure and using DAX Studio I made some queries. If anyone has better way to write this measure I will be thankful. "ms" means milliseconds. Both filters are measures.
Original measure:
--original measure - 417 ms
define measure 'BI Booking'[WCX VRS Deals] =
CALCULATE([# Opportunities Booking],
FILTER('Shared Dim Opportunity',([Ent GTMs]="Americas" && [WCX VRS Amount]>=75000) || [Ent GTMs]<>"Americas" && [WCX VRS Amount]>=50000)
)
EVALUATE {[WCX VRS Deals]}
option 1 - using var filter table before the calculation
//option 1 - 358 ms
DEFINE
MEASURE 'BI Booking'[WCX VRS Deals] =
VAR _tbl =
FILTER (
'Shared Dim Opportunity',
( [Ent GTMs] = "Americas"
&& [CXOne VRS Amount] > 75000 )
|| ( [Ent GTMs] <> "Americas"
&& [CXOne VRS Amount] > 50000 )
)
RETURN
CALCULATE (
[# Opportunities Booking],
_tbl
)
EVALUATE
{[WCX VRS Deals]}
option 2 - using 2 tables and union
//option 2 - 505 ms
DEFINE
MEASURE 'BI Booking'[WCX VRS Deals] =
VAR _tbl_1 =
FILTER (
'Shared Dim Opportunity',
( [Ent GTMs] = "Americas" )
&& [CXOne VRS Amount] > 75000
)
VAR _tbl_2 =
FILTER (
'Shared Dim Opportunity',
( NOT ( [Ent GTMs] = "Americas" )
&& [CXOne VRS Amount] > 50000 )
)
VAR _full_tbl =
UNION (
_tbl_1,
_tbl_2
)
RETURN
CALCULATE (
[# Opportunities Booking],
_full_tbl
)
EVALUATE
{[WCX VRS Deals]}
option 3 - using CALCULATETABLE
//option 3 - 379 ms
DEFINE
MEASURE 'BI Booking'[CXOne VRS Deals] =
VAR _tbl =
CALCULATETABLE (
'Shared Dim Opportunity',
FILTER (
'Shared Dim Opportunity',
( [CXOne GTMs] = "Americas"
&& [CXOne VRS Amount] > 75000 )
|| ( [CXOne GTMs] <> "Americas"
&& [CXOne VRS Amount] > 50000 )
)
)
RETURN
CALCULATE (
'BI Booking'[# Opportunities Booking],
_tbl
)
EVALUATE
{[CXOne VRS Deals]}
I have a tableau report and trying to move things into Power BI. I have a scenario where there is a lot of "IF THEN IF" situations as shown in example
IF [SYSTEM] == "ABC"
THEN [Result Flag]
ELSEIF [SYSTEM] == "XYZ" THEN
IF [Option] == 'HIGH'
AND [Activity] == 'Add' THEN
IF [Add Indicator] == "Yes" THEN "GA"
ELSEIF [Name] == "Add 1"
OR [Name] == "Add 2"
THEN "Not Eligible"
ELSE [Result Flag]
END
ELSEIF [Option] == 'Low' THEN
"2019"
ELSE "Not Eligible"
END
ELSE
In Power BI, we have IF and SWITCH functions but they support "IF THEN ELSE" situation and in my scenario, I have lot of "IF THEN IF" situation. I know Power BI DAX IF and SWITCH Function can handle it but when there are lot of values in the condition(Name field in above example) it is little cumbersome. Is there any better way to handle it?
Appreciate all your help!
IF THEN IF... are nested IF statements and ELSEIF can be handled with SWITCH ( TRUE (), ... ).
There are a ton of possible variations on exactly how to implement this. Here's one as an example:
SWITCH (
[SYSTEM],
"ABC", [Result Flag],
"XYZ",
SWITCH (
TRUE (),
[Option] = "High" && [Activity] = "Add",
SWITCH (
TRUE (),
[Add Indicator] = "Yes", "GA",
[Name] IN { "Add 1", "Add 2" }, "Not Eligible",
[Result Flag]
),
[Option] = "Low", "2019",
"Not Eligible"
)
)
suppose i have 4 parameters eg distance , head count , travellers and country score. what i want to do is if distance<=5 && head count <=20 && travellers <=30 && country score <=1 , then we should get value "VERY LOW".
Try with SWITCH function.
https://dax.guide/switch/
SomeMeasure = SWITCH( TRUE(),
Table[distance]<=5 && Table[head count] <=20 && Table[travellers] <=30 && Table[country score] <=1 , "VERY LOW",
Table[distance <= 10 && Table[Head count] <= 40 && Table[[travellers] <= 60 && Table[country score] <=2 , "LOW",
"NORMAL"
)