How to create relationships with many fact tables in Power BI? - powerbi

How do I create a relationship between these 4 fact tables and 2 dim tables? How do I slice them based on quarter of the year? How to insert Q1 2023 into the fact tables?
When I change from Q1 to Q2 on the slicer, the count of MemberID doesn't change accordingly. Why is that? Thank you so much in advance.

You need to reshape your data in Power Query. Append your 4 tables so you have a single fact table. You can also remove MemberName from your fact table as it is already in your dimension table so just retain MemberID in your fact.

Related

Can we compare two tables in a Switch Case in DAX Power BI?

Is it possible to include columns from 2 different tables in DAX Switch Function ?
Both of my tables are linked by Many to Many Relationship.
I am trying to create a calculated column in Table A where I want to compare Columns from Table B, but I can't add the Table B column in the DAX Switch formula.
Any suggestions would be appreciated.
Thanks & Regards,
Pratik
Sample
Table B
Table A & Expected Result
In my opinion, it doesn't matter whether this table has many relationship, the only thing matter is that you are calculated based on your original table instead of calculate on a new measure, then result is unpredictable:
By create new table from the original table, and add column for the comment, I can provide result as your expectation, hope it solve your problem and accept the answer :)
Create new table from original table (Sheet1 is my old table) and select on ID column
Table = SELECTCOLUMNS(Sheet1,"ID",Sheet1[ID])
Create new column for comment
Column = SWITCH(LOOKUPVALUE('Sheet1'[Value],Sheet1[ID],'Table'[ID]), 0,"Matched with A","Matched with B")
The outcome for the new table:
Instead of trying to use two columns from two different tables for comparison in DAX, you can use the 'RELATED` DAX function to import a column from Table A to Table B or vice-versa. Then you can use this column for comparison in DAX.
Just set the appropriate relationship between Table A and Table B and create a new column with the RELATED DAX function to import the table that you want to use in the SWITCH function.

Power BI Divide two measure from different tables and show in a graph with time

I have two tables, Table one has daily data, Table two has weekly data. I've created a start of week column in Table 1 to get weekly data. Data is as shown below :
I want to create a table where I can divide these two measures. Both measures are counts in a week. I want to present this in a line/bar chart with time at the x-axis. Right now when I use the Date of Table 1, My measure 2 takes the overall count as the date of table 2 is not present and vice versa. I was thinking of creating a new Calendar table but I'm unable to get these measure values in that table.
I tried creating a custom calendar table but I'm not getting the desired result. I'm getting correct values from table 2 but no values from table 1. I feel the problem is because table 1 has duplicate date values.
Table 1 actual data before consolidation:Measure is the count of case numbers
I think you need a slight paradigm shift in your thinking, potentially.
Rather than looking for a way to create a third table from two other tables, what you should do is create a relationship between the two tables to make a rational description of how you want these tables to work, and then write the DAX on top of it.
So, in your case, you describe one table having daily data, and the other having weekly. The intermediary calendar table would be a daily calendar, where each day (row) knows the end of week date.
You would then create a relationship from your daily table to the calendar table based on day, and create a second relationship to your weekly table based on end of week date. (assuming bi-directional filtering)
You could then create a measure:
myRatio = DIVIDE(SUM(DailyTable[value]), SUM(weeklyTable[value])
In your chart, you can then show the daily value as a fraction of the weekly value by using the 'Day' field from the calendar table, or you could show the ratio of the complete week from the daily table to the weekly total in the weekly table by using the end of week date in the chart.
If what you truly need is a 3rd table, then you could use the SUMMARIZE() function on this 3 table set to do the summarization into a 3rd table using the same principle.
myNewTable =
SUMMARIZE(calendarTable
,calendarTable[End of Week Date]
,"My Ratio" //the name of the field you want to create
,[My Ratio] //the formula to describe what goes in the field
)

Power Bi Desktop - How to add values between tables?

I'm trying to create a column that has a total of values between 3 columns from 3 tables. How would I go about doing this?
The 2 tables are tables of values that share an id, and they are both linked to a table of account by Id. The goal is to add up 3 columns, and place it into a table grouped by the Id.
I've attempted summing them, trying to use the USERELATIONSHIP function, and creating a relationship between them. It seems to give very inaccurate results, as if it's summing all of the totals together, and passing them to each Id. That, or it won't let me use the column, as if it never existed.
EDIT: General Idea of what I'm trying to do (Lines should be pointing to Account's Id column, but I messed up the lines)
EDIT 2: I also forgot to illustrate or mention. There are more columns with information in each table that can't be summarized for each account preventing me from just merging the table together.
Make sure your data model looks like this (change names as you please, but the structure must be the same):
In dimensional modeling, your table "Account" is a Dimension, and both fee tables are Fact tables. The operation of combining data from multiple fact tables that share the same dimension is called "drill-across", and it's a standard functionality of Power BI.
To combine fees from these tables, you just need to use measures, not columns. This article explains the difference:
Calculated Columns and Measures in DAX
First, create 2 measures for the fees:
Fee1 Amount = SUM(Fee_1[Amount])
Fee2 Amount = SUM(Fee_2[Amount])
Then, create a third measure to combine them:
Total Fee Amount = [Fee1 Amount] + [Fee2 Amount]
Create matrix visual, and place Account_ID from the Account table on the rows. Then drop all these measures into the matrix values area, like this:
Result:
Of course, you don't have to have all these measure in the matrix, I just showed them for your convenience, to validate the results. If you remove them, the last measure still works:

