Retrieving last message related to a specific status in Power BI - powerbi

I have a table called Sessions containing PC:s downloading software.
I want to create a new column or a measure that shows which version of the software the PC is downloading or has downloaded recently.
Software version can be found in the message at the start of the download.
My measure currently looks like this but in visuals it filters out the rows where the status is not "Start"
Result = CALCULATE(MAX(Sessions[Message]),
ALLEXCEPT(Sessions, Sessions[PC]), Sessions[Status]="Start")
(There is also a DateTime column in Sessions that can be used)

I solved this with a measure.
By using TOPN and filtering by dateTime i could return a single row.
By using MAXX on this row i got the correct SW
getLatestSW =
VAR SINGLE_ROW= TOPN(1,, FILTER(Sessions, Sessions[Status]="Start"),
Sessions[DateTime], DESC)
return MAXX(SINGLE_ROW,[Message])
This was also possible with LOOKUP.
getLatestSWLookUp =
VAR LASTID = MAXX(FILTER(Sessions, Sessions[Status]="Start"),
Sessions[dateTime])
return LOOKUPVALUE(Sessions[Message], SessionEvents[dateTime], LASTID)

Related

PowerBI: Aggregate Measure correctly by condition on DATEDIFF

I have the following Table:
BaseTable
It represents processes with a certain category.
And there is also a Date Table over column TIMESTAMP.
I would like to show a Measure based on another Measure that calculates the Date-Difference until the selected Date.
So first this is how I calculate the Date-Difference:
AGE =
VAR SELECTED_DATE = CALCULATE(MAX(DATUM[Date]), ALLSELECTED(DATUM))
VAR STARTDATE_PROCESS = Calculate(MAX(Workflow[MIN_TIMESTAMP]),DATUM[Date]<=MAX(DATUM[Date]), ALL(DATUM[Date]))
RETURN
DATEDIFF(STARTDATE_PROCESS,SELECTED_DATE,DAY)
Now I want to use a Measure which depends on the result of AGE, like
NEW = IF([AGE]<=3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
or
OLD = IF([AGE]>3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
The Measures AGE, OLD and NEW look like that with the Base Table:
Measures
As you can see the aggregation is not working correctly:
Result_Wrong
But it should be like that
Result_Correct
Any idea how to fix that?
Thank you!
So the problem is that the subtotal is calculated at a whole different context, and because your Age measure is based on the MAX(Workflow[MIN_TIMESTAMP]) that won't take into account that there can be multiple processes.
To do what you want, you need to change the New and Old measures to perform an aggregation per process and then return the result of that. Something like this:
New_agg =
VAR tbl = ADDCOLUMNS(CALCULATETABLE(VALUES(Workflow[Process]), ALL('Date')), "age", [Age], "count_process", CALCULATE(COUNT(Workflow[Process]), ALL('Date')))
RETURN SUMX(tbl, IF([age]<=3, [count_process]))
Demo File
Let me know if below solution is working
Unfortunately I am unable to generate the dummy data that you have been using, so Created my own data for developing the solution.
Now from this data I have calculated the difference of dates and put it as Age
Now to get the count of process for the condition like yours, I have created two formulas and the result is:
Logic I followed here is, instead of creating measure I have created columns and took the sum of those columns which will give the data you need as sum of those columns.
Column for New:
New = IF((Sheet1[Age]) > 20, 1,0)
Column for Old:
Old = IF((Sheet1[Age]) < 20, 1,0)
Now place both formulas in "Values" and take sum as the aggregation.
Final result is

Display Count of Users based on Multiple slicer values Power BI

I am working on a Viewership table which tells numbers of customers watches asset. There are two asset filters. One for watched and one for not Watched. Based on the asset filters, I need to display number of customers who watched the show & customers who didn't watched the show. Below is the method I have used.
First, created a new table "Asset":
This table contains unique assets and used it to create a slicers that affects DAX measure but does not affect the visual (table). Asset table disconnected from the Viewership table (no relationships).
Created the copy of the above table for not watched assets and named it as asset_1
below is the viewership table.
Created below measures to get the customers who viewed the selected asset (from first slicer) & he customers who not viewed the selected asset (from second slicer) &
Watched Status =
VAR Selected_Assets = ALLSELECTED(Asset[asset_id])
VAR Customer_Assets = VALUES(Viewership[asset_id])
VAR Assets_Watched = COUNTROWS(INTERSECT(Customer_Assets, Selected_Assets))
RETURN
IF(Assets_Watched > 0, "Watched", "Not Watched")
Not Watched Status =
VAR Selected_Assets = ALLSELECTED(Asset_1[asset_id])
VAR Customer_Assets = VALUES(Viewership[asset_id])
VAR Assets_Watched = COUNTROWS(INTERSECT(Customer_Assets, Selected_Assets))
RETURN
IF(Assets_Watched > 0, "Watched", "Not Watched")
I got below result
after applying below filters in the visual filter pane I can see the desired result.
Watched Status= "Watched",
Not Watched Status = "Not Watched"
Now I got a Requirement to show total number of customers (Instead list of customers) with above filter criteria. For e.g: In the above case customer count is 1.
I have tried below measure.
Customer Count = CALCULATE(DISTINCTCOUNT(Viewership[customer_id]),FILTER(Viewership,([Watched Status]= "Watched") &&left([Not Watched Status],3)="Not"))
The expected result is 1 (Customer ID: 4). But I am getting result as 2. Could someone help to identify the issue here?
What you can do is drag teh customer_id in the visual as column and ensure it is set to count distinct.
Select your visual, go the the vusialization panel under values you can see the column, click on arrowdown, popus will show:

Display Matched and Non Matched Values based on a slicer value Power BI

I am working on a Viewership table which tells which customer watches which asset. Based on the asset filter, I need to display the customers who watched the show & customers who didn't watched the show. below is my example table
If the asset_id selected as 1 in the slicer, the desired output will be as below
I have tried creating a cross-join table with asset_id and customer_id , but that approach taking much time with large data. Request the experts here to suggest the best optimal solution to achieve this.
First, create a new table "Asset":
This table contains unique assets, and we will use it to create a slicer that affects DAX measure but does not affect the visual (table). To achieve that, the Asset table must be disconnected from the Viewership table (no relationships).
In your viewership table, I just renamed "asset" to "asset_id", to be consistent:
Next, create a measure:
Status =
VAR Selected_Asset = SELECTEDVALUE(Asset[asset_id])
VAR Customer_Asset = SELECTEDVALUE(Viewership[asset_id])
RETURN
IF(Customer_Asset = Selected_Asset, "Watched", "Not Watched")
Result:
Slicer here is created from the "Asset" table, and table is a table visual with customer_id and asset_id from the Viewership table (set them as "don't summarize" values). I turned off "total", assuming you don't need it.
This design requires to set Asset slicer to "single selection" mode, to make sure that you are only getting one value from it. If you want the model to work with multi-select slicer, change DAX measure as follows:
Multi Status =
VAR Selected_Assets = ALLSELECTED(Asset[asset_id])
VAR Customer_Asset = SELECTEDVALUE(Viewership[asset_id])
RETURN
IF(Customer_Asset IN Selected_Assets, "Watched", "Not Watched")
Result:
Edit:
To make it work at the customer level:
Customer Status =
VAR Selected_Assets = ALLSELECTED(Asset[asset_id])
VAR Customer_Assets = VALUES(Viewership[asset_id])
VAR Assets_Watched = COUNTROWS(INTERSECT(Customer_Assets, Selected_Assets))
RETURN
IF(Assets_Watched > 0, "Watched", "Not Watched")
Result:
Explanation: store selected assets in a table variable. Then, store assets visible per customer in another table variable. Find an intersect of the two tables (what they have in common), and count intersect rows. If none - not watched, otherwise watched. If you want, you can actually display the number of movies watched (just return "Assets_Watched" instead of IF statement).

Previous month categorical value Power BI

I have the table which looks something like this. I am trying to find a way to find status change per an account, e.g. if the current month the status is Written off but it was Active last month, the tag should be Newly written Off. Is it feasible in Power BI? I found PREVIOUSMONTH but it deals only with measures, not categorical values like I have.
this seems like a trivial problem, but I found out, that it's not easily solvable in PowerBI.
Before showing the way I solve this, I would recommend you to solve it prior to loading data to PowerBI (ie. in the data source).
If that is not possible here's what you should do:
(recommended) Create T-1 data column == column, which has the previous date for comparison (for you, its previous month or date):
T-1 date =
VAR __acc_id = TABLE[account_id]
VAR __date = TABLE[date]
RETURN
CALCULATE(MAX(TABLE[date]),FILTER(ALL(TABLE), TABLE[account_id] = __acc_id && TABLE[date] < __date))
The filter part of calculation "returns" part of the table, which has the same account_id as current row and smaller date then current row. After that, we simply select the max date, which should be the previous one.
This way, we find the biggest previous date for each account.
If you know what the previous date is, feel free to skip this step.
Prior to the creation of the status column itself, I would create a calculated column, which contains the previous status. The column should be calculated like this:
t-1 status=
var __acc_id = TABLE[account_id]
var tdate = TABLE[T-1 date] //CREATED IN PREVIOUS STEP OR YOUR PREVIOUS DATE METRIC(LIKE dateadd or previousmonth function)
return
CALCULATE(firstnonblank(TABLE[status]), FILTER(ALL(TABLE), TABLE[account_id]=__acc_id && table[date] = tdate))`
With the previous status column, we now have the current and previous status columns, which should be enough to correctly label the "rows" with simple if statement.
The calculated column formula might look something like this:
status_label = if(TABLE[status] == "Written off" && TABLE[t-1 status] == "active", "newly written off", "something else").
If simple IF isn't enough, have a look at switch statement
This sequence of steps should solve your issue, but I have to admit, it's not performance efficient. It would be better to solve it in PowerQuery, but sadly, I do not know how. Any solution using PowerQuery would be highly appreciated.

DAX: How do i create a table for a burndown chart? What status was current on certain dates

I'm creating a dashboard for a sprint overview and want to visualize the progress of the team in a burndown chart. The chart should give information about the amount of user stories that are each new, active and closed. So that in the beginning all user stories are open, then they become active and in the end, there are no open and active stories left.
Now my problem is in modeling the data using DAX.
The data is stored in a big table with a row for each user story that contains all the information on that date. Now, if the information gets modified like in the event of changing the status from new to active or correcting a spelling mistake, the program just adds a new row with a new date.
like in here
The table I want should have the columns date, new, active, and closed. For the date column i have written the following code:
CALENDAR(
FIRSTNONBLANK(
CurrentIterations[StartDate];
CurrentIterations[StartDate] );
FIRSTNONBLANK(
CurrentIterations[FinishDate];
CurrentIterations[FinishDate])
)
But now, oriented on that dates i want the other columns to calculate themselves. In every row I want the amount of user stories in the original table that are active and the latest version on that date.
Examples:
Original table
Wanted table
Wanted burndown chart
Any help greatly appreciated!
Here's another approach.
Calculated table:
MaxRev = CROSSJOIN(VALUES(Test[Id]), VALUES(Test[Date]))
Add the following calculated columns to this table:
MaxRev = CALCULATE(MAX(Test[Rev]),
FILTER(Test, Test[Id] = MaxRev[Id] && Test[Date] <= MaxRev[Date]))
Status = LOOKUPVALUE(Test[State], Test[Id], MaxRev[Id], Test[Rev], MaxRev[MaxRev])
Then use this to create a new calculated table:
Wanted = SUMMARIZE(MaxRev, MaxRev[Date],
"New", CALCULATE(COUNT(MaxRev[Id]), MaxRev[Status] = "New") + 0,
"Active", CALCULATE(COUNT(MaxRev[Id]), MaxRev[Status] = "Active") + 0,
"Solved", CALCULATE(COUNT(MaxRev[Id]), MaxRev[Status] = "Solved") + 0)
Well, it isn't as beatiful, but it does the job. I've created an extra table, to get the last Rev per Id and Day. At least, that's what I thought you meant.
I'm open for better solutions, I'm convinced it can be done better/easier.
'Test' is your original table, make a link on Date to your already created table (Wanted) with the Date column.
Calculated table:
MaxRev =
SUMMARIZE(Test; Tabel[Date]; Test[Id]; "Max"; MAX(Test[Rev]))
Column New (add to table with single date column):
New =
CALCULATE (
DISTINCTCOUNT ( Test[Id] );
CALCULATETABLE (
FILTER (
CROSSJOIN ( 'MaxRev'; Test );
'MaxRev'[Id] = Test[Id]
&& 'MaxRev'[Date] = Test[Date]
&& 'MaxRev'[Max] = Test[Rev]
)
);
Test[State] = "New"
) + 0
Repeat this column, also for Active and Solved.
PBIX file