How to modify DAX ALLEXCEPT to allow filtering - dax

I have a Table called Tracking, as shown below:
Category
Item Id
Work Date
A
1
1/1/2020
B
1
2/1/2020
C
1
3/1/2020
D
1
4/1/2020
A
2
7/1/2020
B
2
8/1/2020
C
2
9/1/2020
D
2
10/1/2020
Using the below measure, I can get Maximum Work Date from the above Table.
maxWorkDate =
CALCULATE (
MAX ( 'Tracking'[Work Date] ),
ALLEXCEPT (
'Tracking',
'Tracking'[Item Id]
)
)
For Item 1, the maximum work date is 4/1/2020 and for Item 2, the maximum work date is 10/1/2020.
I want to get maximum work date by Excluding Category D. So, For Item 1, the maximum work date should be 3/1/2020 and for Item 2, the maximum work date should be 9/1/2020.
I would like to exclude category D within the measure or I can also exclude it in Visual Filters. Please advise. Thanks

It's possible to implement the measure with a filter expression 'Tracking'[Category] <> "D" like follows
maxWorkDate =
CALCULATE(
MAX( 'Tracking'[Work Date] ),
REMOVEFILTERS( 'Tracking' ),
VALUES( 'Tracking'[Item Id] ),
'Tracking'[Category] <> "D"
)

This is what FILTER is for:
maxWorkDate =
CALCULATE (
MAX ( 'Tracking'[Work Date] ),
FILTER (
ALLEXCEPT (
'Tracking',
'Tracking'[Item Id]
),
'Tracking'[Category] <> "D"
)
)

Related

Power Bi: how to parameterize Top N visual level filter [duplicate]

This question already has answers here:
Power Bi: Top N visual level filter as Measure
(2 answers)
Closed 6 months ago.
Since PowerBI don't support Top N filter on page level,
I want to use N as a parameter to change it at once per multiple visuals.
Is it possible?
P.S.In this video (9:15) solution for more complex case is provided.
In the end of this article sample file available
Using the sample dataset, insert a new parameter.
Add a measure as follows:
Measure =
IF(
SELECTEDVALUE('Product'[Product Name]) IN
SELECTCOLUMNS(
TOPN(
[Parameter Value],
ADDCOLUMNS( ALLSELECTED( 'Product'),"#Sales", [Sales Amount] ),
[#Sales]
),
"x",
'Product'[Product Name]),
1)
Every visual you want affected by the TopN should have this filter.
That's it.
From usability perspective it's preferable to return Sales Rank in measure.
Solution below is a copy/paste from SQLBI experts solution with minimal code changes ( ALLSELECTED ( 'Product'[Product Name] ) replaced by ALLSELECTED ( 'Product' ) ):
rnkSales =
IF (
ISINSCOPE ( 'Product'[Product Name] ),
VAR ProductsToRank = [TopN Value]
VAR SalesAmount = [Sales Amount]
RETURN
IF (
SalesAmount > 0,
VAR VisibleProducts =
FILTER( -- filters out data with no sales
CALCULATETABLE (
VALUES ( 'Product' ),
ALLSELECTED ( 'Product') -- Use this if VisualFilterTopN equivalent required
//ALLSELECTED ( 'Product'[Product Name] ) -- Original code - returns TopN per dimension
),
NOT ISBLANK( [Sales Amount] ) -- looks more universal then [Sales Amount]>0 (if calculation for Margin required, it could be negative)
)
VAR Ranking =
RANKX (
VisibleProducts,
[Sales Amount],
SalesAmount
)
RETURN
IF (
Ranking > 0 && Ranking <= ProductsToRank,
Ranking
)
)
)

How to calculate a measure based on a decreasing/increasing column's value in DAX

I have a Sales table and related dimension tables. MySales table contains columns : Week, StoreID, SalesSeasonID, ProductKey and metrics. My dimensions are related to sales table (Date,SalesSeason,Store,Product tables).
I need to find Sales Quantity (LastYear and LastSeason), as a measure
You can find a sample below:
My purpose is when user selected SaleseasonID[4] then it will return 2 as SalesQuantity.
How can I calculate this measure by DAX formula?
Try:
PYSales =
SUMX (
VALUES ( Table1[YearWeek] ),
CALCULATE (
SUM ( Table1[SalesQuantity] ),
ALL ( Table1[SalesSeasonID] ),
FILTER (
ALL ( Table1[YearWeek] ),
Table1[YearWeek] = EARLIER ( Table1[YearWeek] ) - 100
)
)
)
Worked example PBIX file, using your sample data: https://pwrbi.com/so_55703551/

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 Year over Year

I have been trying to get this DAX expression to show me cumulative searches for last financial year. Here is an example of the information;
Fiscal Week Fiscal Year Searches Brand
1 14 1000 Example1
1 15 1200 Example1
2 14 1000 Example1
2 15 1200 Example2
My formula below is working a little, but when I apply an slicers to the data it breaks in PowerBI. i.e. if I slice by another field, like brand.
Cum. Searches PY =
IF (
HASONEVALUE ( 'data'[Fiscal Year] ),
CALCULATE (
SUM ( 'data'[Searches] ),
FILTER (
ALL( 'data' ),
'data'[Fiscal Year.] = VALUES ( 'data'[Fiscal Year] ) - 1
&& CONTAINS(
VALUES ( 'data'[Fiscal Week] ),
'data'[Fiscal Week],
'data'[Fiscal Week] )
)
),
BLANK ()
)
I'd appreciate any pointers to where I'm going wrong? Thanks in advance.
I think it can be simplified, I dont understand the need for the HASONEVALUE or CONTAINS functions. I would use something like the following for your measure:
Cum. Searches PY:= CALCULATE(
SUM( Table1[Searches] ),
FILTER(
ALL( Table1[Fiscal Year] ) ,
Table1[Fiscal Year] = MAX( Table1[Fiscal Year] ) - 1
)
)
With that sample data above, this will produce results of:
Example1 2000
Example2 (Blank)
If that's not your expected result, then explain exactly what you want.

Resources