Forum Discussion

cursty's avatar
cursty
Frequent Visitor
1 year ago
Solved

Calculations with virtual table

Hello everyone,

 

I am working with expenses dataset. This dataset contains 4 columns: the unique id, item id (which can appear multiple times in case few payments have been made for the same item), paid amount (amount paid for unique id on a given date) and payment date.

Last week lbendlin helped me to create virtual tables for total amount calculations
This time, I would like to create a table (or plot) that displays the individual expense, its total amount, and its corresponding percentile. 

 

I started working on that using below percentile measure (orignally created by OwenAuger  ), but it seems that the total number of expenses in range (which is important in this case) is not calculated properly.

Here's the measure:

Perc_Rank =
var maxdt = max('Calendar'[Date])
var mindt = edate(maxdt,-36)+1
var items = ADDCOLUMNS(VALUES(Expenses[item_id]),"total_amount",CALCULATE(sum(Expenses[paid_amount]),Expenses[payment_date] IN CALENDAR(mindt,maxdt)),"total_items",calculate(DISTINCTCOUNT(Expenses[item_id]),Expenses[payment_date] IN CALENDAR(mindt,maxdt)))
var RankArgument = MAXX(items, [total_amount])
var TotalCount = MAXX(items, [total_items])
var NumberOfLess = FILTER(items, [total_amount] < RankArgument)
var NumberOfGreater = FILTER(items, [total_amount] >= RankArgument)
var RankLower = COUNTROWS(NumberOfLess)
var NumberLower = MAXX(NumberOfLess, MAXX(items, [total_amount]))
var NumberUpper = MAXX(NumberOfGreater, MAXX(items, [total_amount]))
var PercentRankArgumentRank = RankLower + 1
var InterpolationFraction = DIVIDE(RankArgument - NumberLower, NumberUpper - NumberLower)
var RankInterpolated = RankLower + InterpolationFraction * (PercentRankArgumentRank - RankLower)
var PercentRankOutput = DIVIDE(RankInterpolated - 1, TotalCount - 1)
RETURN PercentRankOutput

 

That's the output table:

 

And here's the output of my TotalCount variable

 

I would appreciate any help in this matter.
Many thanks in advance

  • Anonymous's avatar
    Anonymous
    1 year ago

    Thanks for the reply from lbendlin and Greg_Deckler, please allow me to provide another insight.
    Hi cursty ,

    Please try the following dax.

    Perc_Rank = 
    VAR _tb = CALCULATETABLE(SUMMARIZECOLUMNS('Expenses'[item_id],"Expense",[Expenses]),ALL())
    VAR _expenses = VALUE([Expenses])
    VAR _lower = IF(COUNTROWS(FILTER(_tb, [Expense]<_expenses))=BLANK(),0,COUNTROWS(FILTER(_tb, [Expense]<_expenses)))
    VAR _higher = IF(COUNTROWS(FILTER(_tb, [Expense]>_expenses))=BLANK(),0,COUNTROWS(FILTER(_tb, [Expense]>_expenses)))
    RETURN
    DIVIDE(_lower,_higher+_lower,0)


    The result is as follows, hopefully it will meet your needs.


    Please see the attached pbix for reference.

    Best Regards,
    Dengliang Li

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

7 Replies

  • What is your expected result and why are you not using the RANK function?

    • cursty's avatar
      cursty
      Frequent Visitor

      I overcomplicated this formula. I was trying use RANKX, but I've been getting 1 for every item on the list. I am actually looking for the equivalent of the Excel PERCENTRANK.INC formula. My expected output is shown below:

       

      Item IDAmountPerc
      item-1130050%
      item-22000%
      item-34500100%
  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks for the reply from lbendlin and Greg_Deckler, please allow me to provide another insight.
    Hi cursty ,

    Please try the following dax.

    Perc_Rank = 
    VAR _tb = CALCULATETABLE(SUMMARIZECOLUMNS('Expenses'[item_id],"Expense",[Expenses]),ALL())
    VAR _expenses = VALUE([Expenses])
    VAR _lower = IF(COUNTROWS(FILTER(_tb, [Expense]<_expenses))=BLANK(),0,COUNTROWS(FILTER(_tb, [Expense]<_expenses)))
    VAR _higher = IF(COUNTROWS(FILTER(_tb, [Expense]>_expenses))=BLANK(),0,COUNTROWS(FILTER(_tb, [Expense]>_expenses)))
    RETURN
    DIVIDE(_lower,_higher+_lower,0)


    The result is as follows, hopefully it will meet your needs.


    Please see the attached pbix for reference.

    Best Regards,
    Dengliang Li

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.