Power BI Table Relationship - database

I am trying to create two relationships between tables in Power BI. I have a table (Observations) that contains an employee_id and a supervisor_id that I am trying to connect to my Contact table.
I need to look up the employee information in the Contact table by using employee_id in the Observation table and also look up the supervisor information in the Contact table using the supervisor_id in the Observation table.
Contact Table Observation Table
=================== =================
Employee_ID Name Position Observation_ID Employee_ID Supervisor_ID
--------------------------------------- ----------------------------------------------
123 John Doe employee 1 123 321
321 Harvey Sample supervisor 2 456 654
456 Mickey Mouse employee 3 456 654
654 Donald Duck supervisor 4 123 321

One possible way is to create two relationships between the observation and contact table:
Create a relationship between Observation Table(Employee_ID) and Contact Table(Employee_ID). The Cardinality should be many to one, and set this relationship as active.
Create a relationship between Observation Table(Supervisor_ID) and Contact Table(Employee_ID). The Cardinality should be many to one, and set this relationship as inactive.
create an inactive relationship Power BI
You will then need to use the DAX USERELATIONSHIP function when you want to use the inactive relationship in a formula. Here is a detailed explanation of how to use USERELATIONSHIP: https://www.sqlbi.com/articles/userelationship-in-calculated-columns/
Alternatively, you can duplicate the contact table and use one copy for the Employee_ID relationship and the other for the Supervisor_ID relationship. Which option is best will depend on your use case.

Related

How can I build a query to view the same value in 20 or more tables in SQL?

Scenarios:
Table 1
Name ID dept
John 112 Fin
Mary 113 Act
Table 2
Name email
John John#gmail.com
Mary Max#gmail.com
Table 3
Name Supervisor
John Kelly
Mary Adam
Table 4
Namevalue Salary
John 1000
Mary 1000
Expected Output:
I would like to know the occurrence of John and Mary in all these tables. There are more than 20 tables in difference database.
Firstly, specify the master table according to data quality, use this table as base and then use left joins to concat with other tables.
But, I think, there is a design problem in the database so maybe creating keys for each user can be useful.
For example, name + surname + User Something to make records unique then use joins otherwise all records will duplicate.

SSIS Multiple flat files into tables with Parent Child

This is my tables in SQL Server:
Table: Customer (CustNum is the PK and auto increments)
CustNum Firstname Lastname
123 Bob Smith
456 John Paton
789 Fred Bloggs
Table: Job (CustNum is the FK, JobNum is the PK and auto increments)
JobNum CustNum Item Desc
852 123 ABCDE Widgets
654 456 WERT More Widgets
987 789 QWE Mouse mat
I then have data coming from two csv's in the below format
Table: Customer
Firstname Lastname
Bob Smith
John Paton
Fred Bloggs
Table: Job
Item Desc
ABCDE Widgets
WERT More Widgets
QWE Mouse mat
I need to import the customers then the jobs into SQL tables but maintain referential integrity. When the Job record is created, it needs to lookup for the correct CustNum - how do I do this? I have found many examples doing this from one CS file, but not from multiple csv's. Also, there is no reliable key to tie the job rows to the customer table - I don't think I will be able to use the SSIS lookup - will I?
You can use an SSIS Package to feed your data base.
the package will look like it
Result Customer
CustNum Firstname Lastname
----------- ---------- ----------
1 Bob Smith
2 Fred Bloggs
3 John Paton
Result
JobNum CustNum Item Desc
----------- ----------- ---------- --------------------------------------------------
1 1 ABCDE Widgets
2 2 QWE Mouse mat
3 3 WERT More Widgets
How to do it:
Main task:
Add a DTS to Customer
Add a DTS to Job
Create the flow (green arrow) from Customer to Job
Customer DTS
Add a connection to Customer File
Add a connection to Customer SQL
Sort Both outputs by Firstname, Lastname
Configure the merge join task like the image
Insert and configure the conditional Split with one output called "new" and expression ISNULL(CustNum)
Insert to the Customer table
Job DTS
Insert a connection to Job file
Insert a connection to Customer table
Sort both outputs by Firstname, Lastname
Configure the join like the image:
Insert a connection to Job table
Sort both by CustNum, Item
Insert and configure the conditional Split with one output called "new" and expression ISNULL(JobNum)
Insert to the Job table
I was able to change the source data so it appears in 1 CSV file as opposed to being in 2. I was then able to use the SQL Lookup function in SSIS and it now works. Thanks for all those who posted.

Table keys for SSAS project?

