PowerBI DAX Measure to return 1 for all the items in category if any item in category has result 1 - powerbi

I need measure (or maybe calculated column) which will return 1 in the column called Final when any of the item Table2[name] per category (Table1[Category]) is 1. So even if there is just one item in the category that has result 1 it returns 1 for all of them in the same category and 0 when all the items in the category have result 0. Hope the example below is clear.
Table1[Category]
Table2[name]
Result
Final
A
A:1
0
1
A
A:2
1
1
A
A:3
0
1
B
B:1
0
0
B
B:2
0
0
C
C:1
1
1
C
C:2
0
1

Using variables you can access the Category to filter on the table and obtain the MAX result.
Calculation: Calculated Column
Final =
VAR CurrentCat = [Table1[Category]]]
VAR MaxResult =
MAXX ( FILTER ( 'Table', [Table1[Category]]] = CurrentCat ), [Result] )
RETURN
IF ( MaxResult = 1, 1, 0 )
Output
Table1[Category]
Table2[name]
Result
Final
A
A:1
0
1
A
A:2
1
1
A
A:3
0
1
B
B:1
0
0
B
B:2
0
0
C
C:1
1
1
C
C:2
0
1

Related

Sum of Value by Category and Status Power BI

I am Attempting to get a sum of ppl that is based on the status of the offer
here is an example of the data
Acc
PPL
Offer comp
ABCDE
1
1
ABCDE
1
0
ABCDE
1
0
CDEFG
2
0
DEFGH
1
1
DEFGH
1
0
DEFGH
1
0
DEFGH
1
1
EFGHI
2
1
EFGHI
2
1
EFGHI
2
0
And this is what I am wanting to achieve as a column either in Power query or Dax
Account
PPL
Offer Comp
total
ABCDE
1
1
1
ABCDE
1
0
1
ABCDE
1
0
1
ABCDE
1
0
1
BCDEF
1
0
1
BCDEF
1
0
1
CDEFG
2
0
2
DEFGH
1
1
2
DEFGH
1
0
2
DEFGH
1
0
2
DEFGH
1
1
2
EFGHI
2
1
4
EFGHI
2
1
4
EFGHI
2
0
4
Basically, we are looking for a count of ppl where if the offer is not completed, we count the ppl, if the offer is accepted, we count the ppl for each acceptance.
Hope this makes sense I would then use this column to create measures as they want ppl count to be unique unless they accepted multiple offers on same account.
I tried to create with selected values and all except but I just couldn't get it to work.
I am probably completely overthinking this.
Very cryptic, and I must admit the sample data and your description is not great, but I think you are after something like this:
Total =
VAR _offer =
SUMX (
ALLEXCEPT (
'Table' ,
'Table'[Account]
) ,
'Table'[Offer Comp] * 'Table'[PPL]
)
RETURN
IF (
_offer > 0 ,
_offer ,
CALCULATE (
MAX ( 'Table'[PPL] ) ,
ALLEXCEPT (
'Table' ,
'Table'[Account]
)
)
)
I have figured it out with help from Marcus
thank you. I realised also need to add a date grouping.
here is the code
Total =
VAR _offer =
CALCULATE(SUM(Table[Offer Comp]),
ALLEXCEPT(Table, Table [Account], Table[Date]))
VAR Pax =
CALCULATE(MAX(Table[PPL]),
ALLEXCEPT(Table, Table[Account]))
RETURN
IF(_offer = 0,
Pax *1,
pax*_offer
)

DAX measure of two layers of intersection and a validation

