Automatic Data Transfer to DAX - powerbi

I'm new in this so please pardon me for such a basic question.
I'm trying to pass some dates as filters to a measure.
I have a simple measure that counts opened items "last week".
The DAX I used:
OpenedLastWeek = CALCULATE(COUNTROWS(Table);Table[Created
Year]=2018;Table[Created Week]=45)
But there should be a way to pass values to the DAX code to replace manually entered "2018" and "45".
I tried Date Tables, extra tables with dates in it... I'm just confused.
Thanks.

In this case, I would probably use variables.
OpenedLastWeek =
VAR CreatedYear = --<Desired Year Calculation>--
VAR CreatedWeek = --<Desired Week Calculation>--
RETURN
CALCULATE(
COUNTROWS(Table);
Table[Created Year] = CreatedYear;
Table[Created Week] = CreatedWeek
)
Basically, you define the week and year how you'd like and then pass those variables into the CALCULATE function.

Thank you very much. It solved my problem.
For this case, I tried variables before but I was getting wrong results. I guess that was because I was using "CountA()" instead of "Countrows()".
Thanks again.
Have a nice day,
Eld

Related

SUMX in Stacked Column chart

I'm hoping for some help with my measures. My data is quite sensitive so it's not easy to share. I've attempted to mock up as sales so hope it makes sense.
https://1drv.ms/u/s!Ap6q8W-mvm27g-dWehgkV6-p33VVsA?e=bGj3nV
I have written a measure to count the number of resales by TransactionID. I then use a sumx to total the resales by Transaction ID.
I've added this measure to a matrix against Month for Banding and this shows the correct row and column totals.
In the FACT Salestable, I have a field 'Tier'. When I attempt to add 'Tier' to a stacked column chart using the same measures, the data is incorrect. I think it is because the sumx is losing the filter created in the first measure but I don't really understand how to rectify this and have been trying for days!
Is anyone able to identify where my measures are incorrect - Id like to try to understand what exactly I'm asking the measure to do so I know where I'm going wrong for future work! Any help would be very much appreciated - thank you!
MEASURE 1:
CountResales =
Var _Cust = Max (FACTSales[CustomerID])
Var _Date = CALCULATE(min(FACTSales[DateOrigSale]),ALLSELECTED(FACTSales),FACTSales[CustomerID]=_Cust)
Var _ID = CALCULATE(min(FACTSales[CohortID]),ALLSELECTED(FACTSales),FACTSales[CustomerID]=_Cust,FACTSales[DateOrigSale]=_Date)
Var _CountResales =
CALCULATE(DISTINCTCOUNT('DIMReSales transactions'[TransactionID]),
VALUES('DIMReSales transactions'[TransactionID]),'DIMReSales transactions'[CohortID]=_ID,
'DIMReSales transactions'[New Sale]=1)
Return
_CountResales
MEASURE 2:
SumResales = Sumx(values('FACTSales'),[CountResales])

Returning the first result of an table

I'm struggling within DAX to find a formula to return the first result. My table is as folllows:
The unique column is Dim_B_ID. So what I really would like to have is to return the first result of Amount ONLY for Dim_B_ID where the column IN is not blank. Im struggling cause I get answers like 3500 (total of the rows) and I can't seem to integrate IF(NOT(ISBLANK( funtion into this. So if someone has a solution for me, I would really appreciate it as I'm not so much of an expert in DAX.
You could try using the following measure:
FirstNonBlank =
FIRSTNONBLANKVALUE(
'Table'[Dim_Date_ID],
SUM( 'Table'[IN] )
)
When put together with Dim_B_ID in a table for example you could visualize FirstNonBlank for every "categorie" like so:
Used slightly modified sample data here:

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

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.

What would be the best way to dynamically use dates in filter function in DAX

I need to find workaround for SAMEPERIODLASTYEAR function since I was not able to make it work.
I can simply use FILTER function but having trouble how to dynamically get dates from the beginning of last year till today year back.
PrevYearPremium = CALCULATE(
[Total Premium],FILTER(dim_Date,dim_Date[Date] >= VALUE("2018-01-01")
&& dim_Date[Date] <=VALUE("2018-04-18"))
)
There are numerous functions like DATESBETWEEN, DATESINPERIOD, DATE. What would be the best way to dynamically use dates?
Try this out.. This will give you YOY compare..
CALCULATE([Total Premium]
,DATESBETWEEN('Dim_Date'[Date]
,FIRSTDATE('Dim_Date'[Date])
,LASTDATE('Dim_Date'[Date]))
,DATEADD('Dim_Date'[Date],-1,YEAR)