DAX Power BI circular dependence problem across multiple tables - powerbi

After years of benefitting from answers here, and long hours of research to resolve a DAX circular dependency, I'm posting my first question. I am self trained, and hopefully, I'll explain properly.
For clarity, I'm giving full detail below, but my basic question is how to avoid circular dependencies when calculating columns in one table that reference multiple other tables.
I'm looking to build a report that shows total sales by salesperson from two tables, CRM_Opportunities and BMC_Usage. Identifying the salesperson for each row of BMC_Usage involves referencing data from four other tables, which works fine within BMC_Usage, but results in a circular dependency when I try to link to the CRM_Users.
relationships
I have five tables from three systems:
Three tables from our CRM: CRM_Users, CRM_Accounts and CRM_Opportunities.
CRM_Users contains the fullname and crm user_id of each
salesperson.
CRM Opportunities contains rows of opportunities, each
with createdon date, client_id and salesperson_key.
CRM_Accounts contains client_id and salesperson_key.
BMC_Usage contains sales data for our new product, but no sales person information. Each row equates to one unique billable event. It does contain the date when the service began, start_date and the client id field bmc_client_id.
One table from our billing system, UBS_Accounts, includes the name of the sales person, ubs_salesperson along with the client id ubs_client_id.
In order to produce a consolidated report by sales person, I need to first identify who the correct sales person is for each row of usage in BMC_Usage using the following series of calculated columns in BMC Usage:
First, find out who opened the last sales opportunity in CRM_Opportunity prior to start_date in BMC_Usage:
Last Opportunity Date = CALCULATE(MAX(CRM_Opportunities[createdon]),CRM_Opportunities[createdon]<=EARLIER(BMC_Usage[start_date]),CRM_Opportunities[client_id]=EARLIER(BMC_Usage[bmc_client_id]))
Next, in case there's been no opportunities created yet, check when the account was last created in CRM_Account and also prior to start_date in BMC_Usage:
Last Acct Date = DATEVALUE(CALCULATE(MAX(CRM_Accounts[account_createdon]),CRM_Accounts[account_createdon]<=EARLIER(BMC_Usage[start_date]),CRM_Accounts[crm_client_id]=EARLIER(BMC_Usage[bmc_client_id])))
Third, grab the salesperson_key field based on the Last Opportunity Date, or if blank, from the Last Acct Date:
salesperson id = IF(ISBLANK(BMC_Usage[Last Opportunity Date]),
IF(ISBLANK(BMC_Usage[Last Acct Date]),
BLANK(),
LOOKUPVALUE(CRM_Accounts[salesperson_key],CRM_Accounts[crm_client_id],BMC_Usage[bmc_client_id],CRM_Accounts[account_createdon],BMC_Usage[Last Acct Date])),
LOOKUPVALUE(CRM_Opportunities[salesperson_key], CRM_Opportunities[client_id],BMC_Usage[bmc_client_id],CRM_Opportunities[createdon],BMC_Usage[Last Opportunity Date]))
Now that I have the salesperson id, I can get the fullname from CRM_Users:
CRM salesperson = LOOKUPVALUE('CRM_Users'[fullname],'CRM_Users'[crm_user_id],BMC_Usage[salesperson id])
Since it's possible our CRM has no record for this client, I can also pull the ubs_saleperson name from our billing system, UBS_Accounts, although it is a less reliable source.
UBS salesperson = RELATED(UBS_Accounts[ubs_salesperson])
Finally, I arrive at who the salesperson is for each row of BMC_Usage by preferring the CRM name over the UBS_Accounts name:
calc salesperson = IF(ISBLANK(BMC_Usage[CRM salesperson]),BMC_Usage[UBS salesperson],BMC_Usage[CRM salesperson])
And it all works fine!
calc salesperson
The problem is, I can't build a relationship between BMC_Usage and CRM_Users so that I can make a consolidated report.
circular dependancy
From reading online, I believe the problem is caused by the multiple calculated columns and possibly the presence of blank rows. I've tried filtering options using ALLEXCEPT, DISTINCT and others but I can't seem to get it right. I'm not sure if I should be filtering the local table or the tables I'm pulling information from, and I'm just lost.
If you've read this far....THANK YOU! I know it's a long question. Perhaps you have an idea?

Related

Date Table Returning zero values

