Power BI: How to pull in a column to an IF statement - if-statement

I am trying to create a measure that says (basically) if the value in a column doesn't exists, display "No", otherwise display the column. Currently, I can't get this to pull in the column. This is essentially what I'm trying to accomplish
Status = IF(Table[Column] = BLANK(), "No", Table[Column])
Currently, the only thing I can get to work is this:
Status = IF(CALCULATE(COUNTA(Table[Column])) = BLANK(), "No", "Yes")

Related

SELECTEDVALUE not returning expected value

I have created this table in Power BI:
DimProduct = UNION(
ROW("ProductId", 1, "Product", "AB","ProductType","A"),
ROW("ProductId", 2, "Product", "AC","ProductType","A"),
ROW("ProductId", 3, "Product", "AD","ProductType","A"),
ROW("ProductId", 4, "Product", "BB","ProductType","B"),
ROW("ProductId", 5, "Product", "BC","ProductType","B")
)
Then I created this table:
DimProductSelectedType =
VAR vSelectedProduct=
SELECTEDVALUE('DimProduct'[Product])
RETURN
ROW("Col1",vSelectedProduct)
I dropped DimProduct[Product] into a slicer
I dropped DimProductSelectedType into a list
I expect that when I pick one product from the product slicer, it will appear in the list. But the list always remains blank.
I had thought that SELECTEDVALUE would reflect any single value picked in a slicer
That's my immediate problem, and this can be summarized as
Table columns that use DAX calcs are evaluated at import time. Measure are evaluated at runtime
What I'm actually working towards is:
Pick a product in a slicer
Identify that products product type
Show visualisations comparing selected product compared to all other products within that product type
I actually started with this and now I'm working my way back.
DimProductTypeSelected =
VAR vSelectedProduct=
SELECTEDVALUE('DimProduct'[Product])
VAR vSelectedProductType=
SUMMARIZE(
FILTER(
ALLSELECTED(DimProduct),
DimProduct[Product]=vSelectedProduct
),
DimProduct[ProductType]
)
RETURN
FILTER(
ALLSELECTED(DimProduct),
DimProduct[ProductType]=vSelectedProductType
)
When you select item in a slicer it already creates fitler context on other visuals, so there is no need to overwrite it manually. What you would need to do is to create a fitler context for all products, e.g. by using ALL or ALLSELECTED for the comparators. But in order for this to be dynamic you need to use measures, tables are static and do not recalculate with UI changes.
To explain why you get your results with your current approach - tables are refreshed once and do not change afterwards. Therefore vSelectedProduct always returns blank, as at the moment of refreshing this table there is no filter context on the DimProduct[Product], so it defaults to blank
If you create a measure Measure = SELECTEDVALUE('DimProduct'[Product]) and put DimProduct[Product] into a slicer it will show the selected product for a single select and blank for multiselect (or all values)
EDIT
Regarding the second part of your question - selecting a product will create a filter context only for that product in the other visuals. Then you could use e.g.:
CountRowsPerType =
VAR vSelectedType =
SELECTEDVALUE('DimProduct'[ProductType])
RETURN
CALCULATE( COUNTROWS(DimProduct), ALL(DimProduct[Product]), 'DimProduct'[ProductType] = vSelectedType )
which will return count for all the products of that type even though the visual will only show the selected product

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.

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

Setting the Default Value of a Slicer on power bi

I want to set default filter of this week to slicer.However ,every answer use the option list.
if(weeknum(now())=weeknum([mydate]),"current week",[mydate]).
I selected the bar and choose start date with end date to filter date.On the previous way it's work on option-list but not on bar.
Any ideas?
I'd go about this in two ways.
Create a slicer which is used to calculate the week:
ThisWeek = if(weeknum(now) = weeknum(date), "Yes", "No")
It is however worth noting that if you have a data set that contains dates over several years it could show more data than required. In this case I would create the below column and use a Slicer based off of that.
ThisWeek = if(weeknum(now) = weeknum(date && year(now) = year(date), "Yes", "No")
You can then setup the interactions to filter your date slicer so the default range would be set as the value defined within "ThisWeek"
Alternately you could create a page filter which only shows records for this week. Again you would use the same format as above however there would be no need for the slicer.