PowerBi Dax subquery with filter - powerbi

any help on this is greatly appreciated.
I'm looking to count the rows in a table where the PersonID is in a subquery list with a filter condition. In SQL it would look like this.
select count(*)
from tableA
where PersonId in(select distinct PersonId from tableA where CallResult = 1)
tableA has the same PersonId multiple times for each day and I'm looking to count how many times that PersonId is in the table but only if the PersonId has CallResult = 1 in any row within the table. There are other PersonId that don't have CallResult = 1 and I'm not looking to count those.
Maybe I'm overthinking this one but dax isn't my strength
PersonId | CallResult | CallNumber |
AB12 1 3
AB12 0 2
AB12 0 1
CD21 0 2
CD21 0 1
EF32 1 2
EF32 0 1
In this example I would expect the subquery to return AB12 and EF32 and the count to be 5 (3+2) calls

I may have figured it out. Posting here in case it helps anyone.
Count = CALCULATE(
SUM(TableA[Calls]),
CALCULATETABLE(
SUMMARIZE(TableA,TablA[PersonID]),
TableA[CallResult]=1
)
)

Related

dax calculation for costume measure

i have a 2 tables that looks like this
Key |Num Of Treatments| Cost |
1 2 1000
1 2 1500
1 2 2000
2 3 700
3 3 800
4 4 900
key | limit |
1 1
2 1
3 2
4 3
the calculation that i want to do on dax is : (Num Of Treatments-Limit)*cost/Num Of Treatments
Assuming that the key column is unique for the second table (Table2 in dax).
Calculation =
VAR _limit =
LOOKUPVALUE ( Table2[limit], Table2[key], [key] )
RETURN
DIVIDE ( ( [Num Of Treatments] - _limit ) * [cost], [Num Of Treatments] )
This can be easily be achieved after creating one to many relationship between two tables with column Key.
Dax formula :
New Measure = ((SUM(Asset[No Of Treatments])-SUM(Tickets[Limit]))*SUM(Asset[Cost]))/SUM(Asset[No Of Treatments])

How to count cumulatively count occurrences in Power BI?

Let's say I have a table like this:
column1 Column2
A 01/01/2020
B 01/01/2020
C 04/01/2020
A 07/01/2020
B 07/01/2020
A 12/01/2020
C 10/01/2020
What I am trying to do is count how many times a value in column1 has occurred. So I want to be able to end up with this:
column1 Column2 column3
A 01/01/2020 1
B 01/01/2020 1
C 04/01/2020 1
A 07/01/2020 2
B 07/01/2020 2
A 12/01/2020 3
C 10/01/2020 2
I found DAX to count how many times in total a value occurs but I can't seem to find how to count them cumulatively. Thanks in advance.
The logic is the same as with a cumulative sum, just with COUNT instead:
column3 =
CALCULATE (
COUNT ( Table1[column1] ),
ALLEXCEPT ( Table1, Table1[column1] ),
Table1[Column2] <= EARLIER ( Table1[Column2] )
)
See also:
Cumulative Total DAX Pattern
Cumulative sum on different columns grouped by date and filtered differently

How to get counts from related tables filtered by dates in both tables

I have two tables A and B as shown below. The AccountID in A has a relationship with the AccountID in B. 
A
AccountID CmpName AccFlag SysStartTime
A1 Test1 1 1/1/2020
A2 Test2 0 1/2/2020
A3 Test3 1 1/2/2020
B
ContactId AccountID ConFlag SysStartTime
C1 A1 1 1/1/2020
C2 A1 1 1/1/2020
C3 A1 0 1/1/2020
C4 A2 1 1/2/2020
I want to get the count of records in A that have 3 related records in B. I did this using a calculated column with the DAX:
getcount = COUNTROWS(RELATEDTABLE(B))
And then created another calculated column to flag the one's with getcount = 3.
But the problem is that I want to check the count of records in A that have 3 related records in B at a given time. So I need to filter by the sysStartTime's in both the tables. For example, I want to get the count of records in A that have 3 related records in B as of 1/1/2010. So the result should be 1. Please advise on how I can do this using a Measure instead of a calculated column.
You should be able to do something like this:
SUMX ( A, IF ( COUNTROWS ( RELATEDTABLE ( B ) ) = 3, 1 ) )
Assuming you have a date table with a relationship to both A and B, a slicer on that date table will apply to the measure and you can set whatever dates you want.

