How to create model where a table represents both fact and dim? - powerbi

I have following tables:
User: userid, username
Customer: customerid, customer name, userid
Sales: salesid, customerid, userid
User filters sales and customers. And also User and customer should be able to filter sales.
How to make a star schema of the above?
Is it correct to duplicate the customer table such that 1 is a dim and other is fact?
Should the both customer tables link to each other or not have Link?

Related

How to multiply to columns in different table and get another column as output in django orm

I have three tables :
First one is product table consist of product details and price
Second one is customer table with customer details
And a cart table with customer_id and product _id as foreign key and qty field for quantity of product
I want join cart table and product table and get an additional column called total price that is the result of price in product *qty in cart
How to do this in django orm
I tried f function but it didn't work
You can try this.
Cart.objects.annotate(total_price=F('product__price')*F('qty'))

Power BI Indirect Relationship Filter Issue

I have 3 tables:
Employee
Survey
Manager
Employee tbl has direct relationship with both tables, however there is no direct relationship between survey and manager.
I am trying to create a chart/scorecard which does the average score on a question in surgery tbl. This works fine, however I would like to filter this by managers name from manager tbl, and this does not work.
Any help please?
If you configure a 1-many relationship from Manager to Employee, and a 1-many relationship from Employee to Survey then filters will flow from Manager to Employee to Survey.

Updating a large number of foreign key references in Django

I have a companies and employees tables, along with a third employees records table which contains a foreign key to both the companies and employees tables.
The company table has an entry of 'Company A'. Subsequently, 'Company A' splits into two new entries: 'Company A1' and 'Company A2'.
Now in the employees records table, currently a large number employees are associated with 'Company A'.
For each employee associated with 'Company A', I want to create two new entries which associates the employee with both 'Company A1' and 'Company A2'.
What would be the optimal way to accomplish this with Django? Considering that there could be hundreds of thousands or possible millions of rows of employee records that need updating.

On what basis is a decision to be made regarding denormalizing dimension tables?

Say I want to create a product dimension. It will have product id as primary key, product name as value.
The sales fact table will link to the product dimension on the product id key.
In the OLTP system the product table has normalized relationship with Product sub category table. And product sub category table has relationship to product category table.
I am trying to understand - On what basis or factors is a decision to be made regarding denormalizing tables and having the product category and sub category values into the product dimension table.

Django orm query help

Employee class has id, first_name, last_name, and a lot more fields.
I want to select all the employees that do not have the same last_name, It's something like distinct in SQL, how to do that?
employees = Employee.objects.value('last_name').distinct() will only include the last_name, so I cannot find id.
If I do employees = Employee.objects.value('id', 'last_name').distinct() the results.count() looks different.
That's just because you may have employees with the same last name.
When you add 'id' in the query, you ask for all distincts rows based on the two criterias, and you always have distinct id.
Am I clear ?...