Convert SQL subquery to Power BI DAX - powerbi

As a newbie of PowerBI, I was so struggled with this problem. I want to create a custom measure, and it can be solved easily with SQL statement which looks like this:
select count(*) from Task
where case_num in(
select case_num from Task
where task_type="DDD") sub
Can somebody help me with this. Thank you!!!!!
The table is roughly like this

You can isolate the case_nums that have a specific task type
VAR tbl = CALCULATETABLE(VALUES('Table'[case_num]),'Table'[task_type] = "DDD")
And then apply that table as a filter:
Measure =
VAR tbl = CALCULATETABLE(VALUES('Table'[case_num]),'Table'[task_type] = "DDD")
RETURN CALCULATE(COUNT('Table'[case_num]), TREATAS(tbl,'Table'[case_num]))
or
Measure =
VAR tbl = CALCULATETABLE(VALUES('Table'[case_num]),'Table'[task_type] = "DDD")
RETURN CALCULATE(COUNT('Table'[case_num]), 'Table'[case_num] IN tbl)

Try this measure
_count =
CALCULATE ( COUNT ( Task[CaseNum] ), FILTER ( Task, Task[task_type] = "DDD" ) )
Edit
If you have a small number of tasks which you want to use in filter, you can use this
Measure =
CALCULATE ( COUNT ( Task[Case] ), FILTER ( Task, Task[task_type] IN {"BB","AA","DDD","CC"}))
If you have a large number of tasks that you don't want to manually type in filter, then create a lookup table first and use that in a measure
TaskLookUpTable = DATATABLE ( --a long list of task; hence taskTbl
"task_type", STRING,
{
{ "BB"},
{ "AA"},
{ "DDD"},
{ "CC"}
}
)
Measure2 = --create the above table first before writing this measure
CALCULATE (
COUNT ( Task[Case] ),
TREATAS ( VALUES ( 'TaskLookUp'[task_type] ), Task[task_type] )
)

Related

DAX Measure: case based on other column text values

I have a big model in PowerBI where there are many different aggregation and grouping based on columns being displayed or not on the final table.
Simplifying: I need to do a conditional statement doing the sum if the value of column 1 is A1 but doing the MAX() if the value of column 1 is A2.
I need to have that information in the same column of the final output.
How would you go for this one?
Thank you very much for your help!
if you have only two values you can do a simple IF like this :
Measure = IF ( SELECTEDVALUE('Table'[Column1]) = "A1", SUM('Table'[Column2]), MAX('Table'[Column2]))
Please try this code:
TblMeasure =
VAR TblSummary =
ADDCOLUMNS (
VALUES ( FactTable[Column1] ),
"Test",
IF (
FactTable[Column1] = "A1",
SUM ( FactTable[Column2] ),
MAX ( FactTable[Column2] )
)
)
RETURN
SUMX ( TblSummary, [Test] )
If we test it on a table visual:

DAX Alternate function for TOP SQL statement

We use this SQL statement for getting top data value in a column
SELECT TOP 1 NOTE_DETAIL
FROM CLAIM_NOTES
WHERE CLAIM_NO = C.CLAIM_NO AND ISNULL(DELETED, 0) = 0
ORDER BY CREATED_DATE DESC
How we can we use this approach in a DAX query if we want last_note from note detail?
For your data:
The DAX measure:
Last Note Detail =
VAR MaxCreatedDate =
CALCULATE (
MAX ( CLAIM_NOTES[CREATED_DATE] ),
ALLEXCEPT ( CLAIM_NOTES, CLAIM_NOTES[CLAIM_NO] )
)
VAR Result =
CALCULATE (
MAX ( CLAIM_NOTES[NOTE_DETAIL] ),
CLAIM_NOTES[CREATED_DATE] = MaxCreatedDate
)
RETURN
Result
Gives a visual of:

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

DAX - Filter Table by Multi Select Filter Condition

Am trying to filter a table with Multi Selection Filter.
DAX: To select the values in the filter selected (Multi)
SelectedEnvironments =
CONCATENATEX (
VALUES ( Environments[ShortEnvName] ),
Environments[ShortEnvName],
", "
)
Result:
But when trying to filter table based on above filter variable doesnt return anything.
DAX:
Aggregated Usage =
VAR __SelectedEnvironments =
CONCATENATEX (
VALUES ( Environments[ShortEnvName] ),
Environments[ShortEnvName],
", "
)
RETURN
CALCULATETABLE (
LastestDBUsage,
LastestDBUsage[Environment] IN { __SelectedEnvironments }
)
If I hard code the values within IN operator it work fine. What am doing wrong? Do I need to format the string for IN operator
DAX (Works fine with Hard Code Values)
Aggregated Usage =
VAR __SelectedEnvironments =
CONCATENATEX (
VALUES ( Environments[ShortEnvName] ),
Environments[ShortEnvName],
", "
)
RETURN
CALCULATETABLE (
LastestDBUsage,
LastestDBUsage[Environment] IN { "DEV", "TST" }
)
Actually, the IN operator works on tables, CONCATENATEX returns a string.
{ __SelectedEnvironments }
returns a table with one row consisting of one column like for instance "DEV, TST"
to make the code work it would be changed to use a table instead, like for instance
Aggregated Usage =
VAR __SelectedEnvironments = VALUES ( Environments[ShortEnvName] )
RETURN
CALCULATETABLE (
LastestDBUsage,
LastestDBUsage[Environment] IN __SelectedEnvironments
)