Choosing the row with the maximum character length in sas

I have the following dataset:
dataseta:
No. Name1 Name2 Sales Inv Comp
1 TC Tribal Council Inc 100 100 0
2. TC Tribal Council Limited INC 20 25 65
desired output:
datasetb:
No. Name1 Name2 Sales Inv Comp
1 TC Tribal Council Limited Inc 120 125 0
Basically, I need to choose the row with the maximum length of characters for the column name2.
I tried the following, but it didn't work
proc sql;
create table datasetb as select no,name1,name2,sum(sales),sum(inv),min(comp) from dataseta group by 1,2,3 having length(name2)=max(length(name2));quit;
If I do the following code, it only partially resolves it, and I get duplicate rows
proc sql;
create table datasetb as select no,name1,max(length(name2)),sum(sales),sum(inv),min(comp) from dataseta group by 1,2 having length(name2)=max(length(name2));quit;
You appear to be joining the results of two separate aggregate computations.
Presuming:
no is unique so as to allow a tie breaker criteria and the first (per no) longest name2 is to be joined with the cost, inv, comp totals over name1.
The query will have lots going on...
1st longest name2 within name1, nested subqueries are needed to:
Determine the longest name2, then
Select first one, according to no, if more than one.
totals over name1
The totals will be a sub-query that is joined to, for delivering the desired result set.
Example (SQL)
data have;
length no 8 name1 $6 name2 $35 sales inv comp 8;
input
no name1& name2& sales inv comp; datalines;
1 TC Tribal Council Inc 100 100 0 * name1=TC group
2 TC Tribal Council Limited INC 20 25 65
3 TC Tribal council co 0 0 0
4 TC The Tribal council Assoctn 10 10 10
7 LS Longshore association 10 10 0 * name=LS group
8 LS The Longshore Group, LLC 2 4 8
9 LS The Longshore Group, llc 15 15 6
run;
proc sql;
create table want as
select
first_longest_name2.no,
first_longest_name2.name1,
first_longest_name2.name2,
name1_totals.sales,
name1_totals.inv,
name1_totals.comp
FROM
(
select
no, name1, name2
from
( select
no, name1, name2
from have
group by name1
having length(name2) = max(length(name2))
) longest_name2s
group by name1
having no = min(no)
) as
first_longest_name2
LEFT JOIN
(
select
name1,
sum(sales) as sales,
sum(inv) as inv,
sum(comp) as comp
from
have
group by name1
) as
name1_totals
ON
first_longest_name2.name1 = name1_totals.name1
;
quit;
Example (DATA Step)
Processing the data in a serial manner, when name1 groups are contiguous rows, can be accomplished using a DOW loop technique -- that is a loop with a SET statement within it.
data want2;
do until (last.name1);
set have;
by name1 notsorted;
if length(name2) > longest then do;
longest = length(name2);
no_at_longest = no;
name2_at_longest = name2;
end;
sales_sum = sum(sales_sum,sales);
inv_sum = sum(inv_sum,inv);
comp_sum = sum(comp_sum,comp);
end;
drop name2 no sales inv comp longest;
rename
no_at_longest = no
name2_at_longest = name2
sales_sum = sales
inv_sum = inv
comp_sum = comp
;
run;

PowerBI Dax Query - Match a Value and get its Maximum Value

I want to match the value in Column1 of one table with another and get the maximum value of column2.
For Example I have two tables,
Table 1
Col1 Col2
AA 17
AA 20
AB 10
AB 21
Table 2
Col1 Col2
AA ?
AB ?
I want my output to look like this,
Col1 Col2
AA 20
AB 21
I have tried,
Col2 = Max(Table1[col2])
but it didn't help. Thanks. Please share your thoughts.
You can use the following DAX:
Col2 =
CALCULATE(
MAX(Table1[Col2]),
FILTER(
Table1,
Table1[Col1] = Table2[Col1]
)
)
Result: