i need your help calculating following:
based on the region and value(left table), i need to calculate the number of minium values displayed.
For instance, Value 5 is the minium value 3 times. Which function allow me to get that, i'm not able to find a right answer for days.
any idea is welcome!
many thanks
See data as example
Use the following DAX measure to achieve your goal:
CountMinValue=
VAR __value = SELECTEDVALUE( 'Table'[Value] )
Return CALCULATE( COUNT( 'Table'[Min Value]), 'Table'[Min Value] = __value )
Related
I replicated an issue I am having with the 'Adventure Works DW 2020' pbix file, so if my analysis seems a little out of context, please understand this example is not the true data I am working with. The pbix I used can be downloaded here:
https://drive.google.com/file/d/1vn6CluiE5rrAF3UjYPh5ejb93H2JX6IX/view?usp=sharing
My goal is to create a measure that can flag the subset of records that I want to use for a matrix visual.
I created the following measure with notes in the syntax:
VAR TABLEVAR =
SELECTCOLUMNS(
FILTER(
SUMMARIZE(
CALCULATETABLE(Sales/*Apply several filters to Sales table*/
,NOT Sales[CustomerKey] = -1
,Sales[orderdatekey] > 20180731
,Sales[orderdatekey] < 20190601
)
,[CustomerKey]/*Count the number of products per customer*/
,"Count",COUNT(Sales[ProductKey])
)
,[Count] > 1/*Only keep customers that bought more than 1 product*/
)
,[CustomerKey] /*Select the identifiers of the desired customers*/
)
RETURN
{
SWITCH(TRUE()
,SELECTEDVALUE(Sales[CustomerKey]) IN TABLEVAR/*Flag the customers that were identified in the previous table*/
,1,BLANK()
)
}
Now, in the PowerBI Matrix visual, this seems to work at first:
I had successfully flagged the desired output. Now I just have to filter for the 'Analysis' measure to be 'Not Blank', but then this happens:
Now removing that filter and going down a level:
So you see, the measure does not evaluate at the record level of the table. Does anyone understand the concept I am missing here? I have tried all kinds of different measures but it all comes down to the same problem about flagging different levels of analysis.
Ideally, the output would only include the following(circled in green):
These are the records that are within the date filters I put into the CALCULATETABLE() arguments.
Any help or insight with this problem would be greatly appreciated. Thank you
I'm not 100% clear what you're trying to do but please try the following and see if it helps.
Analysis =
VAR TABLEVAR =
SELECTCOLUMNS(
FILTER(
SUMMARIZE(
CALCULATETABLE(Sales
,NOT Sales[CustomerKey] = -1
,Sales[orderdatekey] > 20180731
,Sales[orderdatekey] < 20190601,
REMOVEFILTERS()
)
,[CustomerKey]
,"Count",COUNT(Sales[ProductKey])
)
,[Count] > 1
)
,[CustomerKey]
)
RETURN
//CONCATENATEX(TABLEVAR, [CustomerKey], ",")
SWITCH(TRUE()
,SELECTEDVALUE(Sales[CustomerKey]) IN TABLEVAR
,1,BLANK()
)
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
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] )
)
I tried to create a measure which should calculate the average number of the selected regions (via Chiclet Slicer).
If there is no filter set it should take the value of the whole country (CH - which is also part of this table).
In terms of logic, it should read something like this: If FilterIsActive take the average of median_R_mean of the selected regions else select the median_R_mean of "CH".
I have tried this :
RE =
IF(
ISFILTERED(Region[Bezeichnung]),
AVERAGE(COVID19Re_geoRegion_Last_value[median_R_mean]),
FILTER(
COVID19Re_geoRegion_Last_value,
"CH"
)
)
Unfortunately this approach does not work.
Can anyone help me on what possible approach I can follow here?
If I understand your issue correct, you are looking for a measure as below-
RE =
IF(
ISFILTERED(Region[Bezeichnung]),
AVERAGE(COVID19Re_geoRegion_Last_value[median_R_mean]),
AVERAGEX(
FILTER(
ALL(COVID19Re_geoRegion_Last_value),
COVID19Re_geoRegion_Last_value[geoRegion] = "CH"
),
COVID19Re_geoRegion_Last_value[median_R_mean]
)
)
Using DAX to identify first instance of a record
I'm faced with trying to identify the first instance in a database where someone (identified by the ID column) has purchased a product for the first time. It's possible for said person to purchase the product multiple times on different days, or purchase different products on the same day. I drummed up an excel formula that gets me there, but am having trouble translating into DAX.
=COUNTIFS(ID,ID,PurchaseDate,"<="&PurchaseDate,Product,Product)
Which results in the correct values in the "First Instance?" Column.
Ideally I won't have to hardcode values, as I would like to use the "Product" column as a parameter in the future. If there are other suggests aside from translating this in DAX, that would also be appreciated! (IE using filters, or other tools in PowerBI)
Thanks in advance!
This is very similar to an answer I gave to another question (which you can find here).
In that question, the request was to see a running count of rows for the given row's criteria (product, year, etc.). We can modify that slightly to get it to work in your problem.
This is the formula I provided in the answer I linked above. The basic concept is to use the EARLIER functions to get the value from the row and pass it into the filter statement.
Running Count =
COUNTROWS(
FILTER(
'Data',
[ProductName] = EARLIER([ProductName]) &&
[Customer] = EARLIER([Customer]) &&
[Seller] = EARLIER([Seller]) &&
[Year] <= EARLIER([Year])
)
)
What I would suggest for your problem is to create this as a TRUE/FALSE flag by simply checking if the running count is 1. This formula will evaluate to a Boolean flag.
First Instance =
COUNTROWS(
FILTER(
'Data',
[ID] = EARLIER([ID]) &&
[Product] = EARLIER([Product]) &&
[Purchase Date] <= EARLIER([Purchase Date])
)
) = 1