I am pretty new to Power BI and DAX so I apologize if my vocabulary isn't entirely accurate, however I'm having trouble with the functionality of the program.
Goal: I would like to create a visual that returns the count of ID's based off of date using a date table 
Summary of Problem: I create a relationship between a date table and fact table. When creating a visual using the date table field [date], and fact table count of [Id's] the relationship between the date table seems to break and return nothing.
Details: I created a date table and created a relationship to a 'Lead' Database table using the respective "Date" field (in the Main date table) to the "CreatedDate" (in the 'Lead' table). Shown Below
*Uses a 1:Many relationship with a single crossfilter moving towards the 'Lead' table
When I pull in a table in the visualizations, using a the dimensions of 'Main Date Filter'[Date] and count of 'Lead'[Lead Id], it seems theres no relationship between the 'Main Date Filter'[Date] and 'Lead'[CreatedDate] because none of the dates populate and counts all the ID's into a blank date
Also I made sure to turn off filters off for this example and strip all relationships to just the lead object and date
Here are the further data types of each of the fields that are being related
Thank you for taking the time to find a solution and I appreciate the help as this simple problem has been driving me crazy!
Once again the goal is to just return the count of Id's per date and I don't understand why this is not working.

Trying to create a line chart with two lines for similar data from same data table

I'm trying to create a line chart in Power BI with two lines representing counts by date. The data is pulled from a single data table. I am trying to show two lines, one representing an incoming count and one representing an outgoing count. Each record has a unique identifier (IN). So, the chart would have months on the x-axis pulled from a date table I created. One line would show the count of incoming IN for each month, and the other line would show the count of outgoing IN for each month. When I try to do this, both lines show the same count. I've tried a number of different measures, but nothing seems to work. These are the latest that I'm trying.
Intake by Date = CALCULATE(DISTINCTCOUNT('sysadm kennel (2)'[impound_no]),'sysadm kennel (2)'[intake_date])
Outcome by Date = CALCULATE(DISTINCTCOUNT('sysadm kennel (2)'[impound_no]),'sysadm kennel (2)'[outcome_date])
I'm pretty new to Power BI, so I don't have a full grasp of how to create measures. The Intake By Date seems to be the correct one, but the Outcome by Date is showing the same number. Any help would be appreciated.
Check your model. Create an active relationship between Calendar and DataTable to your Intake; Add an inactive relationship between Calendar and DataTable to your Outcome; In measure for [Outcome by Date] use USERELATIONSHIP('Calendar'[Date],DataTable[OutcomeDate])
https://dax.guide/userelationship/

How to switch data from two tables based on filter in Power Bi

I have two tables which have counts and sales based on dates and one of them also have customer ID. The counts are not same when we see by customer and summary. I also have customer filter on my dashboard. What I want to achieve is if no customer is selected the count should come from summary table otherwise it should come from customer if multiple or one is selected in the filter.
Customer Table
Summary Table
Any hints, I have tried lookupvalue function but I cant put date as search value from date table.
It's much easier to use Measures, instead of creating calculated tables to obtain those metrics. Also, summarized tables would not have the same filter context your are looking for.
Measure 1
Total Customers =
DISTINCTCOUNT('Customer Table'[CustomerID])
Measure 2
Total Sales =
SUM ( 'Customer Table'[Sales])

Calculated column based on a disconnected slicer

I'm not sure I've summarised this question correctly, so I'll add a description below of the actual problem.
We have a report that displays data for 'jobs'. These jobs have a few relevant date fields on them such as Created Date, Completed Date and Invoice Date. We currently have 3 slicers on the report that work as expected based on these dates.
Users sometimes use all 3 slicers at the same time and end up with all sorts of useless data. No matter how many times we've told them to reset the filters before changing the date slicers, they keep forgetting to do that.
They also do not want to have 3 separate reports.
We've been tasked with providing them with the following:
Something that lets them select what they want to filter on, with the options only being Created, Completed or Invoiced
A slicer that works based on the option they selected above.
We've tried creating a calculated column that is set to one of those dates based on the option selected in the disconnected slicer, and then adding a slicer that works off that column, but we aren't having any luck. The values in the calculated column are not correct
This is what we've done in attempting to solve the problem:
Create a measure that captures the selected value
SelectedMeasure = SELECTEDVALUE('Date Filter Options'[Name],"Created Date")
Create a calculated column to set the appropriate date value
Please note we're only testing with two out of the three possible values at the moment.
Selected Date Filter = if(jobs[SelectedMeasure] = "Created Date", (jobs[Created Date (DateOnly)]), (jobs[Completed Date (DateOnly)]))
What we're finding is that the Selected Date Filter column does not update with the correct date value, even after we change the slicer and the measure updates as expected.
We're totally stumped - I'm aware we are probably a fair bit away from the correct solution, so any help would be appreciated
Not quite sure how to best present the data model, but here are the relevant fields of the 'jobs' table
jobNumber: string
jobId: guid
CreatedDate: date
CompletedDate: date
InvoicedDate date
The Date Filter Options table just consists of one column with these options:
"Created Date"
"Completed Date"
"Invoiced Date"

Filter by last not blank date in Power BI

I have data from multiple countries on a monthly basis. Since the updates are not regular, I want to set up filter to visuals, so they would show the last month for which I have data from all the countries. I have data from each country loaded into a separate dataset, which then are merged into one big. Is there an easy way to place such filter? I managed to use "LASTDATE" function in each of country sets to find which date is last, but if I try to filter with that measure, I simply get nothing in a result. Thanks!
Well, this feels a little clunky to me but I believe it will work for you. There are two steps. The first is to create a summary table that reads through your data and counts the number of distinct countries that you have in each month. This will be a new table in your model, so go into the modeling tab, click 'New Table' and add this DAX. Obviously, correct for your table and column names.
SUMMARIZED_ROWS = SUMMARIZE(
'Table1'
,Table1[Month]
,"CountOfCountries"
,DISTINCTCOUNT(Table1[Country])
)
Now add a measure to the table (or anywhere) like this:
MonthWithMostCountries = CALCULATE(
LASTNONBLANK(SUMMARIZED_ROWS[Month], 1 )
, FILTER(SUMMARIZED_ROWS, SUMMARIZED_ROWS[CountOfCountries] = MAX(SUMMARIZED_ROWS[CountOfCountries]) ) )
This is going to give you the month where you have the most distinct countries in your data. You'll want to look at it in a card or similarly isolated visual as it is a measure and can be affected by filter context.
So, on the left is my mock data - 3 countries, 3 months each with a 1 month stagger. On the right you see the result of the Summarize table. Then the measure showing the final result.
Hope it helps.