Power BI - Creating a calculated table - powerbi

I am creating a dashboard in Power BI. I have to report the executions of a process in a daily basis. When selecting one of these days, I want to create another calculated table based on the day selected (providing concrete information about the number of executions and hours) as it follows:
TABLE_B = FILTER(TABLE_A; TABLE_A[EXEC_DATE] = [dateSelected])
When [dateSelected] is previously calculated from the selected day as it follows:
dateSelected = FORMAT(FIRSTDATE(TABLE_A[EXEC_DATE]);"dd/MM/yyyy")
I tried a lot of alternatives as, for example, create individualy the year, month and day to later compare. I used the format in both sides of the comparation, but none of them works for me. The most of the cases it returns me the whole source table without any kind of filters. In other cases, it doesn't return anything. But, when I put a concrete day ...
TABLE_B = FILTER(TABLE_A; TABLE_A[EXEC_DATE] = "20/02/2019")
... it makes the filter correctly generating the table as I want.
Does someone know how to implement the functionality I am searching for?
Thanks in advance.

You're almost there Juan. You simply need to use dateSelected as a varialbe inside of your DAX query:
TABLE_B =
var dateSelected = FIRSTDATE(TABLE_A[EXEC_DATE])
return
FILTER(TABLE_A, TABLE_A[EXEC_DATE] = dateSelected)
Note that all my dates are formatted as Date so I didn't need to use a FORMAT function.
Here's the final result:
I admit that this behavior can be quite confusing! Here is a useful link that will help you understand Power BI's context:
https://community.powerbi.com/t5/Desktop/Filtering-table-by-measures/td-p/131361
Let's treat option 1 as FILTER(TABLE_A; TABLE_A[EXEC_DATE] = "20/02/2019") and option 2 as FILTER(TABLE_A; TABLE_A[EXEC_DATE] = [dateSelected]). Quote from the post:
In option 1, in the filter function, you are iterating
over each row of your 'Table' (row context). In option 2, because you
are using a measure as part of the filter condition, this row context
is transformed into an equivalent filter context (context transition).
Using variables (...) is very convenient when you want to filter
a column based on the value of a measure but you don't want context
transition to apply.

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

Power BI - Cell should be coloured once I select it

I have created a table in Power BI and it has deal names, deal stage, amount, sales region etc. Is there any way, where if I select the deal name - it should be coloured or highlighted and stay like that unless i remove it?
You can make it this way. Create an additional table with Label (column which you use to slice/select) don't add relationship!. For example, I want to highlight Day from the selected month (not filtering). We need to create a measure to check what is pickedup
WhatISelected = var __label = SELECTEDVALUE(MonthLabel[Calendar[Month]]])
return
if (SELECTEDVALUE(Query1[Calendar[Month]]]) = __label, 1,0)
Then go to conditional Formating:
Repeat for every column.
As you see below, I Select February.

How to select the last value of the day with DAX in Power BI

I am new to power BI and stuck with an issue. I have my model as follows:
Date Dimension
Measurement Fact
The date column in Date Dimension is link to measuredate in Measurement Fact
Below is a sample data:
NB: In edit query, I have changed the type of measuredate to Date only.
I have tried the measure below but it doesn't work the way I want. It will sum all the values of the day but what I want is the last value of the day:
day_fuel_consumption =
CALCULATE (
SUM ( measurement[measurementvalue] ),
FILTER (
measurement,
measurement[metername] = "C-FUEL"
&& measurement[measuredate] = MAX ( measurement[measuredate] )
)
)
My Goal is to get 29242, i.e the last value of the day. Remember that measuredate is a Date field and not Datetime (I changed to Date field so that my Year and Month filter can work correctly). I have changed the type in edit query.
Changing your measure to use a variable could be the solution:
DFC =
var maxDate = MAX(measurement[measuredate])
return
CALCULATE(
SUM(measurement[measurementvalue]),
measurement[measuredate] = maxDate
)
However, you should keep the datetime format for measureDate. If you don't want to see the time stamp just change the format I power bi. Otherwise power bi will see two values with max date and sum them, instead of taking the last one.
Well, if you want to avoid creating a measure, you could drag the fields you are filtering over to the visual filters pane. Click your visual, and scroll a tiny bit and you will see the section I am referring to. From there, just drag the field you are trying to filter In this case, your value. Then select "Top N". It will allow you to select a top (number) or bottom (number) based on another field. Strange enough, it does allow you to do top value by top value. It doesn't make sense when you say it out loud, but it works all the same.
This will show you the top values for whatever value field you are trying to use. As an added bonus, you can show how little or how many you want, on the fly.
As far as DAX goes, I'm afraid I am a little DAX illiterate compared to some other folks that may be able to help you.
I had to create two separate measures as shown below for this to work as I wanted:
max_measurement_id_cf = CALCULATE(MAX(measurement[measurementid]), FILTER(measurement, measurement[metername] = "C-FUEL"))
DFC =
var max_id_cf = [max_measurement_id_cf]
return
CALCULATE(SUM(measurement[measurementvalue]), measurement[measurementid] = max_id_cf)

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.

Obtain MAX of column and display for each row

I'm trying to obtain the MAX of a particular column in a Power BI Report and place this as a new Measure within each ROW of the same dataset. Please see the example below.
Is this possible in DAX and via DirectQuery/LiveConnection? The report is pointing to a tabular model but due to outside factors the measure must be created in the report.
Thanks
You can accomplish this a few ways. Essentially, you need override the filter context so that the MAX function isn't just running over whatever slice you're showing in the visual. Using CALCULATE or the iterator function MAXX, set the wrap the table in the ALL() function to override the context and calculate max over all rows.
= CALCULATE(MAX([Calendar`Year]), ALL('Smithfield_Fiscal_Calendar'))
or
= MAXX(ALL('Smithfield_Fiscal_Calendar'), [Calendar`Year])
To get the breakout by date, you'll need to include a Date table in your model. PowerBI makes this possible with a few different DAX options. As an example, go to your Model tab, click 'New Table' and put in the following expression:
MyCalendar = CALENDAR(DATE(2019,1,1), DATE (2019,1,10))
This is a little trivial -- you'd want to use a useful range of dates but this one matches your example above. Next, add a column to [MyCalendar]
CalendarMonthYear = month([date]) & "-" & year([date])
Go to your budget table and add a similar field
BudgetMonthYear = month([date]) & "-" & year([date])
Go into your Model view and create a relationship between CalendarMonthYear and BudgetMonthYear. This will associate every date in the date table with the particular budget row from your budget table.
Hope it helps.