power bi: combine multiple tables into one

In Power BI, I am creating a report with some finance data of a company. I have 3 different tables. The table structure of all three tables are as follows:
I want to change these tables into this structure:
Is it possible to achieve this kind of structure? If yes and please suggest some method to do this?
To do it simply you can import 3 times your table using the query editor, and then in one table keep only columns for Planned, in the second table keep columns for Actual, and so on...
Hope that helps!

One-to-many relationship always changed into many-to-one by PowerBI

I have two tables from Azure SQL in PowerBI, using direct query:
EMP(empID PK)
contactInfo(contactID PK, empID FK, contactDetail)
which have an obvious one-to-many relationship from EMP.empID to contactInfo.empID. The foreign key constraint is successfully enforced.
However I can only create a many-to-one relationship (contactInfo.empID to EMP.empID) in PowerBI. If I ever try the opposite, PowerBI always automatically converts the relationship to many-to-one (by swapping the from and to column), which prevents me from creating visuals. Does PowerBI think the two are equivalent?
Update:
What I'm doing is to just create a table in PowerBI showing the join results of these two tables. The foreign key constraint is contactInfo.empID REFERENCES EMP.empID, which is many-to-one. That should not be a problem, I guess, since I can directly query the join using SQL.
Please also suggest if I should create the foreign key in the opposite direction.
More info on failure to create visual
The exact error message is:
Can't display the data because Power BI can't determine
the relationship between two or more fields.
Version: 2.43.4647.541 (PBIDesktop)
To reproduce the error:
DB schema is as follows:
What I want is a table in PowerBI showing contact and sales info of am employee, that is, joining all the four tables. The error will occur when VALUES of the table visual contains "empName, contactDetail, contactType, productName", however, error will NOT occur if I only include "empName, contactDetail, contactType" or "empName, productName". At first I thought the problem may lie in the relationship between contactInfo and emp, but it now seems to be more complicated. I guess it may be caused by multiple one-to-many relationships?
Expanding my comments to make an answer:
Root of the Problem
In your data model, a single employee can have multiple contacts and multiple sales. But, there's no way for Power BI to know which contactDetail corresponds to which productName, or vice versa (which it needs to know to display them together in a table).
Deeper Explanation
Let's say you have 1 emp row, that joins to 10 rows in the sales table, and 13 rows in the contactInfo table. In SQL, if you start from the emp row and outer join to the other 2 tables, you'll get back (1*10)*(1*13) rows (130 rows in total). Each row in the contactInfo table is repeated for each row in the sales table.
That repetition can be a problem if you do something like sum the sales and don't realize a single sales record is repeated 13 times but might be fine otherwise (e.g. if you just want a list of sales and all associated contacts).
Power BI vs. SQL
Power BI works slightly differently. Power BI is designed primarily to aggregate numbers, and then break them down by different attributes. E.g. sales by product. Sales by contact. Sales by day. In order to do this, Power BI needs to know 100% how to divide numbers up between the attributes on your table.
At this point, I'll note that your database diagram doesn't include any obvious metrics that you'd use Power BI to aggregate. However, Power BI doesn't know that. It behaves the same whether you have metrics to aggregate or not. (And failing all else, Power BI can always count your rows to make a metric.)
Let's say that you have a metric on your sales table called Amt Sold. If you bring in the empName, productName, and Amt Sold columns, Power BI will know exactly how to divide up Amt Sold between empName and ProductName. There's no problem.
Now add in contactDetail. Using your database diagram, Power BI has no way of knowing how an Amt Sold metric in the sales table relates to a given contactDetail. It might know that $100 belongs to empID 27. And that empID 27 corresponds to 3 records in the contactInfo table. But it has no way of knowing how to divide up the $100 between those 3 contacts.
In SQL, what you'd get is 3 contacts, each showing the $100 amount sold. But in Power BI, that would imply $300 was sold, which isn't the case. Even equally dividing the $100 up would be misleading. What if the $100 belonged entirely to 1 contact? So instead, Power BI shows the error you're seeing.
My Recommendations
If you can, I recommend changing your data model before your bring it in. Power BI works best with a single fact table, which would contain your metrics (like amount sold). You then join this fact table to as many lookup tables as you like (e.g. customer, product, etc.), directly. This allows you to slice & dice your metrics with any combination of attributes from any of the lookup tables. I would recommend checking out the star schema data model and the concept of lookup tables: powerpivotpro.com/2016/02/data-modeling-power-pivot-power-bi
At the very least, you would want to flatten your tables (i.e. merge the contactInfo and sales tables into a single table before importing them into your data model.
It may be that Power BI isn't the best tool for what you're trying to accomplish. If all you want is a table showing all sales & contact info for an employee, without any associated metrics, a regular reporting tool + SQL query might be a better way to go.
Side Note: You can't reverse a many:one relationship to get past this error. The emp table contains one row per empID. Both the contactInfo and sales tables contain multiple rows with the same empID. This means the emp table is necessarily the "one" side of the relationship to both those tables. You can't arbitrarily change that.