I need to find a measure with the following logic:
Firstly, a link is dictated by Table 2 (LinkedID column) back to Table 1 (UniqueID column) = LinkedID -> UniqueID
For example: ID 1/UniqueID AA is a S_Req and has links with ID 9/UniqueID II because ID 1 is found in Table 2 with LinkedID = II which back in Table 1 will be ID 9/Unique II and this is an E_Req, next link is to ID 3/UniqueID CC(M_Req) and from these links(S_Req linked with E_Req or M_Req) also with the help of Table2 will determine the links with an Tst, so from ID 9/UniqueID II will have link to ID 6/UniqueID FF(Tst - Passed) and ID 2/UniqueID BB (Tst - Failed) and ID 3/UniqueID II will have a links to ID 10/UniqueID JJ (Tst - Failed) and ID 8/Unique HH(Tst - Not Done)
I need a measure that calculates the total number of S_Req having at-least one TST with the TestResult "passed" and none "failed" via M_req or E_Req links = 1
= 0 (explanation: as explian above, there's an TST with "Failed" result, so the other doesn't matter) +
0 (explication: ID 5/UniqueID EE -> ID 3/UniqueID CC(M_Req) -> ID 10/UniqueID JJ (Tst - Failed) the other link doesn't matter) +
1 (ID 7/UniqueID GG(S_Req) -> ID 9/Unique II(E_Req) -> ID6/UniqueID FF (Tst - Passed) -> ID 11/Unique KK(M_Req) -> ID8/UniqueID HH (Tst - Not Done) )
Table1: ID UniqueID TestResult S_Req M_Req E_Req Tst
Table1 :
ID
UniqueID
TestResult
S_Req
M_Req
E_Req
TST
1
AA
1
0
0
0
2
BB
Failed
0
0
0
1
3
CC
0
1
0
0
4
DD
Passed
0
0
0
1
5
EE
1
0
0
0
6
FF
Passed
0
0
0
1
7
GG
1
0
0
0
8
HH
Not Done
0
0
0
1
9
II
0
0
1
0
10
JJ
Failed
0
0
0
1
11
KK
0
1
0
0
Table 2:
ID
LinkedID
1
II
1
CC
3
JJ
3
HH
5
CC
7
II
7
KK
9
FF
9
BB
11
HH
That means will be a two layer intersection as i explain earlier: For now all i got is a total number of S_Req that have at least a link with M_Req or E_Req and that's only one layer of intersection:
Measure = VAR _tab1 = CALCULATETABLE ( VALUES ( 'Table 1'[ID] ), FILTER ( 'Table 1', 'Table 1'[S_Req] = 1 ) ) VAR _tabuid = CALCULATETABLE ( VALUES ( 'Table 1'[UniqueID] ), FILTER ( 'Table 1', 'Table 1'[S_Req] = 1 || 'Table 1'[E_Req] = 1 ) ) VAR _tab2 = CALCULATETABLE ( VALUES ( 'Table 2'[ID] ), FILTER ( 'Table 2', 'Table 2'[LinkedID] IN _tabuid ) ) VAR _tab3 = INTERSECT ( _tab2, _tab1 ) RETURN COUNTX ( _tab3, [ID] )

inputing a pandas dataframe matching two columns from two dataframes

I have a dataframe C and another dataframe S. I want to change the values in one of the column of C if two columns in C and S have same values.
Please consider the example given below,
C.head(3)
id1 id2 title val
0 1 0 'abc' 0
1 2 0 'bcd' 0
2 3 0 'efg' 0
S.head(3)
id1 id2
0 1 1
1 3 0
I want to assign the value of 1 to the column 'val' in C corresponding only to the rows where C.id1 = S.id1 and C.id2 = S.id2
The combination of (C.id1, C.id2) and (S.id1, S.id2) is unique in respective tables
In the above case, I want the result as
C.head(3)
id1 id2 title val
0 1 0 'abc' 0
1 2 0 'bcd' 0
2 3 0 'efg' 1
as only in the third row of C, it matches with one of the rows of S for the columns id1 and id2.
I think need merge with left join and parameter indicator, last convert boolen mask to 0 and 1:
#if same columns for join in both df parameter on is possible omit
df = C.merge(S, indicator=True, how='left')
#if multiple same columns in both df
#df = C.merge(S, indicator=True, how='left', on=['id1', 'id2'])
df['val'] = (df['_merge'] == 'both').astype(int)
df = df.drop('_merge', axis=1)
print (df)
id1 id2 val
0 1 0 0
1 2 0 0
2 3 0 1
Solutiion working nice with new data:
df = C.merge(S, indicator=True, how='left')
#if multiple same columns in both df
#df = C.merge(S, indicator=True, how='left', on=['id1', 'id2'])
df['val'] = (df['_merge'] == 'both').astype(int)
df = df.drop('_merge', axis=1)
print (df)
id1 id2 title val
0 1 0 abc 0
1 2 0 bcd 0
2 3 0 efg 1

Transform categorical column into dummy columns using Power Query M

Using Power Query "M" language, how would you transform a categorical column containing discrete values into multiple "dummy" columns? I come from the Python world and there are several ways to do this but one way would be below:
>>> import pandas as pd
>>> dataset = pd.DataFrame(list('ABCDACDEAABADDA'),
columns=['my_col'])
>>> dataset
my_col
0 A
1 B
2 C
3 D
4 A
5 C
6 D
7 E
8 A
9 A
10 B
11 A
12 D
13 D
14 A
>>> pd.get_dummies(dataset)
my_col_A my_col_B my_col_C my_col_D my_col_E
0 1 0 0 0 0
1 0 1 0 0 0
2 0 0 1 0 0
3 0 0 0 1 0
4 1 0 0 0 0
5 0 0 1 0 0
6 0 0 0 1 0
7 0 0 0 0 1
8 1 0 0 0 0
9 1 0 0 0 0
10 0 1 0 0 0
11 1 0 0 0 0
12 0 0 0 1 0
13 0 0 0 1 0
14 1 0 0 0 0
Interesting question. Here's an easy, scalable method I've found:
Create a custom column of all ones (Add Column > Custom Column > Formula = 1).
Add an index column (Add Column > Index Column).
Pivot on the custom column (select my_col > Transform > Pivot Column).
Replace null values with 0 (select all columns > Transform > Replace Values).
Here's what the M code looks like for this process:
#"Added Custom" = Table.AddColumn(#"Previous Step", "Custom", each 1),
#"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", 0, 1),
#"Pivoted Column" = Table.Pivot(#"Added Index", List.Distinct(#"Added Index"[my_col]), "my_col", "Custom"),
#"Replaced Value" = Table.ReplaceValue(#"Pivoted Column",null,0,Replacer.ReplaceValue,Table.ColumnNames(#"Pivoted Column"))
Once you've completed the above, you can remove the index column if desired.

SAS: do loop help: variable =1 from start of by group until marker =1

In SAS, I have data that is sorted by time. For the first n minutes, a marker variable is marked 0 then changes to 1. This occurs within a 'by' variable. I want to create a new variable that =1 for each minute from the start of the 'by' group observations until the marker variable gets to 1. I have tried dozens of do loop combinations and have been unsuccessful. Any help would be appreciated!
From a data set like this:
by_var marker
A 0
A 0
A 1
B 0
B 0
B 1
You can get a data set that looks like this (if each observation is a minute):
by_var marker minute
A 0 1
A 0 2
A 1 3
B 0 1
B 0 2
B 1 3
From your data set by sorting by by_var and using a simple data step:
data my_data2;
set my_data;
by by_var;
if first.by_var then minute = 0;
minute+1;
output;
run;
Or did you need to change something like this (with a time variable):
by_var marker time
A 0 12:34:01
A 0 12:34:59
A 0 12:35:01
A 0 12:36:12
A 1 12:36:50
B 0 12:34:01
B 0 12:34:09
B 0 12:34:59
B 0 12:36:12
B 1 12:37:50
To this:
by_var marker time time2 minutes Cumsum_minutes
A 0 12:34:01 . 0 0
A 0 12:34:59 12:34:01 0 0
A 0 12:35:01 12:34:59 1 1
A 0 12:36:12 12:35:01 1 2
A 1 12:36:50 12:36:12 0 2
B 0 12:34:01 12:36:50 0 0
B 0 12:34:09 12:34:01 0 0
B 0 12:34:59 12:34:09 0 0
B 0 12:36:12 12:34:59 2 2
B 1 12:37:50 12:36:12 1 3
CODE:
data my_data;
input by_var $ marker time time.;
format time time.;
cards;
A 0 12:34:01
A 0 12:34:59
A 0 12:35:01
A 0 12:36:12
A 1 12:36:50
B 0 12:34:01
B 0 12:34:09
B 0 12:34:59
B 0 12:36:12
B 1 12:37:50
;
run;
proc sort data=my_data;by by_var;run;
data final;
set my_data;
format time2 time.;
by by_var;
time2=lag(time);
if first.by_var then do;
minutes = 0;
Cumsum_minutes = 0;
end;
else minutes=intck("minutes",time2,time);
Cumsum_minutes+minutes;
run;