Dependency error while using LOOKUPVALUE() in Power BI with two foreign keys - foreign-keys

I am stuck with a very simple (yet frustrating problem). I have table1 with the following columns :
id1 id2 title1 title2
------------------------------------------------
1 2 A great headline ERROR
1 3 A great headline ERROR
title1 & title2 are column populated from table2 with the LOOKUPVALUE() function. table2 looks like this :
id title
--------------------------
1 A great headline
2 Another great headline
3 A third great headline
I've put a many-to-one relationship between table1[id1] and table2[id] and managed to get values for table1[title1] using this formula :
LOOKUPVALUE(table2[title]; table2[id]; table1[id1])
However, when I try to populate table1[title2], I get a circular dependency error.
I tried both those functions :
LOOKUPVALUE(table2[title]; table2[id]; table1[id2]) -> Look id2 into id and return title
LOOKUPVALUE(table1[title1]; table1[id1]; table1[id2]) -> Look id2 into id1 and return title1
Could someone suggest a work-around for this problem?
Many thanks.

I duplicated your example with no errors. Check the direction of your one to many relationship.
I setup two relationships. Note that only one can be "Active".

Related

Power BI - Need a slicer to look at multiple columns of Data

I am learning PowerBI as i go along and have been able to solve most issues faced by a quick google. Unfortunately this problem has baffled me.
We have thousands of lines of data which include a "Home Country" column & "Away Country" column.
What we need our slicer to do is to is to pick up for example Australia in both of these columns and display the results.
I currently have a table created using the below:
slicercountrytable = distinct(
Union(
Values('All Data'[Home Country]),
Values('All Data'[Away Country])))'''
and then a measure:
Measure =
if(
Min('All Data'[Home Country]) in values (slicercountrytable[Country])
|| Min('All Data'[Away Country]) in values (slicercountrytable[Country]),
1,
Blank()
)
And have also tried the below measure:
Measure 3 = VAR
Searchvalue=search(SELECTEDVALUE(slicercountrytable[Country]),SELECTEDVALUE('All Data'[Combined Country]),,Blank())
Return
If(Searchvalue > 0,"Found")
I need the slicer to control the entire sheet however the above are doing nothing.
Thanks
In that case, instead of building some complicated DAX I suggest to work the model.
Let's say your current table looks like that:
id
home country
away country
1
A
B
2
B
C
You may want to deal with that in another table by unpivoting home_country and away_country in PowerQuery.
In order to have a country table like that:
id
country
attribute
1
A
home
1
B
away
2
B
home
2
C
away
Then you link this new table to your existing one on id and filter/slice in it.
I reproduced your example and feel that it is already showing the desired behavior. I think the problem you might be running into is that you will now need to add your 'measure' filter to each and every visual individually. It cannot be added to 'Page' or 'All Pages' filter areas. This is because of the way a measure's calculation varies across context and cannot be avoided.
DAX
Table 2 = distinct(union(all('Table'[Away]), all('Table'[Home])))
Measure =
if(
MAX('Table'[Away]) in VALUES('Table 2'[SelectedCountries])
|| MAX('Table'[Home]) in VALUES('Table 2'[SelectedCountries]) ,
1,
blank()
)

Where not equals not filtering anything out

I'm trying to filter out certain values from a table by using WHERE <> in PROC SQL. The query runs without errors, but doesn't actually filter anything out.
ID Category Sub_Category
1 Food Vegetables
2 Food Chicken
3 Appliance Mixer
The code I have looks like this:
PROC SQL;
CREATE TABLE APPLIANCE AS
SELECT * FROM GENERAL_TABLE
WHERE Sub_Category <> "Chicken"
OR Sub_Category <> "Vegetables";
RUN;
I know I can switch the where statement to be WHERE = "Mixer" but I'd like to understand why WHERE <> isn't filtering anything out.
You need AND not OR.
OR means if at least one condition is true, then it will display that row. Since one of those conditions will always be true, it will display all rows.
Rather than the <> and OR, use IN or NOT IN
PROC SQL;
CREATE TABLE APPLIANCE AS
SELECT * FROM GENERAL_TABLE
WHERE Sub_Category not in ("Chicken", "Vegetables");
RUN;

Count unique patients and overall observation using PROC SQL

Working in SAS but using some SQL code to count the number of unique patients but also the total number of observations for a set of indicators. Each record has a patient identifier, the facility where the patient is, and a group of binary indicators (0,1) for each bed section (the particular place in the hospital where the patient is). For each patient record, only 1 bed section can have a value of '1'. Overall, patients can have multiple observations in a bed section or in other bed sections, i.e. patients can be hospitalized > 1. The idea is to roll this data set up by facility and count the total # of admissions for each bed section but also the total people for each bed section. The people count will always be <= to the observation count. Counting people was just added to my to-do list and to this point I was only summing up observations for each bed section using the code below:
proc sql;
create table fac_bedsect as
select facility,
sum(bedsect_alc) as bedsect_alc,
sum(bedsect_blind) as bedsect_blind,
sum(bedsect_gen) as bedsect_gen
from bedsect_type
group by facility;
quit;
Is there a way I can incorporate into this code the # of unique people for each bed section? Thanks.
With no knowledge of the source table(s) it is impossible to answer precisely, but the syntax for counting distinct values is as seen below. You will need to use the correct column name where I have used "patient_id":
SELECT
facility
, COUNT(DISTINCT patient_id) AS patient_count
, SUM(bedsect_alc) AS bedsect_alc
, SUM(bedsect_blind) AS bedsect_blind
, SUM(bedsect_gen) AS bedsect_gen
FROM bedsect_type
GROUP BY
facility
;

SQLite C++ Compare two tables within the same database for matching records

I want to be able to compare two tables within the same SQLite Database using a C++ interface for matching records. Here are my two tables
Table name : temptrigrams
ID TEMPTRIGRAM
---------- ----------
1 The cat ran
2 Compare two tables
3 Alex went home
4 Mark sat down
5 this database blows
6 data with a
7 table disco ninja
++78
Table Name: spamtrigrams
ID TRIGRAM
---------- ----------
1 Sam's nice ham
2 Tuesday was cold
3 Alex stood up
4 Mark passed out
5 this database is
6 date with a
7 disco stew pot
++10000
The first table has two columns and 85 records and the second table has two columns with 10007 records.
I would like to take the first table and compare the records within the TEMPTRIGRAM column and compare it against the TRIGRAM columun in the second table and return the number of matches across the tables. So if (ID:1 'The Cat Ran' appears in 'spamtrigrams', I would like that counted and returned with the total at the end as an integer.
Could somebody please explain the syntax for the query to perform this action?
Thank you.
This is a join query with an aggregation. My guess is that you want the number of matches per trigram:
select t1.temptrigram, count(t2.trigram)
from table1 t1 left outer join
table2 t2
on t1.temptrigram = t2.trigram
group by t1.temptrigram;
If you just want the number of matches:
select count(t2.trigram)
from table1 t1 join
table2 t2
on t1.temptrigram = t2.trigram;

Finding closest date to a specified date in sas dataset?

Based on the data could anyone assist me - making a program that contains
test_date closest to delivery_date.
delivery_date 11/16/2011
test_date 21/nov/2011 10/nov/2011 5/oct/2010
Thanks in advance
Can't really answer properly without more information, but if you have two datasets and one row per date, and actual date variables, then the solution is something like this:
create table finaldsn as
select a.*
, b.*
, a.delivery_date-b.test_date as days
, abs(calculated days) as absdays
, min(calculated absdays)as close
from dsnA as a
full join
dsnB as b
on a.subject=b.id
where a.delivery_date ne . and b.ltest_datebdt ne .
and b.id in (select distinct subject
from dsnA)
group by a.subject, a.delivery_date
having calculated absdays=calculated close
;
quit;
This is from the following paper: http://www.lexjansen.com/pharmasug/2003/coderscorner/cc001.pdf which also presents a few other solutions.
You can use the SAS function INTCK to calculate the diference between a sample date and your target date, then you can just keep the date which is closer to 0.
here is a link so you can quickly pick up on this functions:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212868.htm
also, feel free to check out the other functions present on the left side of the link, as they can come in handy in the long run.