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
mattramirez2020
Helper II
Helper II

Sales and Returns in One Table

Hi! I have a dataset with each row representing a single sale. Each row has a sale date and a return date (if applicable). I am really struggling to generate a sales measure that generates the sales from a month and the returns that happened in that same month (March 2020 for example). I have been able to get this to work by duplicating the table and showing sales on one table and returns on another but I feel like there has to be a better way. 

 

Any help would be appreciated!!!

 

Sales and Returns Table:

Purchase_IDPurchase_DateDiscount_PriceReturn_DateCustomer_ID
1001/1/2015003/1/201
1011/20/201900 2
1023/1/2074004/1/203

 

Desired Measure: Net Sales (anything within the currently selected filter range, for example, March 2020 less March 2020 returns) 7400-1500=5900.

 

I feel like an idiot for not being able to make something so simple work but I may be overthinking it. Everything I do only sums by purchase date-regardless of when the return was returned.

2 ACCEPTED SOLUTIONS
jdbuchanan71
Super User
Super User

@mattramirez2020 

The idea of what you are trying to do is pretty straight forward, it's the how that is a little difficut at first.  You will need a Dates table in your model, this is a dedicated table that holds the calendar for your model to use.  Then you link your Sales table to the Dates table using the [order date], this is the primary relationship.  You also link it using the return date, this is a secondary inactive relationship.

Then we can use the secondary relationship to calculate the return amount.

jdbuchanan71_0-1594754187435.png

We start with a measure to sum the price.

Price = SUM ( Sales[Discount_Price] )

Then we write a measure to calculate the return price and that is where we activate the secondary relationship.

Return Amount = 
CALCULATE (
    [Price],
    USERELATIONSHIP ( Sales[Return_Date], Dates[Date] ),
    NOT ISBLANK ( Sales[Return_Date] )
)

Then a measure to calculate the difference.

Total Amount = [Price] - [Return Amount]

jdbuchanan71_1-1594754340226.png

I have attached my sample file for you to look at.

 

View solution in original post

@mattramirez2020 

The problem is Net Sales is [Sales] - [Returns + Exchanges] except in Jun the amount is - so that would make Net Sales + 895
0 - ( -895) and that is different than the calc for Apr.  You will see the Net Sales sign behavior does not match between those two months:

jdbuchanan71_0-1594849212115.png

You can trick it with two new measures

Price no Exchanges = 
CALCULATE(
    SUM ( Sales[Discount_Price] )
    ,Sales[Exchange_Purchase_ID] = 0
)
Returns + Exchanges = 
VAR _ReturnNoExchange = 
    CALCULATE (
        SUM ( Sales[Discount_Price]),
        USERELATIONSHIP ( Sales[Return_Date], Dates[Date] ),
        NOT ISBLANK ( Sales[Return_Date] ),
        NOT Sales[Purchase_ID] IN VALUES ( Sales[Exchange_Purchase_ID] )
    )
VAR _ReturnWithExchange = 
    CALCULATE (
        SUM ( Sales[Discount_Price]),
        USERELATIONSHIP ( Sales[Return_Date], Dates[Date] ),
        NOT ISBLANK ( Sales[Return_Date] ),
        Sales[Purchase_ID] IN VALUES ( Sales[Exchange_Purchase_ID] )
    )
VAR _ExchangeFromReturn =  
    CALCULATE(
        SUM ( Sales[Discount_Price] )
        ,Sales[Exchange_Purchase_ID] <> 0
    )

RETURN _ReturnNoExchange + _ExchangeFromReturn - _ReturnWithExchange

You leave the total amount as the regular [Price] - [Returns] and you get something like this:

jdbuchanan71_1-1594849440884.png

The green highlight just shows that the Total uses the base Price + Returns but you would not show that in your visual but again, the sign for Apr and Jun don't match.

 

What about showing it in 3 buckets?  Price w/o Exchanges, Returns w/o Exchanges, Exchanges.  Then the total is A - B + C and it at least looks more reasonable:

jdbuchanan71_2-1594849946351.png

Anyway, I have attached my updated sample file, take a look.

 

 

 

View solution in original post

5 REPLIES 5
jdbuchanan71
Super User
Super User

@mattramirez2020 

