Match and Get the Value from another table (Vlookup) - powerbi

I have 2 tables connected to each other through col A. I want to match the column C with Col A and get the value of Col B.
For Example,
Table 1
ColA ColB Colc
a a1 b
a b1 c
c c1 a
Table2
ColA ColB
a a1
b b1
c c1
Now, I have already created relationships between Table2 and Table1 for my other calculations connecting both the tables with the colA.
Now, I am trying to match ColC from Table1 with ColA of Table2 and return the value of ColB from Table2 as MatchedOutput.
Expected output
Table1
ColA ColB Colc MatchedOutput
a a1 b b1
a b1 c c1
c c1 a a1

The DAX function for this is LOOKUPVALUE.
MatchedOutput = LOOKUPVALUE(Table2[ColB],Table2[ColA],Table1[ColC])
This looks for the value in Table2[ColB] where Table2[ColA] matches Table1[ColC].

Related

PowerBI variable selection from union tables

I have two tables, table1 & table2. I created 'Union' of these two tables in PowerBI.
table1
id city fruit table
a1 NY apple table1
a2 NY apple table1
a3 NY pear table1
a4 MI pear table1
a5 MI apple table1
a6 MI strawberry table1
a7 TO strawberry table1
a8 TO strawberry table1
a9 TO kiwi table1
table2
id city fruit table
a11 NY rockmelon table2
a12 NY rockmelon table2
a13 BJ pear table2
a14 BJ pear table2
a15 BJ apple table2
a16 LA blueberry table2
a17 LA blueberry table2
a18 PA blueberry table2
a19 PA kiwi table2
I would like to create a filter "filter1", which takes all rows if fulfilling these criteria:
"city" in table1 is "MI", or "fruit" in table2 is "blueberry".
If the criteria is met, call it "Awesome" in the dropdown of "filter1".
Can you please see if the following measure is in the right direction, and how to proceed? Thank you.
filter1 =
VAR
table1 = FILTER('Union', 'Union'[table]="table1" && 'Union'[city]="MI")
VAR
table2 = FILTER('Union', 'Union'[table]="table2" && 'Union'[fruit]="blueberry")
Awesome_test =
var filter1 =
FILTER(union, union[city]= "MI" && union[table]="table1")
var filter2 =
FILTER(union, union[fruit]= "blueberry" && union[table]="table2")
var filter3 = UNION(filter1, filter2)
RETURN
CALCULATE(MAX(union[id]), filter3)
// then pull the new measure into filter, select all that contains "a", then only those that fulfill the requirements are retained.

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.

SAS : Map a column from 1 table to any of the multiple columns in another table

I have a table1 that contains 4 different kind of ids
Data table1;
Input id1 $ id2 $ id3 $ final_id $;
Datalines;
1 a a1 p
2 b b2 q
- c c2 r
3 d - s
4 - d4 t
A table2 contains any of the ids from id1, id2 or id3 of table1:
Data table1;
Input id $ col1 $ col2 $;
Datalines;
1 gsh ywu
b hsjs kall
c2 jsjs ywe
3 sja weei
d4 ase uwh
I want to left join table1 on table2 such that I get a new column in table2 giving me final_id from table1.
How do i go about this problem?
Please help.
Thank you.
You can do it using SQL:
proc SQL noprint;
create table merged as
select b.final_id, a.*
from table2 as a left join table1 as b
on (a.id eq b.id1 or a.id eq b.id2 or a.id eq b.id3)
;
quit;

Selecting columns whose name matches a regular expression in PostgreSQL

How can I select only those columns whose name matches a regular expression in PostgreSQL?
For example, how do I select only the columns whose name begins with 'A' in the following table, without explicitly enumerating them in the select list?
id A1 A2 A3 A4 A5 B
1 a b c d e f
2 g h i j k l
You will need to write a dynamic sql('select '||colname||' from (yourtable)') to accomplish this and dynamic sql should have supplied column names from the following sql:
SELECT column_name
FROM information_schema.columns
WHERE table_name = '<your table>'
AND column_name LIKE '<beginning of column name>%';

SAS data manipulation for 2 table based on one column id

I have 2 tables as followings:
Table 1
data table1;
input id $ value;
datalines;
A 1
A 2
B 1
B 2
C 1
D 1
;
Table 2
data table2;
input id $ value;
datalines;
A 1
B 2
C 1
D 1
E 1
;
As you may observed that the unique id for table 1 is A, B, C, D.
I would like to delete observations those id in table2 do not appear in table1.
Therefore last observation of table2 should be deleted as E not in {A, B, C, D}
Desired output:
A 1
B 2
C 1
D 1
You can do this with proc sql:
proc sql;
delete from table2
where not exists (select 1 from table1 where table1.id = table2.id);