12-04-2018 01:46 AM - edited 12-04-2018 01:47 AM
I am fairly new Power BI user, currently working on interesting piece of DAX logic, which should not cause too much hasle, but I feel like I missing something in my code and cannot find out what exactly.
I have data on client movement through different sales stages which structured like this:
I have to represent this data by slowing the max status the client has achieved for any selected date range. E.g. if I select 12.02 to 12.03, I would want the matrix table to add a count to Registration counter like this:
Here is the current code that I tried using:
Measure := COUNTROWS ( FILTER ( 'Raw LH', GROUPBY ( FILTER ( ADDCOLUMNS ( SUMMARIZE ( CALCULATETABLE ( 'Raw LH', FILTER ( DateTable, DateTable[Date] >= MIN ( DateTable[Date] ) ), FILTER ( DateTable, DateTable[Date] <= MAX ( DateTable[Date] ) ) ), 'Raw LH'[LeadId], 'Raw LH'[NewStatus], 'Raw LH'[CreatedDateTime], 'Raw LH'[Id] ), "MaxDate", CALCULATE ( MAX ( 'Raw LH'[CreatedDateTIme] ), FILTER ( 'Raw LH', 'Raw LH'[LeadId] = EARLIER ( 'Raw LH'[LeadId] ) ) ) ), 'Raw LH'[CreatedDateTIme] = [MaxDate] ), 'Raw LH'[Id] ) ) )
In this code, If I understand correctly, I am selecting the data for the selected range, then grouping on the basis of LeadId, interaction Id, InteractionTime and the newStatus. I am also adding another column for the max date. I then select only the rows with max date, and group again by unique event identifier (Id).
As much as I have tested this code, the relative date filtering seems to be working, so does grouping and selecting the latest, thus the maximum, interaction, however when run this measure in the matrix table, it adds count for all interactions during the select time period.
I hope this makes sense and I would greatly appreciate if you could help me with this. I feel like I am so close to getting it right, but as much as try I can't crack this.
Solved! Go to Solution.
12-20-2018 04:07 AM - edited 12-20-2018 04:08 AM
I have found solution to my problem. Here is the code:
CountOfMaxStatus = COUNTROWS( FILTER ( TestData; TestData[TimeStamp] = CALCULATE ( MAX ( TestData[TimeStamp] ); FILTER(ALL(TestData);TestData[Client] = EARLIER ( TestData[Client])); FILTER(ALL(TestData);TestData[TimeStamp]>=MIN(DateTimeDimension[TimeStamp])); filter(ALL(TestData);TestData[TimeStamp]<=MAX(DateTimeDimension[TimeStamp])) ) ))
Here is link to the pbix file if you want to see it in the action: http://www.filedropper.com/maxstatusmeasure
12-04-2018 03:40 PM
For being a "fairly new Power BI user" you have here a pretty scary-looking, big-ass measure, dude
I haven't had much time but a couple of things stand out:
1. Although what you do seems logical overall, I believe the code could be done considerably leaner.
2. What is the point of the CALCULATETABLE() as first argument of SUMMARIZE? Why not just 'Raw LH'?
3. What is the point of the GROUPBY? By using it the result of the code you show above will just be a list of Ids
4. How exactly do you go about setting up the matrix with the measure? The issue might be there. (If you can provide the actual Power BI file including the reports rather than just the excel with, it would be useful)
5. The outermost FILTER(), does it not yield an error at execution time? You are passing on a table as filter argument.
Hopefully tomorrow I will have some more time.
12-04-2018 09:26 PM
You maybe take a look at these two Quick Measures as I think you want something like them.
By the way, for CreatedDateTime is a datetime column with different time,
and Date table is only a datetime column with 12:00:00 AM.
So you‘d better use this formula to create a new CreatedDateTime
New CreatedDateTime = 'Raw LH'[CreatedDateTime].[Date]
then use this new date column for calculation.
Hope these can help you.
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
12-05-2018 02:17 AM
@v-lili6-msftthanks for attempt, but these solutions do not fix the issue.
@AlBwith regards to your questions:
1. I agree that code which I provided is bloated, because I am trying to stitch together measure that is doing its job.
2. Reason for doing CALCULATETABLE() is to filter the selected date range only
3. GROUPBY() was my attempt to isolate unique eventId' s
4. I see what you mean, and this actually might be the root of the problem, please see below.
I tried to simplify the task, and ignore the relative date filtering for a while. I created a new table which just lists the unique status change Ids for the whole period, and then created a connection with the main datatable:
Testing = SELECTCOLUMNS ( FILTER ( ADDCOLUMNS ( SUMMARIZE ( ExampleData, ExampleData[LeadId], ExampleData[Id], ExampleData[CreatedDateTime] ), "MaxDate", CALCULATE ( MAX ( ExampleData[CreatedDateTime] ), FILTER ( ExampleData, ExampleData[LeadId] = EARLIER ( ExampleData[LeadId] ) ) ) ), ExampleData[CreatedDateTime] = [MaxDate] ), "Id", ExampleData[Id] )
I then created a measure for exactly the same code as above, but wrapped it in the COUNTROWS(). I then created the table of statuses and inserted both the measure and count of Testing[Id] as values. The output for both variants of the same code are different.
I don't understand how can this be, because the "Count of Id" is showing the correct output, but "Measure" is showing the wrong one, even though the both use exactly the same code! I feel like I am missing something very obvious here, but I don't know what. ;/
Here is link to the model: http://www.filedropper.com/example_20
12-05-2018 03:24 AM
Try this. If I've understood what you are looking for I think it might work. Set up the matrix as you just explained. You could also use date on a slicer. This measure will not show anything for the Total as it is now because of the SELECTEDVALUE but if it works we can fix that, if necessary, later.
Measure = COUNTROWS ( FILTER ( CALCULATETABLE ( FILTER ( 'Raw LH'; CALCULATE ( MAX ( 'Raw LH'[CreatedDateTime] ); ALLEXCEPT ( 'Raw LH'; 'Raw LH'[LeadId] ) ) = 'Raw LH'[CreatedDateTime] ); ALL ( 'Raw LH'[NewStatus] ) ); SELECTEDVALUE ( 'Raw LH'[NewStatus] ) = 'Raw LH'[NewStatus] ) )
12-05-2018 03:52 AM
On the measure in my previous post, the innermost FILTER already gets you a table with only the latest NewStatus per LeadID.
The rest is to make it work the way you want it on the matrix. The tricky part there is that placing NewStatus in the rows is affecting the table when you are doing your calculations and you would want it only to take effect at the end. I mean, first you want to select the latest NewStatus for each LeadID in the period without the filter on NewStatus and then, only then, you want the filter on NewStatus to act to count only the rows with the value you are interested in (Rejected, Qualified...)
Regarding the date filtering. Why don't you do it with a slicer on Date??
It is true and interesting what @v-lili6-msft mentions. Your date column includes relevant time info (not just 00:00:00 as in most cases). I could not create a relationship with a standard date table, I suspect precisely because of that. It is the first time I am coming across this, so best to refer to what @v-lili6-msft is suggesting. What I did was create an additional column with only the date (no time) in 'Raw LH' and create the relationship with 'Date' through that.