A co-worker has asked me to help her with an issue she is having. Her data model looks as follows:
She is using SUMMARIZE to build the Jobs table. In part, the DAX looks like this:
Jobs = SUMMARIZE(
'MainSI Table',
'MainSI Table'[Market],
'MainSI Table'[Region],
'MainSI Table'[EmployeeId],
'EmployeeTable'[Business],
)
This works but she is not able to add any columns from WODetail (the error is that the column selected from WODetail cannot be found) and I do not know why when there is a 1:M relationship between MainSI and WODetail.
You need to use ADDCOLUMNS, RELATEDTABLE and then summarise the result from the detail table according to requirements. e.g.
ADDCOLUMNS (
SUMMARIZE(MainSI Table, MainSI Table'[Market]),
"Max",MAXX(RELATEDTABLE(WODetail),WODetail[Job Finish Date])
)
Related
I have a table Deals which has columns [DealId], [Open Date Id], [Closed Date Id] where the last 2 columns are like a foreign key to the Date table which has [Date], [DateId] column.
Power BI won't let me have 2 active relationship, so one is inactive.
Now I want to create some visuals indicating the deals that were open and closed in a custom range of time (using slicer).
How I tried to solve
The closest solution to this was creating a calculated column with the LOOKUPVALUE and adding the close and open dates directly to Deals table. And created 2 different pages with 2 different slicers, but this is far not the solution I wanted.
How can I solve this problem?
I don't know if what I'm going to say suits your needs based on the size of the tables or the rigidity of the data model due to other measures. I think that in the end what matters is to understand what are the limitations of what you want to show. However, something almost similar I answered here: https://stackoverflow.com/a/66792957/15460989
From what I could understand you have two tables similar to:
Deals = {[DealID] [OpenDate] [CloseDate] [Quantity] [Price] ...}
Dates = {[Date] [MonthName] [MonthNumber] [Year] ...}
And you want to filter Deals based on two relationships
USERELATIONSHIP (Dates [Date], Deals [OpenDate])
USERELATIONSHIP (Dates [Date], Deals [CloseDate])
I am not going to discuss the option of duplicating Dates Table because it was previously covered using two slicers.
But what if the characteristics of my model allow me to use a table with two relationship (One active and the other inactive) while my visualization uses the content of an unrelated table?
Let's define my new unrelated table as:
HarvestingDates = {[Date] [MonthName] [MonthNumber] [Year] ...}
and what I'm trying to achieve is something like this:
From a model like this one:
Deals[DealID]: Unique values.
Deals[OpenDate]: Repeated and missing dates
Deals[CloseDate]: A random number between 0 and 5 is added to Deals [OpenDate]
Instead of choosing an opening date and a closing date, I choose a date range not related to the model and the context related to the deals comes from the measures. Example:
Opened Deals: All the deals opened in a certain date range and summarized by the visualization.
HOpenedDeals: =
CALCULATE(
COUNTROWS(Deals),
TREATAS(
VALUES(HarvestingDate[Date]),Dates[Date]
)
)
Closed Deals: All the deals closed in a certain date range and summarized by the visualization.
HClosedDeals:=
CALCULATE(
COUNTROWS(Deals),
USERELATIONSHIP(Dates[Date],Deals[CloseDate]),
TREATAS(VALUES(HarvestingDate[Date]),Dates[Date])
)
Open and closed deals: All open and closed deals in the same date range summarized by the visualization
HOpened&Closed :=
VAR TotalRow= SUMMARIZE(HarvestingDate,HarvestingDate[Year],HarvestingDate[MonthName])
VAR CurrentDates=VALUES(HarvestingDate[Date])
VAR Result=
ADDCOLUMNS(TotalRow, "Count",
VAR CurrentMonthName= {CALCULATE(VALUES(HarvestingDate[MonthName]))}
VAR CurrentYear= {CALCULATE(VALUES(HarvestingDate[Year]))}
RETURN
COUNTROWS(INTERSECT(
CALCULATETABLE(Deals,
USERELATIONSHIP(Dates[Date],Deals[CloseDate]),
TREATAS(CurrentMonthName, Dates[MonthName]),
TREATAS(CurrentYear, Dates[Year]),
TREATAS(CurrentDates, Dates[Date])
),
CALCULATETABLE(Deals,
TREATAS(CurrentMonthName, Dates[MonthName]),
TREATAS(CurrentYear, Dates[Year]),
TREATAS(CurrentDates, Dates[Date])
)
)))
RETURN SUMX(Result,[Count])
Opened & Not Closed Deals: All open and non-closed deals in the same date range summarized by visualization
HO&NOTC :=
VAR TotalRow= SUMMARIZE(HarvestingDate,HarvestingDate[Year],HarvestingDate[MonthName])
VAR CurrentDates=VALUES(HarvestingDate[Date])
VAR Result=
ADDCOLUMNS(TotalRow, "Count",
VAR CurrentMonthName= {CALCULATE(VALUES(HarvestingDate[MonthName]))}
VAR CurrentYear= {CALCULATE(VALUES(HarvestingDate[Year]))}
RETURN
COUNTROWS(EXCEPT(
CALCULATETABLE(Deals,
TREATAS(CurrentMonthName, Dates[MonthName]),
TREATAS(CurrentYear, Dates[Year]),
TREATAS(CurrentDates, Dates[Date])
),
CALCULATETABLE(Deals,
USERELATIONSHIP(Dates[Date],Deals[CloseDate]),
TREATAS(CurrentMonthName, Dates[MonthName]),
TREATAS(CurrentYear, Dates[Year]),
TREATAS(CurrentDates, Dates[Date])
)
)))
RETURN SUMX(Result,[Count])
TEST
Date range: {5/27/2021…5/31/2021}
I am sure this can be improved but as I said at the beginning it is just an idea. Cheers!
In this case the easiest way is to implement a role playing dimension functionality by duplicating your date table. Power BI engine does not support role playing dimensions, so the workaround for small tables is just duplicating them, as described in this article.
In your case, you could create a table "Date_for_closed" using
Date_for_closed = ALL('Date')
This creates a copy of your original Date table. Then you can create relationships and only have active ones. This way it is even easier to maintain than a bunch of inactive relationships.
With this implemented you can build this:
from this source:
I have a table that looks like this. Table Screenshot
"FY 20-21 (Budgeted)", "FY21 Approved Budget" and "Revised Budget" are columns coming from three different data sources that I appended into one table. There isn't always data in all three of these columns, so I created a new column to consolidate the data with the following formula:
FY 20-21 Budget =
if(
and(
isblank('Comprehensive Budget'[FY 20-21 (Budgeted)]),
isblank('Comprehensive Budget'[FY21 Approved Budget])
),
'Comprehensive Budget'[Revised Budget],
if(
and(
isblank('Comprehensive Budget'[FY21 Approved Budget]),
isblank('Comprehensive Budget'[Revised Budget])
),
'Comprehensive Budget'[FY 20-21 (Budgeted)],
'Comprehensive Budget'[Revised Budget]
)
)
If both Budgeted and Approved are blank, use Revised.
If not, if both Revised and Approved are blank, use Budgeted.
If not, use Revised.
But if you look on the screenshot, if NONE of the columns are blank, it gives me Revised plus Budgeted. Where is the problem in my formula?
I have added your data here and found your Measure is perfectly returning your expected data as shown in the below image-
I Guess, there are some Aggregation issue in your case. You can right click on all column in the table visual properties and select Don't Summarize from the options. This should solve your issue I hope.
Can you please explain why I run into this alert message of circular dependency when I try to create relationship between dimension #product (or #region) and a #bridge table which is a Cartesian of product x region?
I have connected #bridge with Sales and Budget by single column P#G witch is concatenation of product and region.
Download file here: PBIX
The solution is simple. Do not use CALCULATE function in the DAX bridge tables. Instead add all that columns to the same table later as calculated columns.
I changed the original code of the bridge table which was:
ADDCOLUMNS (
CROSSJOIN ( '#product', '#region' ),
"P#R", COMBINEVALUES("#",'#product'[product], '#region'[region]),
"sales", CALCULATE ( SUM ( Budget[target] ) ),
"IsSale", IF ( CALCULATE ( SUM ( Budget[target] ) ) > 0, "Yes", "No" )
)
To something simpler:
ADDCOLUMNS (
CROSSJOIN ( '#prodact', '#region' ),
"P#R", COMBINEVALUES("#",'#prodact'[product], '#region'[region])
)
I modified the DAX code of bridge table so as to leaving only the columns necessary for joins. The columns that I needed to be calculated I added as calculated columns. And that's it. It was by pure chance I found that out while experimenting with it.
For playing with bridge tables I recommend this Alberto Ferrari's article: https://www.sqlbi.com/articles/avoiding-circular-dependency-errors-in-dax/. It inspired me to solve the problem. What I get from the Alberto's text is that the functions VALUES and ALL are no good for bridge tables. He mentions issue of using CALCULATE function inside the bridge DAX tables. The function somehow is translated to mixture of ALL and FILTER functions. Instead of VALUE and ALL, use functions as DINSTINCT and ALLNOBLANKROW.
Working PBIX file. Hurray!
A quick and dirty solution is to creat to new versions of #product and #region by using VALUES. This is probably not the best way of doing it...
NewProduct = VALUES('#product'[product])
This new tables can be linked to #bridge with a 1:* relationship and thus can be used as a slicer on the dashboard.
Alberto has written about this on the sqlbi blog: Circular dependency sqlbi blog
I have the following tables & relationships in our pbix report:
For some obvious reasons, I need to have a relationship (non-active) between Dates[date] and Table2[T2Date]. However, doing so causes data fluctuation to measure 'Total Amount' in Table1.
Here are some screenshots:
Before Relationship (Dates[date] - Table2[T2Date]):
After Relationship (Dates[date] - Table2[T2Date]):
I need to understand why this difference is coming up and how the relationship is causing it since the measure uses a different relationship.
For reference, I am attaching the pbix report.
https://drive.google.com/open?id=1XknisXvElS6uQN224bEcZ_biX7m-4el4
Any help would be appreciated :)
The link that #MikeHoney gives has really useful information on the subtleties of relationships and does relate to this problem (do watch it!), but this issue is not ultimately related to bidirectional filtering in particular. In fact, I can reproduce it with this simplified relationship structure:
The key thing to note here is that when you attach Table2 to Dates, since Table2 contains T2Date values that don't match to any Date[date], this creates an extra row in Dates with a blank date which you can notice in your filter on 6. Year when that relationship exists (active or inactive). Filtering out that that blank in the 6. Year filter would work, except that in your measure, you use ALL(Dates) to strip all filtering done on that table.
There are multiple ways to resolve this discrepancy, the easiest being replacing ALL with ALLNOBLANKROW. If you used ALLSELECTED that would also work in conjunction with filtering out blanks on your report-level filter on 6. Year.
Cleaning up some items not relevant in this context and changing ALL to ALLNOBLANKROW, your total measure can be more simply written as:
ALLNOBLANKROW =
VAR EndServiceDate =
MAX ( Dates[Date] )
RETURN
CALCULATE (
SUM ( Table1[Net Amount] ),
FILTER (
ALLNOBLANKROW ( Dates ),
Dates[Date] <= EndServiceDate
),
Table1[Flag2] = 1,
Table1[Flag] = TRUE ()
)
Results with no 6. Year filter and with two measures, one using ALL and one using ALLNOBLANKROW:
Notice that every row in the ALL column has been reduced by -7,872.01. This is the sum of all the Net Amount values that don't match to any dates in the Dates table. If you remove the relationship from Dates[date] to Table2[T2Date] then the blank row no longer exists and both of these will match the ALLNOBLANKROW version.
Setting the Cross Filter Direction to Both on any relationship is a bit risky - you essentially hand over control of the runtime query designs to the Power BI robots. There's then a risk that they will come up with a "creative" query design that is unexpected.
There's some insight into how this happens in a recent talk by Alberto Ferrari:
https://www.sqlbi.com/tv/understanding-relationships-in-power-bi/
I'm sure you'll agree it's quite terrifying.
Looking at your info, I expect you can avoid those traps by changing the Cross Filter Direction to Single, for the relationship from MonthYear to Dates.
This doesn't seem like it should be too complicated, but I'm not quite sure how to get it working.
I have a table in PowerBI with the following columns:
The columns in the database have an entry for Submitter and QAer
The QAs Posted column is basically just a COUNT of the Submitter
For QAs Pulled, I need to get the count of rows where the particular Submitter (in the first column) is listed as the QAer.
Is this something I can do?
Any help is appreciated, thanks!
EDIT: More about the data model - here's a screenshot example.
I think you are looking for something like this:
Measure =
COUNTROWS (
FILTER (
ALL( 'datatabel' ),
'datatabel'[QAer] = SELECTEDVALUE ( 'datatabel'[Submitter] )
)
)