I'm creating a very basic cube with the data that I have. This is my scenario; please let me know if I'm approaching this the wrong way.
In our case, there's data in fact table FactSalesDaily that my dim table DimEmp doesn't have, and I want to know what that data is.
I have three tables: FactSalesDaily, DimDate, DimEmp
[FactSalesDaily]
EmpKey DateKey SalesAmount
194 20170125 45.30
195 20170125 21.00
[DimDate]
DateKey FullDate
20170125 01-25-2017
[DimEmp]
EmpKey FullName
194 Joe Smith
As you can see, my fact table has EmpKey 195 that's not part of DimEmp. My understanding is that in the Data Source View Wizard, when I click on Add Related Tables, the wizard will display the related tables based on the foreign keys for fact table FactSalesDaily.
In my particular case, I would like to see the total sales for employee 195. But I can't add a foreign key of FactSalesDaily to DimEmp because 195 doesn't exist.
As a result, Data Source View Wizard will not select DimEmp because there's not relationship between the two tables.
How am I supposed to deal with this issue? I can add a bogus row in DimEmp for all the employees that don't exist, but then I'd have to alter FactSalesDaily by replacing 195 with this bogus row.
Thanks.

Multiple fields in one sheet mapping to single field in another sheet

I need help designing the following architecture. Say Table 1 has a list of companies along with their information. Then Table 2 has a list of "primary" companies, followed by multiple fields denoting the companies that they do business with ("secondary" companies). So the first field in Table 2 is the "primary" company, and then there could follow up to 20 columns, each with a single entry of a company that does business with the "primary company". Each of those companies are listed in Table 1. I want to be able to tie the information about those "secondary companies" in with the information about them in Table 1. For example, I want to be able to see how many "secondary" companies in Table 2 are from California. I each company listed once in Table 1 along with headquarters locations; but each company in Table 2 is in a distinct column. I'm just getting confused about how to structure this, because when I try to make a query relationship between Table 1 and Table 2, I end up making a join between "Company" in Table 1 and every column in Table 2. Not good, right?
I have something like this...
Table 1
Company City State
A Los Angeles CA
B San Diego CA
C New York NY
.
.
.
Table 2
Primary Company Secondary1 Secondary2 Secondary3 Secondary4
A B C X Y
B A C Z W
C A B W X
Do I need another table for this? Should I just concatenate these 20+ secondary company fields into one column somehow? Any direction you could give would be very helpful.
Thank you!
I think what you want is this:
Table 1
Company City State
A Los Angeles CA
B San Diego CA
C New York NY
.
.
.
Table 2
Primary Company Secondary
A B
A C
A X
A Y
B A
B C
B Z
B W
...
In this case you will have a relationship between table1, column1 and table2 column 1 and 2. This is acceptable (though technically better if you use numbers instead of letters).

Entity relation

Is the following entity relation correct? I am trying to link employee, timesheet and approver table.
There are few other tables and fields but for this question my main concern is following three tables. One employee can have more than timesheet approver.
Employee
--------
EmpID pk
Name
TimeSheet
------------
TSHEET_ID PK
FK_EmpID FK
Approved_By
Timesheet_Approver (one employee can have more than one approver
but only one will be approving the timesheet)
------------------
EmpID
Approver_EmpID
Employee Table Data:
EmpID Name
----- -----
1 john
2 david
3 mark
Timesheet Approver Data:
EmpID Approver
----- --------
1 2
1 3
2 3
Timesheet
TSID EMPID APPROVED_BY
---- ----- -----------
101 1 2
102 1 3
103 2 3
OR this looks okay?
My Suggestion
Why do you want Approver as a seperate table? Can you keep a Bit Field like IsApprover in Employee table? Otherwise you are creating redundancy
I feel something wrong in the design. This is my idea.
Employee
EmpID pk
Name
TimeSheet
TSHEET_ID PK
FK_EmpID
Approved_By
Third table is unnecessary.
If there are several approves we can't add a column called Approved_by to the TimeSheet table.
Then we can add two more relations. One is for approvers list.
Approvers
ApproverID
Department
Name
TimeSheetApproves
TimeSheetID
Department1ApproverID
Department2ApproverID
..........................so on
Don't forgot to remove Approved_by to the TimeSheet realtion.
Employee
EmpID - Pk
Name
TimeSheet
TSHEET_ID - PK
EmpID - FK
ApproverID - FK
Approvers
ApproverID -PK
Department
OK this is my final answer. If it doesn't suit you can try your own one. I don't have time to read all those comments. And I was trying to help you.
Using the Approvers relation you can get approvers in a department and other things. Check sql queries to get those data.

Resources