DAX Index and Match function? - dax

I'm trying to use function LOOKUPVALUE, but I have more than one ID and return error "A table of multiple values was supplied where a single value was expected"
For example:
I need to fill all lines in "Measure_id"
Group_ID
ID
Desc
Measure_ID
112233
0
close
12345
112233
12345
open
12345
112233
0
close
12345
223344
0
close
23456
223344
0
close
23456
223344
23456
open
23456
112233
12345
open
12345

LOOKUPVALUE is not compatible in DAX measure. You can replace it with following.
_return =
VAR _1 =
MAX ( 'fact'[Group_ID] )
VAR _2 =
CALCULATE (
MAX ( 'fact'[ID] ),
ALL ( 'fact' ),
FILTER ( VALUES ( 'fact'[Group_ID] ), 'fact'[Group_ID] = _1 )
)
RETURN
_2

A simpler way of calculating this is by using ALLEXCEPT() in a measure like so:
Measure =
CALCULATE (
MAX ( 'Table'[ID] ) ,
ALLEXCEPT ( 'Table' , 'Table'[Group_ID] )
)
This yields the same result as #smpa01 but is a much more performant measure, around 30-40% quicker on this data set, according to DAX Studio analysis.

Related

Creating calculated table with MAX and MIN dates in DAX Power BI

I have the following table, imported in Power BI - QOL_Exp (see screenshot example below)
I need to create a calculated table which will filter out values, where Rating = 999 and,
at the same time, will pick only the highest and the lowest Date values from Date column, based on ClientID (see highlighted grey and peach colored areas).
I highlighted in red font - the values that I expect to see in my calculated table
For example, for ClientID = 3052 I will need the records where Date = 11/20/2020 (lowest date for this ClientID) and Date = 5/17/2021 (highest date for this ClientID)
For ClientID = 2666 I will not need the record where Rating = 999 (one of the conditions)
I managed to filter out (to exclude Rating = 999) but struggling with including only Max and MIN date in the new calculated table
This is my DAX:
QOL = CALCULATETABLE(QOL_Exp, QOL_Exp[Rating]<>999)
How should I modify it in order to only leave Max(Date) and Min(Date) records, based on ClientID?
UPD:
Based on the answer given, slightly updated (see below):
QOL =
FILTER (QOL_Exp, QOL_Exp[Rating] <> 999
&&
(( QOL_Exp[Date] = CALCULATE (MIN ( QOL_Exp[Date] ),
ALLEXCEPT(QOL_Exp,QOL_Exp[ClientID])))
|| QOL_Exp[Date] = CALCULATE (MAX ( QOL_Exp[Date] ),
ALLEXCEPT(QOL_Exp, QOL_Exp[ClientID]))))
QOL =
FILTER (
QOL_Exp,
QOL_Exp[Rating] <> 999
&& (
QOL_Exp[Date]
= CALCULATE (
MIN ( QOL_Exp[Date] ),
FILTER (
QOL_Exp,
QOL_Exp[Rating] <> 999
&& QOL_Exp[ClientID] = EARLIER ( QOL_Exp[ClientID] )
)
)
|| QOL_Exp[Date]
= CALCULATE (
MAX ( QOL_Exp[Date] ),
FILTER (
QOL_Exp,
QOL_Exp[Rating] <> 999
&& QOL_Exp[ClientID] = EARLIER ( QOL_Exp[ClientID] )
)
)
)
)

Created table in DAX, but can't extract the correct value

I'm attempting to create a measure to give me the rank of a Salesman by Sales. I'll use the measure in a table where one of the columns is Salesman, so there should be the appropriate row context applied. But using
RETURN Calculate( MAXX (RankTable, [Rank] ) )
just gives me a value of 1 for every broker. I can't figure out what to used to just pull out the value for Rank calculated in RankTable. How do I do that?
SumSales = sumx( SalesData, [Sale])
----------
SalesRank =
VAR SummaryTable =
ADDCOLUMNS(
SUMMARIZE( SalesData, [Salesman] ),
"Sales", [SumSales]
)
VAR RankTable =
ADDCOLUMNS(
SummaryTable,
"Rank", RANKX( SummaryTable, [Sales])
)
RETURN
Calculate( MAXX (RankTable, [Rank] ) )
I know that RankTable is correct, since DAX Studio give me this result:
Salesman Sales Rank
A 907 1
B 600 3
C 900 2
D 500 4
Here's code for measure mentioned in comments:
Priced. =
COUNTROWS(
FILTER( 'Cases',
[Date Initiated] >= [MinDate]
&& [Date Initiated] <= [MaxDate]
&& not ISBLANK( [Date To Pricing] )
)
)
I think you might be overcomplicating things, this should work:
SalesRank = RANKX(ALL('Salesdata'[Salesman]), [SumSales])
Reason you're getting 1 for every line is because in a visual the measure is calculated in the context containing a single salesman. So you need to remove this filter using the ALL() function.