The idea of what you are trying to do is pretty straight forward, it's the how that is a little difficut at first.  You will need a Dates table in your model, this is a dedicated table that holds the calendar for your model to use.  Then you link your Sales table to the Dates table using the [order date], this is the primary relationship.  You also link it using the return date, this is a secondary inactive relationship.

Then we can use the secondary relationship to calculate the return amount.

jdbuchanan71_0-1594754187435.png

We start with a measure to sum the price.

Price = SUM ( Sales[Discount_Price] )

Then we write a measure to calculate the return price and that is where we activate the secondary relationship.

Return Amount = 
CALCULATE (
    [Price],
    USERELATIONSHIP ( Sales[Return_Date], Dates[Date] ),
    NOT ISBLANK ( Sales[Return_Date] )
)

Then a measure to calculate the difference.

Total Amount = [Price] - [Return Amount]

jdbuchanan71_1-1594754340226.png

I have attached my sample file for you to look at.

 

@jdbuchanan71  So this worked almost perfectly except I realized I am inflating sales and inflating returns because I don't take into account exchanges properly. I would love your feedback on the best way to handle exchanges if at all possible!!

 

In my example below, May sales would be $4895, June sales would be $0, June exchange difference would be -$895.

 

Purchase_IDPurchase_dateDiscount_PriceReturn_DateExchange_Purchase_ID
1001/1/2015003/1/200
1011/20/201900 0
1023/1/2074004/1/200
1035/6/2028956/8/200
1045/6/202000 0
1056/8/202000 

103

 

Right now it gives me an output that nets out correctly but I would rather it be like this:

Basically the most important thing I am trying to accomplish to a way to reference on ID 105 that it is an exchange from purchase_ID 103 that does not include a $2895 in sales and then a $2000 in returns but just a -$895 in exchange difference. 

 

 SalesReturns+ExchangesNet Sales
Jan3400  3400
Feb   0
Mar74001500 5900
Apr 7400 -7400
May4895  4895
Jun  -895-895

@mattramirez2020 

The problem is Net Sales is [Sales] - [Returns + Exchanges] except in Jun the amount is - so that would make Net Sales + 895
0 - ( -895) and that is different than the calc for Apr.  You will see the Net Sales sign behavior does not match between those two months:

jdbuchanan71_0-1594849212115.png

You can trick it with two new measures

Price no Exchanges = 
CALCULATE(
    SUM ( Sales[Discount_Price] )
    ,Sales[Exchange_Purchase_ID] = 0
)
Returns + Exchanges = 
VAR _ReturnNoExchange = 
    CALCULATE (
        SUM ( Sales[Discount_Price]),
        USERELATIONSHIP ( Sales[Return_Date], Dates[Date] ),
        NOT ISBLANK ( Sales[Return_Date] ),
        NOT Sales[Purchase_ID] IN VALUES ( Sales[Exchange_Purchase_ID] )
    )
VAR _ReturnWithExchange = 
    CALCULATE (
        SUM ( Sales[Discount_Price]),
        USERELATIONSHIP ( Sales[Return_Date], Dates[Date] ),
        NOT ISBLANK ( Sales[Return_Date] ),
        Sales[Purchase_ID] IN VALUES ( Sales[Exchange_Purchase_ID] )
    )
VAR _ExchangeFromReturn =  
    CALCULATE(
        SUM ( Sales[Discount_Price] )
        ,Sales[Exchange_Purchase_ID] <> 0
    )

RETURN _ReturnNoExchange + _ExchangeFromReturn - _ReturnWithExchange

You leave the total amount as the regular [Price] - [Returns] and you get something like this:

jdbuchanan71_1-1594849440884.png

The green highlight just shows that the Total uses the base Price + Returns but you would not show that in your visual but again, the sign for Apr and Jun don't match.

 

What about showing it in 3 buckets?  Price w/o Exchanges, Returns w/o Exchanges, Exchanges.  Then the total is A - B + C and it at least looks more reasonable:

jdbuchanan71_2-1594849946351.png

Anyway, I have attached my updated sample file, take a look.

 

 

 

@jdbuchanan71 This worked! So I realized I labeled my columns poorly and caused a ton of confusion. I like returns and exchanges being in their own columns and used your last format below. Apologies for the confusion but I appreciate you catching it and all of your help. I modified slightly and changed returns to being negative so now returns and exchange difference both show up negative and get added correctly for net amount.

 worked perfectly! This is so cool! Thank you @jdbuchanan71 

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.