Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
algunn14
Frequent Visitor

Calculate Median Revenue Per Customer where Category = X

Hi All,

 

I am trying to calculate median donation amount per donor for selected time period where the category = B using DAX.

 
donation_datecustomer_idcategorydonation_amount
9/15/202141A26
9/22/20217A55
10/1/202141B10
10/1/202111B60
10/4/20211B52
10/7/20217B69
10/12/20213B89
10/12/20219B49
10/15/202115B26
10/18/202127B32
10/21/202133B100
11/1/202141B37

 

Since median requires a column or table, I created the calculated column

CatB Revenue = if( 'donors'[category] = "B", 'donors'[donation_amount])
 
I then created the following measure, and placed it on a Card
Median Donation Amount = MEDIAN('donors'[CatB Revenue])
 
When filtering the date from 9/15/2021 - 10/22/2021 the calc seems to work
algunn14_0-1656608226487.png

 

However, when I expand the end date to 11/1/2021 the median is not returning what I want. You can see in the table on the left (screenshot below) the median is 50.5, however my desired output is median for the table on the right, where we would aggregate revenue by donor_id and find the median. I'm sure the solution is simple, but after searching through the forums, and trying several different calcs I have had no luck. Any assistance is greatly appreciated! 

algunn14_2-1656609699655.png

 


 

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@algunn14,

 

Try these measures. This approach is dynamic, allowing you to specify the category via a slicer instead of hardcoding the category in a calculated column. The second measure creates a virtual table with the sum of each donation amount by customer id, and then MEDIANX iterates this virtual table, returning the median.

 

Sum Donation Amount = SUM ( donors[donation_amount] )
Median Donation Amount = 
VAR vTable =
    ADDCOLUMNS (
        VALUES ( donors[customer_id] ),
        "@DonationAmount", [Sum Donation Amount]
    )
VAR vResult =
    MEDIANX ( vTable, [@DonationAmount] )
RETURN
    vResult

 

DataInsights_0-1656682217087.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
DataInsights
Super User
Super User

@algunn14,

 

Try these measures. This approach is dynamic, allowing you to specify the category via a slicer instead of hardcoding the category in a calculated column. The second measure creates a virtual table with the sum of each donation amount by customer id, and then MEDIANX iterates this virtual table, returning the median.

 

Sum Donation Amount = SUM ( donors[donation_amount] )
Median Donation Amount = 
VAR vTable =
    ADDCOLUMNS (
        VALUES ( donors[customer_id] ),
        "@DonationAmount", [Sum Donation Amount]
    )
VAR vResult =
    MEDIANX ( vTable, [@DonationAmount] )
RETURN
    vResult

 

DataInsights_0-1656682217087.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@DataInsights this was very helpful, I really appreciate your support!

 

Would it be possible to include the CatA / CatB within the calc instead of using a slicer?

@algunn14,

 

Glad to hear that works. Here's a measure that allows you to hardcode the category:

 

Median Donation Amount Cat B = 
VAR vTable =
    CALCULATETABLE (
        ADDCOLUMNS (
            VALUES ( donors[customer_id] ),
            "@DonationAmount", [Sum Donation Amount]
        ),
        donors[category] = "B"
    )
VAR vResult =
    MEDIANX ( vTable, [@DonationAmount] )
RETURN
    vResult




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.