I have a wide table called Sales.
Each row represents a sale. It has got various info like sales, customer, product.
I'm building a star schema from this using SQL to create product dimension using:
Select distinct prodnumber,prodname,size,class
From sales
Building fact table as follows:
Select salesamt,salesdate,prodnumber,prodname,size,class
From sales
The reason for having prod fields in fact table is that- A unique product unfortunately isn't identified by productnumber, rather by prodnumber,prodname,size,class.
Hence the key is a combination.
So in Power BI when joining the prod dimension and fact table in the final report I need to join on 4 columns. Is there an alternative way to do this? Like somehow generating a unique key to make the join simpler?
2条答案
按热度按时间kqlmhetl1#
This answer is done in Power BI PowerQuery.
It is doing the almost exactly the same thing as the other SQL answer.
For your first query, let's call it
Dim Product
, Go toAdd Column
in the ribbon, and selectIndex Column
>From 1
. Then in the formula bar, rename"Index"
to"ID"
(or similar).For your second query, let's call it
Fact Sale
, in theHome
tab in the ribbon, selectMerge Queries
. In the pop-up:Fact Sale
at the top click onprodnumber
column, then while holding downCtrl
orShift
, selectprodname
,size
, andclass
. You should see 1, 2, 3, 4 appear in their headers.Dim Product
headers, ensuring the same order as above.It should look like:
Finally:
prodnumber
,prodname
,size
,class
- and remove those columns.Dim Product
header, and only selectID
, and uncheckUse original column name as prefix
.ID
column toProduct ID
if you like.And that's it.
1dkrff032#
Then you will need to include the PK (in an FK form) in your fact table You can find what this is by doing the select as follows
CREATE THE FACT TABLE FIRST WITH THE COLUMNS LISTED APART FROM THE PRODNUMBER, PRODNAME, CLASS AND SIZE COLUMNS BUT WITH A PRODUCT_FK INT NOT NULL
Finally you can then join a single table using the product_pk and product_fk in your dim and fact tables respectively (if in power bi create the relationship on this)