Given the following data model:
I need to build a report that will display the following attributes:
As you see, I need to provide an attribute from the SalesOrder table [InternDocumentNumber[, so the visual will have grain on the order level.
Now, the problem is with measure [first order date v1] that should calculate the first order date made by the customer and display this value for all different orders from the same customer.
So far, I build the following measure
first order date v1 = CALCULATE (
FIRSTDATE( SalesOrderDate[SalesOrderDate]),
USERELATIONSHIP( SalesOrder[OrderDate], SalesOrderDate[SalesOrderDate]),
CROSSFILTER ( SalesOrder[OrderDate], SalesOrderDate[SalesOrderDate],both ),
ALL(SalesOrder)
)
However, this measure calculates forever, ending with the error:
Could you please advise me on how to write this measure, so it will perform better?
EDIT
Let me use an example from DAX.DO to be more clear on what I would like to get. Below is a screen of query and the result
What I would like to achieve is a measure, that will show me the min date for order on a customer level, and not the order level, as it is now. In other words, values in the red rectangle should be the same for customer 6, regardless of the grain of the table. Is this possible in DAX at all?
Reference to DAX.DO snippet:
https://dax.do/wxU6NNRHrencrg/
EDIT 2
Actually, I am able now to design a measure that solves the problem, however it performs very bad (timeouts in the end)
Here is the code screen and the DAX.DO reference
https://dax.do/wxU6NNRHrencrg/
You can try this:
CALCULATE (
MIN ( Sales[Order Date] ),
ALLEXCEPT ( Sales, Customer[CustomerKey] )
)
Related
I have a table from a survey that reports the score given to a specific employee, and various columns are there to hold each score for each question. Like this table below:
Now, what I want to do is make a row for each question, and in the original table, each question is a column. And I'd like for example, John, to have 1 entry for each question, and the average of that score stored next to each question like in this table here:
.
This shows clearly what I'm aiming for.
I believe I need some sort of pivot or unpivot table going on, but I'm not too sure on the Power BI DAX syntax for creating a new table.
I currently have a table that provides each Employee once, and columns showing their average score for each question, but that is a bit harder to dice up the way I want to. Code pasted below:
ReportTable =
SUMMARIZE(
ALL ( '360Sample'[Name], '360Sample'[Relationship to person being reviewed]),
'360Sample'[Name],
'360Sample'[Relationship to person being reviewed],
"Employee Satisfaction", DIVIDE(CALCULATE(AVERAGE('360Sample'[Treats others with respect/Truly values employees]))+AVERAGE('360Sample'[Encourages and supports staff in developing their skills])+AVERAGE('360Sample'[Provides effective mentoring]),3),
"Quality Product", DIVIDE(CALCULATE(AVERAGE('360Sample'[Consistently strives to provide products above industry quality standards]))+AVERAGE('360Sample'[Takes ownership of project outcome])+AVERAGE('360Sample'[Ensures quality control always happens on products]),3),
"Client Satisfaction", DIVIDE(CALCULATE(AVERAGE('360Sample'[Fosters open, honest, and consistent communication with clients]))+AVERAGE('360Sample'[Responds quickly to client questions and concerns.])+AVERAGE('360Sample'[Successfully communicates contractual needs and requirements with the Client, including schedules and scope and fee increases]),3),
)
Only difference here is that I have an extra attribute of "Relationship" which I'll also need to include but is less important for now. It makes a row of each employee for every unique Relationship, which is 2.
Hello You need to first use the "unpivot" in Power Query to convert your table into this shape: It is not so hard.
Like this:
Then use this DAX Code:
ReportTable =
ADDCOLUMNS (
SUMMARIZE ( 360Sample, 360Sample[Name], 360Sample[ScoreNum] ),
"ScoreAvg", CALCULATE ( AVERAGE ( 360Sample[Score] ) )
)
And It produces:
Description:
I need help trying to figureout my issue with a visual not displaying after I use a date filter. Below is my simple measure for counting the unique ID while filtering ALLSELECTED within the given Year/Month (202111). My result is a running total that doesn't care about the year so that I can keep it adding for any given time range, instead of it splitting per year (which is the default calculation). I can't use ALL in my FILTER because none of the page filters will function.Blank visual when filtering CreatedDt only.
Blank visual when filtering CreatedDt only.
Problem:
My problem is that when I have a change my date range filter (CreatedDt Filter) everything in my visual goes blank, but when I use any other filter (i.e. Carrier, Agent, etc.) those will work for filtering. It seems that its only the date filter and when the measure has ALLSELECTED in it.
Measure Calculation:
QuoteRequestId running total in CreatedDtMonth =
CALCULATE (
COUNT( 'vw_rpt_AllSubmissions'[QuoteRequestId] ),
FILTER (
ALLSELECTED ( 'vw_rpt_AllSubmissions' ),
'vw_rpt_AllSubmissions'[CreatedDtYearMonth] <=
MAX('vw_rpt_AllSubmissions'[CreatedDtYearMonth] )
)
)
I have actually found my own solution to this issue, see below for my answer:
Calculation Solution:
CALCULATE ( COUNT( 'vw_rpt_AllSubmissions'[QuoteRequestId] ), FILTER ( ALLEXCEPT( vw_rpt_AllSubmissions,vw_rpt_AllSubmissions[PayToCarrierName],vw_rpt_AllSubmissions[AgencyName], vw_rpt_AllSubmissions[AgentFullName],vw_rpt_AllSubmissions[CreatedDt] ), 'vw_rpt_AllSubmissions'[CreatedDtYearMonth] <= MAX ( 'vw_rpt_AllSubmissions'[CreatedDtYearMonth] ) ) )
Solution Description:
I have come up with the above work around that allows me to add the listed Page Filters into my DAX calculated column so that it manually accepts what is being used. This isn't the best case scenario because I'll have to add it each time that I change my page filters but I won't be doing that often. I hope someone else can benefit from this solution and please leave any comments if a better solution comes up.
INTRO
I realize the title makes the problem sound simple, however, this task has proved incredibly difficult for me and it's taken up hours of my time every day for the past week. With that being said, any help is appreciated!
The first table involved is LoadView, which contains the fields LoadNumber, CarrierID, and BookedFrom. The second table is LoadBaseView, which contains the fields LoadNumber, CarrierID, and BookedOnDateTime. These two are related by LoadNumber.
The visualization I wanna add to is the following, where the new row would be "New Carriers" listed right under "Carriers":
Lastly, preliminary info wise, that table is just a matrix with LoadView[BookedFrom] as the only context (Autobook, etc.) and simple measures across LoadView along with it.
PROBLEM
Now that I've (hopefully) laid everything out clearly, let me explain exactly what I am looking for. I would like to count the amount of new carriers that have booked in each BookedFrom category, i.e. I would like to count the amount of carriers booking that have never booked before for each category. This means that any carrier could potentially be counted in each BookedFrom column, just to clarify. I've tried many different measure to capture this and I've run into a whole host of problems, including memory insufficiencies to exceeding available resources. The latter's DAX is the following:
IsFirstCarrierBookedFrom* =
Var current_booked_from = MIN(dsgLoadView[BookedFrom])
Var T1 = ADDCOLUMNS(ALL(dsgLoadView),"BookedOnDateTime",RELATED(dsgLoadBaseView[BookedOnDateTime]))
Var T2 = GROUPBY(T1,dsgLoadView[CarrierId],dsgLoadView[BookedOn],"MinBookedOnDateTime",MINX(CURRENTGROUP(),[BookedOnDateTime]))
Var T3 = NATURALINNERJOIN(T1,T2)
Var T4 = FILTER(T3,[BookedOnDateTime]=[MinBookedOnDateTime])
RETURN
CALCULATE(DISTINCTCOUNT([CarrierId]),FILTER(T4,[BookedFrom]=current_booked_from),USERELATIONSHIP(dsgLoadView[BookedOnDate],dsgCalendar[Date]))
The above results in this error:
This next attempt results in a memory insufficiency error:
TotalFirstCarrierBooks* =
Var current_row_carrier_id = MIN(dsgLoadBaseView[CarrierId])
Var current_row_booked_from = MIN(dsgLoadView[BookedFrom])
Var first_booked_from_date_time =
CALCULATE(
MIN(dsgLoadBaseView[BookedOnDateTime]),
FILTER(
ALL(dsgLoadBaseView),
dsgLoadBaseView[CarrierId]=current_row_carrier_id
),
FILTER(
All(dsgLoadView),
dsgLoadView[BookedFrom]=current_row_booked_from
)
)
Var is_first_date = IF(first_booked_from_date_time=MIN(dsgLoadBaseView[BookedOnDateTime]),1,0)
RETURN
SUMX(dsgLoadBaseView,is_first_date)
With that being said, if I take out the BookedFrom bits (current_row_booked_from, etc.) the measure works and when alongside LoadNumber it returns a 1 or 0, denoting that the LoadNumber was or was not the first booking by the Carrier. I decided this wasn't the right path, though, due to that memory error. Also, summing up these 1's gets me duplicate bookings per Carrier per BookedFrom. In other words, a Carrier can book 2 loads via Manual at the same DateTime and, as those 2 rows would have 1's per the logic, that would add up to 2 which is a no-no.
THANK YOU
Seriously, to anyone who got this far! This problem has eaten up a ton of time for me, I've Googled relentlessly and I've watched countless YouTube videos. Thanks for your time!
I unable to provide a solution that would work because I don't have the data and its impossible to solve questions like these without the actual data, but I do know DAX so based on my experience here are my suggestions.
For measure IsFirstCarrierBookedFrom:
Substitute DISTINCTCOUNT with SUMX ( VALUES ( Table[CarriedID] ), 1 ) and see if that resulst in better performance
You are adding a column to dsgLoadView with RELATED, what is the cardinality of this table? Pay attention to these details and based on that supply a smaller table to ADDCOLUMNS and then use CALCULATE to compute BookedOnDateTime
Functions like NATURALINNERJOIN utilize the slower engine of DAX
You are probably applying a huge table T4 into the filter context with CALCULATE
For measure TotalFirstCarrierBooks:
You are probably iterating a huge tabls inside CALCULATE in the variable first_booked_from_date_time, try to change that to this:
VAR first_booked_from_date_time =
CALCULATE (
MIN ( dsgLoadBaseView[BookedOnDateTime] ),
dsgLoadBaseView[CarrierId] = current_row_carrier_id,
dsgLoadView[BookedFrom] = current_row_booked_from,
REMOVEFILTERS ( dsgLoadBaseView ),
REMOVEFILTERS ( dsgLoadView )
)
The RETURN part isn't working how you would expect it to, variables in DAX are constants, before RETURN the value of is_first_date is computed and is now a fixed value, nothing can change it, let's assume it is 10 then inside SUMX ( dsgLoadBaseView, is_first_date ) you are summing 10 for each row of the table dsgLoadBaseView
Basically, I’d like to get one entity totals, but calculated for another (but still related/associated!) entity. Relation type between these entities is many-to-many.
Just to be less abstract, let’s take Trips and Shipments as mentioned entities and Shipments’ weight as a total to be calculated.
Calculating weight totals just per each trip is pretty easy task. Here is a table of Shipments weights:
We place them into some amounts of trucks/trips and get following weight totals per trip:
But when I try to show SUM of Trip weight totals (figures from 2nd table) per each related Shipment (Column from 1st table), it becomes much harder than I expect.
It should look like:
And I can’t get such table within Power BI.
Data model for your reference:
Seems like SUMMARIZE function is almost fit, but it doesn’t allow me to use a column from another table than initialized in the function:
Additional restrictions:
Selections made by user (clicks on cells etc.) should not affect calculation anyhow.
The figures should be able to be used in further calculations, using them as a basis.
Can anyone advise a solution? Or at least proper DAX references to consider? I thought I could find a quick answer in DAX reference guide on my own. However I failed to find a quick answer.
Version 1
Try the following DAX function as a calculated column inside of your shipments table:
TripWeight =
VAR tripID =
RELATED ( Trips[TripID] )
RETURN
CALCULATE (
SUM ( Shipments[ShipmentTaxWeightKG] );
FILTER ( Shipments; RELATED ( InkTable[TripID] ) = tripID )
)
The first expression var tripID is storing the TripID of the current row and the CALCULATE function gets the SUM of all of the weight for all the shipments that belong to the current trip.
Version 2
You can also create a calculated table using the following DAX and create a relationship between the newly created table and your Trips table and simply display the weight in this table:
TripWeight =
GROUPBY (
Shipments;
Trips[TripID];
"Total Weight KG"; SUMX ( CURRENTGROUP (); Shipments[ShipmentTaxWeightKG] )
)
Version 3
Version 1 and 2 are only working if the relationship between lnkTrip and Shipment is a One-to-One relationship. If it is a many-to-one relationship, the following calculated column can be created inside of the Trips table:
ShipmentTaxWeightKG by Trip = SUMX(RELATEDTABLE(Shipments); Shipments[ShipmentTaxWeightKG])
hope this helps.
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.