Unable to connect Fact Tables to Calendar Table - powerbi

In all my fact tables, I have separate columns for quarter, and year.
I also have a calendar table with columns for Date, Quarter, Year, Month Name etc.
I want to connect both columns from my fact tables to the calendar table but PowerBI wont allow me. It also tells me that I have cardinality many-to-many since I have multiple records for the same Year and Quarter in both the Calendar and Fact tables.
My goal is to have a report consisting of slicers / dropdowns for Yearand Quarter, so that I can see how my metrics performed over time.
I'd like to know how to best handle such instances. How should I design my data model that will allow me to filter my reports by quarter and year.
Really appreciate your help!

The fact tables need to have an value that is unique in the calendar table, so the corresponding row from the calendar table can be identified.
In the most common scenarios, the fact table will have a transaction date column and the calendar table has a date column. In the calendar table, the date column has only unique values, whereas the columns for Month, year, etc, contain duplicates, of course.
In the Fact table, there may be many records with the same date, but each date can be connected to just ONE single entry in the calendar table.
Your screenshot does not show any date columns in the fact tables.
You don't have to use a date, but you need a key column in the Fact tables that can be linked to a column with unique values in the Calendar table.
If you add a combination of Year-Quarter to all tables, Fact AND Calendar, then you can use these in the relationship and the error will go away

Related

How to build relationship based on range of dates (periods) in Power BI

I'm trying to figure out the best way to build a relationship from a table that has records in a daily format (one record represents a single date) to a table that contains records in a date-range format (one record has a start-date and end-date, consequentially representing a period or range of dates).
Since my actual datafiles contain work-related information, I created 2 demo tables that contain dummy data that reflects the date columns in question.
Here is my DailyDate table
Here is my DateRanges table
Here is the current model view
I would like to be able to have a relationship built between the tables so that if I were to have 2 tables/matrices in the Report view, with one table showing the Daily Date Data and the other table showing the Ranged Data Data, I would be able to select a record in the Daily table and Power BI's highlight functionality would filter the records in the Ranged table so that only date ranges containing my selected date appear, and vice-versa (if possible).
For example, referencing this screenshot, if I were to select index 0 in the 'Daily Data Data' table, the 'Ranged Date Data' table should be filtered to only show records with index of 0 in the other table. If I were to select Index 2 (01/03/2022) in the Daily Date Data table then the Ranged Date Data table should be filtered to only show indices (0, 1).
In the model view, when trying to build this relationship, I can create a relationship from DailyDates.Date to DateRanges.StartDate and then from DailyDates.Date to DateRanges.EndDate; however, only a single relationship can be active so the highlight and slicer functionality will not give me the results I'm looking for.
As you can see from this demo, the datasets are small; however, my actual datasets contain around 50 million records in the Daily table and 10+ million records in teh Ranged table, so I'm hoping there can be an efficient method of getting this functionality that will not be too much of a load on memory.
Any advice into how to accomplish this would be greatly appreciated.

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
)

How to use a date slicer to filter two visualizations of two different columns in the same table in PowerBI

I have searched for this query and attempted to implement some solutions i've found but i've not been able to apply the solutions to my problem so i'm hoping you can help.
We have a table with data, including "created" and "submitted" date columns. I'm wanting to use one slicer to individually show how many results for items created on the selected date, and items submitted on the selected date.
Any help would be great.
Thanks in advance.
You will need a calendar table for this to work correctly as you will have to have a relationship between your data table and the calendar.
Using this example
In the above table, there is Order and Shipping Date, with a quantity column.
The calendar table is connected by the data column to the Order and Shipping Date. As you can only have one active relationship between the table, one is set as inactive. In this case the Shipping Date is Inactive.
So if I select a filter of 07/01/2018 on the Order data it will not show the quantity of items shipped, just the items ordered as it will filter out those rows.
In the above image, the table has been filtered using the active relationship and it only shows Orders for the 7th and Shipped on the 12th as that is what is in the table (Visual on the right)
So you will need two measures, one using the active and one using the inactive.
So you count the Orders as normal, and the Shipped with a relationship modifier:
Total Orders = SUM('TableName'[Quantity])
Total Shipped = CALCULATE(SUM(Billing[Quantity]), USERELATIONSHIP(Billing[Shipping Date], 'Calendar'[Date]))
USERELATIONSHIP forces the Total Shipped to ignore the filter context on active relationship and use the inactive one.
So it should return Shipped 366, Ordered 353 for the 7/01/2018. You'll need to use the calendar table in your visuals to drive the dates, not the dates in your data.

How to use a single date table for two date fields?

I'm using Power BI Desktop to explore some data. I have a table called "Reports" which contains a list of reports that need to be filed. There are two date fields, "Due Date" and "Date Completed".
I have a single date table, Dates. It has been marked as my date table in Power Bi.
My goal is produce a matrix where each row is a month in the due date and each column is a month in the date completed. However, I can't figure out how to get Power BI to use the dates table for both of my date fields. I can join it in the relationships pane - but it looks like I can have only a single active relationship at a time. I've also tried the "Get Data" feature to add the date table multiple times, but it will only add once.
How can I use my date table for more than one date field at a given time?
You need to create two relationships to the dates table-- one is Active, one is Inactive. All measures will use the Active relationship, and to calculate data in the reports using the inactive relationship you create DAX Measures using the USERELATIONSHIP() function.
Here is a couple resources:
https://www.sqlbi.com/articles/userelationship-in-calculated-columns/
https://carldesouza.com/power-bi-dax-userelationship-multiple-dates/

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.