Forum Discussion
Find the Second Highest Value
Hi All,
I am new to DAX and am having trouble pulling the second largest value for a filter function.
I am currently using the following the calculate the number of sales:
CALCULATE(SUM('Database Export'[Nb Sales]),(FILTER('Database Export', 'Database Export'[Weeks Since Launch]=MAX('Database Export'[Weeks Since Launch]))))Where "Weeks Since Launch" is just a standard integer.
However my data source has changed it's formatting slightly and I now need to take the second highest value from the weeks since launch and not the highest. Is there an easy way to pull this number? I'm assuming I'd need something that functions similarly to a 'LARGE' command in excel?
Thanks in Advance
7 Replies
- Phil_SeamarkMicrosoft Employee
Hi Petersfield
I'm wondering if we can take advantage of the RANK function and then look for items where RANK = 2 ???
Do you have some sample data we can try this on?
- PetersfieldRegular Visitor
Hey Phil!
Sure, here's an example below of one product for one territory. What I would be looking to do is display is the Sum of the second highest week since launches sales, and then if possible the percentage change between the second higest and third highest week since launch.
Thanks!
Date Platform Title Sales Territory Measure Name Sales Weeks Since Launch 20/03/2017 MS1 WDD2.1 United States WAU 42016 19 13/03/2017 MS1 WDD2.1 United States WAU 118464 18 06/03/2017 MS1 WDD2.1 United States WAU 114464 17 27/02/2017 MS1 WDD2.1 United States WAU 121696 16 20/02/2017 MS1 WDD2.1 United States WAU 136992 15 13/02/2017 MS1 WDD2.1 United States WAU 129920 14 06/02/2017 MS1 WDD2.1 United States WAU 159072 13 30/01/2017 MS1 WDD2.1 United States WAU 175328 12 23/01/2017 MS1 WDD2.1 United States WAU 183552 11 16/01/2017 MS1 WDD2.1 United States WAU 191936 10 09/01/2017 MS1 WDD2.1 United States WAU 208384 9 02/01/2017 MS1 WDD2.1 United States WAU 265792 8 26/12/2016 MS1 WDD2.1 United States WAU 316896 7 19/12/2016 MS1 WDD2.1 United States WAU 192544 6 12/12/2016 MS1 WDD2.1 United States WAU 104544 5 05/12/2016 MS1 WDD2.1 United States WAU 116736 4 28/11/2016 MS1 WDD2.1 United States WAU 135296 3 21/11/2016 MS1 WDD2.1 United States WAU 160672 2 14/11/2016 MS1 WDD2.1 United States WAU 128320 1 20/03/2017 P4S WDD2.1 United States WAU 49024 19 13/03/2017 P4S WDD2.1 United States WAU 129856 18 06/03/2017 P4S WDD2.1 United States WAU 130400 17 27/02/2017 P4S WDD2.1 United States WAU 148800 16 20/02/2017 P4S WDD2.1 United States WAU 167488 15 13/02/2017 P4S WDD2.1 United States WAU 146496 14 06/02/2017 P4S WDD2.1 United States WAU 184480 13 30/01/2017 P4S WDD2.1 United States WAU 199168 12 23/01/2017 P4S WDD2.1 United States WAU 213344 11 16/01/2017 P4S WDD2.1 United States WAU 235136 10 09/01/2017 P4S WDD2.1 United States WAU 240128 9 02/01/2017 P4S WDD2.1 United States WAU 312512 8 26/12/2016 P4S WDD2.1 United States WAU 377952 7 19/12/2016 P4S WDD2.1 United States WAU 245920 6 12/12/2016 P4S WDD2.1 United States WAU 146400 5 05/12/2016 P4S WDD2.1 United States WAU 163552 4 28/11/2016 P4S WDD2.1 United States WAU 192160 3 21/11/2016 P4S WDD2.1 United States WAU 224256 2 14/11/2016 P4S WDD2.1 United States WAU 175520 1 20/03/2017 iPC WDD2.1 United States WAU 3712 19 13/03/2017 iPC WDD2.1 United States WAU 10112 18 06/03/2017 iPC WDD2.1 United States WAU 11776 17 27/02/2017 iPC WDD2.1 United States WAU 12768 16 20/02/2017 iPC WDD2.1 United States WAU 15904 15 13/02/2017 iPC WDD2.1 United States WAU 17504 14 06/02/2017 iPC WDD2.1 United States WAU 17952 13 30/01/2017 iPC WDD2.1 United States WAU 21248 12 23/01/2017 iPC WDD2.1 United States WAU 23296 11 16/01/2017 iPC WDD2.1 United States WAU 33344 10 09/01/2017 iPC WDD2.1 United States WAU 33824 9 02/01/2017 iPC WDD2.1 United States WAU 40448 8 26/12/2016 iPC WDD2.1 United States WAU 50848 7 19/12/2016 iPC WDD2.1 United States WAU 40608 6 12/12/2016 iPC WDD2.1 United States WAU 36768 5 05/12/2016 iPC WDD2.1 United States WAU 45024 4 28/11/2016 iPC WDD2.1 United States WAU 45152 3 21/11/2016 iPC WDD2.1 United States WAU 224 2 - Phil_SeamarkMicrosoft Employee
Hi Petersfield
Sorry for the delay in replying
Please try adding the following 2 calculated columns to your table
Week Sales = CALCULATE(SUM('Table1'[Sales]),ALLEXCEPT('Table1',Table1[Date]))and
Ranking On Week Sales = VAR CurrentWeekSales = 'Table1'[Week Sales] RETURN COUNTROWS( FILTER( ALL(Table1[Week Sales]), 'Table1'[Week Sales] < CurrentWeekSales) ) + 1This assigns a ranking value to each week based on the sum of sales.
You can then build the following calculated measure on your table that use the above column (repeat for 3rd highest week) or just use the column above.
Sum of second highest week = CALCULATE( SUM('Table1'[Sales]), FILTER( 'Table1', 'Table1'[Ranking On Week Sales] = 2) )