Count how many sub-activities were created based on an activity

I have a dimension that stores workflows(cases, subcases). I would like to do a count of how many subcases are created for each case.
Workflow Dimension
Workflow
------------------------------
Case Number WorkflowType
------------------------------
10 Case
20 Case
30 Case
20-1 Subcase
20-2 Subcase
20-3 Subcase
10-1 Subcase
The desire output I would like is, for every case count how many subcases were created.
Workflow
------------------------------------------------
Case Number WorkflowType CountOfSubcases
------------------------------------------------
10 Case 1
20 Case 3
30 Case 0
------------------------------------------------
Total 4
I have a current dax measure that works, but the total at the bottom does not show when looking at multiple rows, only display when one case is selected.
Total Subcases =
VAR CC = FIRSTNONBLANK ( Workflow[Case Number], 1 )
RETURN
COUNTX (
FILTER (
ALL( Workflow ),
SUBSTITUTE ( Workflow[Case Number], RIGHT ( Workflow[Case Number], 2
), "" )
= CC
&& Workflow[WorkflowType] = "SubCase"
),
Workflow[WorkflowID]
)
If anybody could help me tweak my measure or present with a new measure, that would be great.
Note: I'm pointing my report to Analysis Services.
Thanks in advance.
You can fix your measure as follows:
Total Subcases = 0 +
COUNTX (
FILTER (
ALL( Workflow ),
SUBSTITUTE ( Workflow[Case Number], RIGHT ( Workflow[Case Number], 2 ), "" )
IN VALUES( Workflow[Case Number] )
&& Workflow[WorkflowType] = "SubCase"
),
Workflow[WorkflowID]
)
The VALUES function returns a list of all the values in the current filter context instead of just the one you were picking before.
Note: To make things easier to work with, I'd suggest splitting the Case Number column into two columns in the query editor stage. Then you don't have to work with all the string manipulation.
Edit: Note that x IN <Table[column]> is equivalent to the older CONTAINS syntax:
CONTAINS(Table, [column], x)
So if you can't use IN then try this formulation:
Total Subcases = 0 +
COUNTX (
FILTER (
ALL( Workflow ),
CONTAINS(
VALUES( Workflow[Case Number] ),
Workflow[Case Number],
SUBSTITUTE ( Workflow[Case Number],
RIGHT ( Workflow[Case Number], 2 ), "" )
)
&& Workflow[WorkflowType] = "SubCase"
),
Workflow[WorkflowID]
)

Powerpivot: Retrieve max value for a group in a related table

I have 2 tables with a one-to-many relationship.
-TableGroup: table with groupletter
-TableAll: table with unique identifier, groupletter, a date
Problem: I want to get the max value of the date from TableAll into a new column in TableGroup. See below.
Question: What is the formula for column MAXdate?
TableAll:
ID | Group | date
1 A 4/01/2017
2 A 2/10/2016
3 A 2/06/2016
4 B 2/12/2016
5 B 15/12/2016
6 B 2/03/2017
7 C 5/02/2016
8 C 16/01/2016
TableGroup:
Group | MAXdate
A 4/01/2017
B 2/03/2017
C 5/02/2016
The below formula doesn't work:
See here
The answer is:
CALCULATE (
MAX ( TableAll[Date] ),
FILTER ( TableAll, TableAll[Group] = EARLIER ( TableGroup[Group] ) )
)
Try:
CALCULATE (
MAX ( TableAll[Date] ),
FILTER ( TableGroup, TableGroup[Group] = EARLIER ( TableGroup[Group] ) )
)
How it works:
EARLIER ( TableGroup[Group] ) expression essentially means "current row". Filter function goes row by row over TableGroup table, filters it by current row's group, and then finds max date for that group.

dax if column is null or blank how can I set my as result previous column

currently my dax formula is =pathitem([hierarchy path]), 3) for column3 but I want to add formula that if column is blank or null get value from column2. How can I write dax formula for that?
You could use something like this:
=
IF (
ISBLANK ( PATHITEM ( [Hierarchy Path], 3 ) ),
[Column3],
PATHITEM ( [Hierarchy Path], 3 )
)
Column3 would have to be Data Type Text.

Resources