Here is one DAX solution:
There are some options in how you define the measures, so just treat this as a template
Assume the model contains these two tables:
Transactions
[TABLE="class: grid, width: 300, align: left"]
<tbody>[TR]
[TD]
Transaction ID[/TD]
[TD="align: right"]
Amount[/TD]
[/TR]
[TR]
[TD]T001[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD]T002[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD]T003[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD]...[/TD]
[TD="align: right"]...[/TD]
[/TR]
</tbody>[/TABLE]
NumTopTransactions
[TABLE="class: grid, width: 150"]
<tbody>[TR]
[TD]
NumTopTransactions[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD="align: right"]...[/TD]
[/TR]
</tbody>[/TABLE]
For this particular method to work, a dimension over which to count the TopN etc is needed. I've used Transaction ID.
Define these measures:
Code:
[B]Transaction Amount[/B] :=
SUM( Transactions[Amount] )
Code:
[B]TopN Selection[/B] :=
IF (
HASONEVALUE ( NumTopTransactions[NumTopTransactions] ),
VALUES(NumTopTransactions[NumTopTransactions] )
)
Code:
[B]TopN Transaction Amount[/B] :=
CALCULATE(
[Transaction Amount],
TOPN( [TopN Selection], ALLSELECTED(Transactions[Transaction ID]), [Transaction Amount] )
)
Code:
[B]NumBottomTransactions required to at least equal TopN[/B] :=
IF (
NOT( ISBLANK( [TopN Selection] ) ),
DISTINCTCOUNT( Transactions[Transaction ID] ) +1 -
COUNTROWS (
FILTER(
ADDCOLUMNS (
VALUES ( Transactions[Transaction ID] ),
"CumAmount",
CALCULATE (
[Transaction Amount],
FILTER (
ALLSELECTED ( Transactions[Transaction ID] ),
[Transaction Amount] <=
CALCULATE ( [Transaction Amount], Transactions[Transaction ID] = EARLIER( Transactions[Transaction ID], 2))
)
)
),
[CumAmount] >= [TopN Transaction Amount]
)
)
)
I've assumed that the TopN is calculated over the currently selected transactions (ALLSELECTED used in a couple of places).
The last measure
- Constructs a table containing cumulative transaction amount for each Transaction ID
- Counts the number of transactions with cumulative Transaction Amount (summing smallest to largest) >= TopN Transaction Amount
- Subtracts this count from the total number of transactions and adds 1. This gives the number of transactions required to just equal or exceed the TopN Transaction Amount.
You could handle this differently depending how you want boundary cases handled, for example instead count transactions with CumAmount <= TopN Transaction Amount...it will only matter in cases where the bottom transactions don't exactly equal to TopN Transaction Amount.
NumTopTransactions can be selected by slicer or otherwise filtered.