I use power bi Version: 2.112.603.0 64-bit (December 2022).In this version when im writing dax measure with switch function after giving the second result with the comma its asking third result.Its skipping 3 rd Value.What Im doing wrong here.
Intellisense isn't always correct. Just provide the correct number of arguments and the DAX engine will interpret them correctly.
The (switch function) has a minimum of 3 parameters that are Expression, Value, Result. It is in this situation like the IF function. But you can also add as many as you want other Value, Result pairs, in this case is more similar to the Case When statement.
To come back to your example, you could add other hasonevalue functions and the respective Result value and as last Parameter the else Value (if no Value is matched).
Related
I'm making a time-spending tracker based on the work I do every hour of the day.
Now, suppose I have 28 types of work listed in my tracker (which I also have to increase from time to time), and I have about 8 significance values that I have decided to relate to these 28 types of work, predefined.
I want that, as soon as I enter a type of work in cell 1 - I want the adjacent cell 2 to get automatically populated with a significance value (from a range of 8 values) that is pre-definitely set by me.
Every time I input a new or old occurrence of a type of work, the adjacent cell should automatically get matched with its relevant significance value & automatically get populated in real-time.
I know how to do it using IF, IFS, and IF_OR conditions, but I feel that based on the ever-expanding types of work & significance values, the above formulas will be very big, complicated, and repetitive in the future. I feel there's a more efficient way to achieve it. Also, I don't want it to be selected from a drop-down list.
Guys, please help me out with the most efficient way to handle this. TUIA :)
Also, I've added a snapshot and a sample sheet describing the problem.
Sample sheet
XLOOKUP() may work. Try-
=XLOOKUP(D2,A2:A,B2:B)
Or FILTER() function like-
=FILTER(B2:B,A2:A=D2)
You can use this formula for a whole column:
=INDEX(IFERROR(VLOOKUP(C14:C,A2:B9,2,0)))
Adapt the ranges to your actual tables in order to include in the second argument all the potential values and their significances
This is the formula, that worked for me (for anybody's reference):
I created another reference sheet, stating the types of work & their significance. From that sheet, I'm using either vlookup, filter, xlookup.Using gforms for inputting my data.
=ARRAYFORMULA(IFS(ROW(D:D)=1,"Significance",A:A="","",TRUE,VLOOKUP(D:D,Reference!$A:$B,2,0)))
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 question about MDX tuples, I would like to gain some insight on something that seems confusing to me.
Most of the literature I have read talks about tuples being a set of co-ordinates essentially pointing to a cell which contains a measure value. From what I understand a tuple is defined as containing only one distinct member from each dimension. Typically when writing queries we don't specify every member for every dimension we let SSAS engine use the default members and aggregate the measure data accordingly.
Straight out of the adventure works sample OLAP database (cube) "adventure works"
A super simple query that I understand represents a tuple:
SELECT
([Date].[Calendar Quarter of Year].&[CY Q3],[Measures].[Sales Amount]) --Tuple
ON COLUMNS
FROM [Adventure Works]
SS Management studio returns this result
No problem here the tuple specified by the &[CY Q3] member point to the cell containing the displayed measure amount. Clearly a tuple has been returned.
Typically though I use this sort of thing more often:
select
non empty ([Date].[Calendar].[Calendar Quarter],[Measures].[Sales Amount]) --Tuple??
ON COLUMNS
FROM [Adventure Works]
Which returns all the quarter totals across all years for said measure (not a great example but it's just an example):
I see this result as a set because more than one distinct member has been returned from the same dimension (date). In fact, by default all members are being returned if so how can it be a tuple?
So my question is this. The parenthesis around the "tuple" in the query above, indicate to me that I'm selecting a tuple, the query engine processes and a result is returned that to me looks like a set, not because more than one cell value is returned but because more than one member from the date dimension has been used.
The query indicates that a tuple is being selected, and the query engine seems to accept it as one however the result set, includes multiple members from the same dimension and corresponding cell values indicating to me that more than one tuple will be returned --> set.
Also, The query engine throws no error when I treat it as a set and use set functions on it:
select
nonempty({([Date].[Calendar].[Calendar Quarter],[Measures].[Sales Amount])}) --Set
ON COLUMNS
FROM [Adventure Works]
My question is this, Assuming I am correct and that the results do in fact represent a set (a set of tuples denoted by each distinct member instance), why does the query engine allow you to specify parenthesis indicating selection of a tuple to return something that is not a tuple?
This makes more sense to me :
SELECT
nonemptycrossjoin(
{[Date].[Calendar].[Calendar Quarter]}, --Set 1
{[Measures].[Sales Amount]} --Set 2
)
ON COLUMNS
FROM [Adventure Works]
At least this code reflects the result set that's returned Thoughts?
Or is it all just Analysis Services semantics?
Thanks
Unfortunately, MDX is an ambiguous language. From what I've understood, the () notation is a tuple or an operator precedence notation. And:
{...} , {...}
is actually a crossjoin:
{...} * {...}
Then when you specify a " level " where a set is expected, the level.members is defaulted. When you specify a tuple where a set is expected a singleton set is created with this only tuple. So:
( [level], [measures].[amount] )
is equivalent to:
crossjoin( [level].members, { [measures].[amount] } )
The only tuple (a member is a tuple) specified is [Measures].[Amount] which by the way does not use the () notation ;-)
You mention that you typically use this syntax - but I write mdx nearly every day and never use this syntax -
SELECT
NON EMPTY ([Date].[Calendar].[Calendar Quarter],[Measures].[Sales Amount]) ON COLUMNS
FROM [Adventure Works];
I'm a little surprised it runs as it seems to be indicating to analysis services to create a tuple from a level and a measure member.
MarcP mentions that mdx is ambiguous, but I'm more in favour of saying it can be written ambiguously - it unfortunately fails quite graciously most of the time and you get no numbers returned or the wrong numbers - I wish it threw more errors and enforced tighter syntax rules as this might make it more understandable.
Your original script I would just use the * operator rather than typing out the full crossjoin function when you need it - in your script it is much more readable to move measures onto the rows and delete that tuple? Like Marc mentions SSAS's implicit use of the MEMBERS function - I find things more readable to include it explicitly when it is being used:
SELECT
NON EMPTY
[Date].[Calendar].[Calendar Quarter].MEMBERS ON 0,
[Measures].[Sales Amount] ON 1
FROM [Adventure Works];
[Date].[Calendar].[Calendar Quarter]
As Marc mentioned this is actually the same as [Date].[Calendar].[Calendar Quarter].MEMBERS in this case MSDN is your friend (and for mdx unlike some other ms languages I find msdn very good) - here is the definition of the MEMBERS function:
https://msdn.microsoft.com/en-us/library/ms144851.aspx
telling you the return type:
Returns the set of members in a dimension, level, or hierarchy.
nonemptycrossjoin
You mentioned nonemptycrossjoin - this is not needed any more - just a simple crossjoin via * is all that is needed with the last 2 or three versions of SSAS.
I have an IF statement that says if AG, AL, AJ all have Yes in the cell then I want it to go to a lookup table to retrieve a result. I then want it to take that result and perform a calculation but I don't know how to write it.
If one of the three cells has a no in it, then I just want it to return the value in cell AK
Here is an example:
IF(AG="Yes", AI="Yes", AJ="Yes"), VLOOKUP(Payout Table!A:B,201, 2,True), (O7*H17*AK) False=AK
This is my first time trying to do this so if you can recommend a link I can go to too better understand the correct formula that would be great.
What you want here is the AND function. AND returns TRUE if all of its own arguments are TRUE. ie:
=IF(AND(AG1="Yes", AI1="Yes", AJ1="Yes"), VLOOKUP(Payout Table!A:B,201, 2,True)*(O7*H17*AK), AK1)
Note also that I have fixed your formula for making the VLOOKUP amount multiply against your other cells, and also that I have added row references to what you had there ("AG" is not a valid cell reference; replace "AG1" with the appropriate row number you want to check against).
You need to use AND as well as change the order of your VLOOKUP like below:
=IF(AND(AG="Yes",AI="Yes",AJ="Yes"),VLOOKUP(201,Payout Table!A:B, 2,True)*(O7*H17*AK), AK1)
I wonder if you can use two kind of data types in the DAX, IF formula.
I want to calculate the EPS value. If it's positive, I want to return the value.
If it's negative, I want to show "(d)" as deficit.
Example code:
=IF([EPS]<=0;"(d)";
IFERROR([EPS];BLANK()))
But I only get the following error message:
"The second and third arguments of function IF have different data types"
Is there a workaround of this or a any ideas how I can combine the text and numeric data type in the IF function?.
Power Pivot for Excel 2016 and in Power BI Desktop can have an IF() function with multiple return types. In 2013, this is not currently possible.
Try =IF([EPS]<=0,"(d)")
Note that no value has been specified for value_if_false. Therefore, the function returns the default, which is an empty string.