Tried to add new measures via PowerBI but the EARLIER functions seem not working properly.
The dataset including Year, Month, DealerName, MerchantType, Province, TotalApplications, and Requested Amount. I want to calculate the Total Applications based on DealerName and MerchantType respectively. Please see one of the functions below:
Merchant_TotalApp = CALCULATE(SUM('Requested Summary'[TotalApplications]), FILTER('Requested Summary','Requested Summary'[MerchantType] = EARLIER ('Requested Summary'[MerchantType]))))
The EARLIER ('Requested Summary'[MerchantType]) is underlined red and the error message is "EARLIER/EARLIEST refers to an earlier row context which doesn't exist". When hovering over to the error part, it showing "parameter is not the correct type".
Wondering how to solve the problem? is it related to the data type erro or anything. Thanks for any help!
EARLIER is rarely used in Measures.
It is used in an iterator function when you want to compare what's in the current row context to a prior row context.
In your current Measure, the FILTER function is not nested within a row context, so the EARLIER function has nothing to 'look back' to.
If what you are after are sums that pay attention only to Merchant type and ignore any other filters - you can get that with on of the ALL functions.
Merchant_TotalApp =
CALCULATE (
SUM ( 'Requested Summary'[TotalApplications] ),
ALLEXCEPT('Requested Summary','Requested Summary'[MerchantType])
)
Related
Working with basketball data, I'm trying to get the time on court for the players (there are some columns that have information about a player or players).
I tried to obtain the value with a calculated column, named "TimeOnCourt". The code works for most cases but there is a case that, due to a mistake in the data entry team, there are different values of the players columns for the same "TimeOnCourt" so, when I try to visualize the information, the data entry mistake comes out.
I guess I could use the column "Index" to add a piece of code to choose the MIN value for the "TimeOnCourt" column but, after trying some options, I don't know where to put it or if I have to change the full code.
I also tried with Test_Flags but not working for all cases (but could fix 2 of the 4 cases).
Add you the link with the pbix file and the Test_Flag measures I tried: Link to pbix file v3
And the image with the mistake marked. The expected time in the right visualization should be 0:40:00 instead of 0:43:03 (it's due to the duplicate in Full Quarter = 2Q and Time_Def = 0:04:00. This could happen again although I talked with them so the solution should be general, not filtering this specific case.
Problem
I'm trying to create a measure which can give me the primo date value for a given timeframe eg. start date of: current month, current year, YTD etc, I'm using calculation groups, but i've boiled the problem down to this.
In this example i'm tying to find the value of the stat date of current YTD-period, which should be 01/01/2022 :
primotest =
var primoDateValue = TOTALYTD(
FIRSTDATE( Dato[FuldDato])
, Dato[FuldDato]
)
//in my actual measure i have to filter the primoDateValue by [level2]-column, which i why it is important to include it in this example.
var randomMeasure =
CALCULATE(1
,'Dim funktionsbudgetter'[level2] = "this doesn't matter"
)
return
primoDateValue
for some unknown and really weird reason the randomMeasure interacts with the primoDateValue measure.
if 'Dim funktionsbudgetter'[level2] = "this doesn't matter" is included in randomMeasure. the result is incorrect
if I remove ''Dim funktionsbudgetter'[level2] = "this doesn't matter" the value is correct
My datamodel looks like this:
I'm guessing that i have somekind of problem with my datamodel. but no matter what that problem is. I can't think of a single scenario that causes an interaction between primoDateValue and randomMeasure.
I'm hoping that some of you can think up a scenario that would cause the weird interaction between the measures. so that I can figure out where the problem actually is.
okay so i figured out the problem or actually problems.
it turns out that it is a combination of using time intelligent functions eg. TotalYTD, calculation groups (CG) and composit models.
basically, you should be really careful when using CG and composit models.
in short, you can't use remote data with a local defined CG and vice versa. it will straight up ignore the CG item and just return the normal measure.
you can read more about it here: sqlbi - guide
I also found that the time intelligence function doesn't always play nice with composit models. which i also the problem in my "boiled-down" measure in my OP.
I don't know exactly why. it works in other scenarios.
for now i can just rewrite my measure to avoid TI functions
The documentation for COUNTAX (DAX) and COUNTX (DAX)
states that the second argument is an expression that is evaluated for each row.
See: https://learn.microsoft.com/en-us/dax/countax-function-dax
This is exactly what I need, but I cannot figure out what the 'expression' should look like.
The example given in Microsoft's documentation is this:
=COUNTAX(FILTER('Reseller',[Status]="Active"),[Phone])
But the second argument ([Phone]) does not look like an expression.
An expression in my expectation is something like "value > 3 AND value <= 10"
What kind of expression can be used here?
In the example [Phone] is the expression evaluated for each row in the resulting table. To clarify, since COUNTAX and COUNTX return the count of nonblank rows, the count of nonblank values in the [Phone] column is the computed expression. After the FILTER function is applied to the table, the expression would be equivalent to COUNT([Phone]) for this context. Using the Server Timings feature in Dax Studio the text representation of what is passed to the storage engine can be viewed. In the case of COUNTX you will see a query with IS NOT NULL in the WHERE clause for the column used as the expression, as would be [Phone] in this case, with the COUNT function selected since any rows with a blank [Phone] will have already been filtered out.
The statement below is an example query from the Server Timings feature in DAX Studio using the example measure from your question. As you can see, there are two filters in the WHERE clause. The first on the Status column, to return only rows that are active. The second is to eliminate null values in the Phone column. This leaves the COUNT aggregate function to count all rows that have an active Status and a non-blank value in the Phone column, which is equivalent to a count of the Phone column with the active Status. The query here is only a text representation of the requests sent to the storage engine, thus the syntax displayed won't be actual SQL, but will give you a better idea of how the DAX is being processed.
SET DC_KIND="AUTO";
SELECT
COUNT ( )
FROM 'Reseller'
WHERE
'Reseller'[Status] = 'Active' AND
'Reseller'[Phone] IS NOT NULL;
'Estimated size ( volume, marshalling bytes ) : 224012, 1392082'
usually in dax the term expression means Column or Formula, so you could use either a column or a formula in the countax/countx or in any other functions that are evaluated for each row (generally functions that end in x :D )
Thank you #userfl89, for the enlightening and analytic explanation.
I see now that <expression> is actually a WHERE clause. Now it starts making sense why Microsoft choosed to use the word 'expression' here.
Still, it all is full of contradictions.
The syntax for Count is
COUNT(<column>)
Then, [Phone], in COUNT([Phone]) is of type <column>. I assume this is also just a WHERE clause towards the storage engine?
Compare with COUNTAX(FILTER('Reseller',[Status]="Active"),[Phone]), where [Phone] is of type <expression>.
The real logic in COUNTAX is in fact the FILTER-function.
The <expression> is always a WHERE clause that but selects the non-blanc values. OK, it is an 'expression', but it is always the same one. Right?
I cannot but conclude that Microsoft's type system in their syntax description is a bit messy and makes it hard for me (as a starter on PowerBI) to make my own DAX queries based on their documentation.
Or they should come up with better examples to compensate for the confusing syntax descriptions
I have a dataset TransactionId, PolicyId, DebitnoteId and Amount. I need to add cumulative/rolling balances for the policy and debit note balances. I have illustrated what the PolicyID Rolling Balance and the DebitNote Rolling Balance should be using colors to display items which made up the balances in the image below. I am new to DAX and not able to get the balances working without using the Date Functions.
I have not tried it out but here's something that you should give a try:
PolicyID Rolling Balance = calculate(
sumx(values(policiID);[sum(amount) or lastnonblank(amount;0) I'm not sure :/]);
filter(all(Table) ; transactionID [<= or >=] MAx(transaticonID)))
or with earlier function in a calculated column instead of max or in maxx (instead of max: maxx(Table; transactionID[<= >=]earlier(transationID)))
I hope at least it gives you a little hint which is working.
The DebitNote Rolling Balance should be done similarly (with DebitnoteId).
and another note instead of sumx one could use groupby function too, I guess. Like this:
calculate(sum(amount); groupby(table; policyID); /\*filter like before*/)
Oh. And one more thing I used ';' because I have this in my locale settings or somewhere. If it's underscored then you should use ','.
//sorry for this "Answer". There's one answer I want to ask something but I can't, because I need some "reputation"...
I hope somebody can help me with some hints for the following analysis. The students may do some actions for some courses (enroll, join, grant,...) and also the reverse - to cancel the latest action.
The first metric is to count all the action occurred in the system between two dates - these are exposed like a filter/slicer.
Some sample data :
person-id,person-name,course-name,event,event-rank,startDT,stopDT
11, John, CS101, enrol,1,2000-01-01,2000-03-31
11, John, CS101, grant,2,2000-04-01,2000-04-30
11, John, CS101, cancel,3,2000-04-01,2000-04-30
11, John, PHIL, enrol, 1, 2000-02-01,2000-03-31
11, John, PHIL, grant, 2, 2000-04-01,2000-04-30
The data set (ds) is above and I have added the following code for the count metric:
evaluate
sumx(
addcolumns( ds
,"z+", if([event] <> "cancel",1,0)
,"z-", if([event] = "cancel",-1,0)
)
,[z+] + [z-])
}
The metric should display : 3 subscriptions (John-CS101 = 1 , John-PHIL=2).
There are some other rules but I don't know how to add them to the DAX code, the cancel date is the same as the above action (non-cancel) and the rank of the cancel-action = the non-cancel-action + 1.
Also there is a need for adding the number for distinct student and course, the composite key . How to add this to the code, please ? (via summarize, rankx)
Regards,
Q
This isn't technically an answer, but more of a recommendation.
It sounds like your challenge is that you have actions that may then be cancelled. There is specific logic that determines whether an action is cancelled or not (i.e. the cancellation has to be the immediate next row and the dates must match).
What I would recommend, which doesn't answer your specific question, is to adjust your data model rather than put the cancellation logic in DAX.
For example, if you could add a column to your data model that flags a row as subsequently cancelled, then all DAX has to do is check that flag to know if an action is cancelled or not. A CALCULATE statement. You don't have to have lots of logic to determine whether the event was cancelled. You entirely eliminate the need for SUMX, which can be slow when working with a lot of rows since it works row by row.
The logic for whether an action is cancelled or not moves to your source system (e.g. SQL or even a calculated column in Excel), or to your ETL (e.g. the Query Editor in Power BI) which are better equipped for such tasks. The logic is applied 1 time and then exists in your data model for all measures, instead of needing to apply the logic each time a measure is used.
I know this doesn't help you solve your logic question, but the reason I make this recommendation is that DAX is fundamentally a giant calculator. It adds things up. It's great at filters (adding some things up but not others), but it works best when everything is reduced to columns that it can sum or count. Once you go beyond that (e.g. wanting to look at the row below to adjust something about the current row), your DAX is going to get very complicated (and slow), whereas a source system or the Query Editor will likely be able to handle such requirements more easily.